Using PyObjC I’m creating an application for my M1 Mac that should go into a directory of the google chrome application and delete old versions of the application. To do this, I use PyObjC to open up the alias at “/Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/Current” and try to get the path that the alias file leads to.
My implementation is as follows:
def getAliasTarget(aliasPath):
url = NSURL.fileURLWithPath_(aliasPath)
bookmarkData, error = NSURL.bookmarkDataWithContentsOfURL_error_(url, None)
if bookmarkData is None:
print(error)
return None
opts = NSURLBookmarkResolutionWithoutUI | NSURLBookmarkResolutionWithoutMounting
resolved, stale, error = (
NSURL.URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
bookmarkData, opts, None, None, None
)
)
return resolved.path()
The issue with the code is when trying to get the bookmarkData we get the error “Error Domain=NSCocoaErrorDomain Code=256 “The file couldn’t be opened.”” I believe this is some sort of permission error with accessing the BookmarkData of a file inside an application package, but I’m not sure.
I’ve tested my logic in another folder and it works as expected, the error only persists when getting BookmarkData for the path of the alias inside the application package.
SlipperyWhist18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.