I have a script that gets passed POSIX paths. It then asks the Finder to show the Info window for those items.
My code looks like this:
on show_info(pp)
tell application "Finder"
set theFile to (POSIX file pp as alias)
with timeout of 1 second
open information window of theFile
end timeout
activate
end tell
end show_info
However, when I pass a POSIX path with a component using a “/” in its Finder name (e.g. “NameWithSlash/”, like the following:
/Folder/NameWithSlash:
… then I get an error -1728 (***Finder got an error: Can’t get alias “:MyDisk:Folder:NameWithSlash:”***).
The path in the error message indicates that the file name is not correctly converted, i.e. the “:” should have been converted into a “/” but didn’t. I assume this is a (known?) bug in Apple’s AppleScript code.
Important: The folder of the probed name needs to exist, or this bug won’t show up.
To test this yourself, create a folder in your root volume, and call it “Slash/”. Then run this script:
tell application "Finder"
set volname to name of startup disk
end tell
set x to volname & ":Slash/"
tell application "Finder"
get name of alias x
end tell
If you run this, it’ll work, showing the name of the item, i.e. “Slash/”.
Now, try this by addressing the same item using a POSIX path. I tried this, but it won’t run as the alias path is incorrect:
tell application "Finder"
set x to POSIX file "/Slash:"
-- set x to POSIX file "/System" -- this works, i.e. for "normal" folders
set x to x as alias
get name of x
end tell
Now, does someone have a suggestion how to work around this bug?
Actually, I only use this AppleScript as a helper in my ObjC app, because NSWorkspace doesn’t seem to offer a command for “Show Info”. If you know another way I could use in my ObjC code, that’d work, too. E.g, I could try to create an NSAppleEventDescriptor that already holds the correct HFS path, but I can’t find a way to do that in the modern SDK API.
Update: I also tried using [NSAppleEventDescriptor descriptorWithFileURL:]
to pass the file reference, then in the script wrote set theFile to pp as alias
. This, however, has the same effect: It doesn’t create the HFS path correctly (which should be: MyDisk:Folder:NameWithSlash/
)
Update 2: Jeez! More bugs:
When I use the proposed CoreFoundation method to convert a file URL into a HFS path with CFURLCopyFileSystemPath((CFURLRef)url, kCFURLHFSPathStyle)
, the resulting HFS path is incorrect as well when the last POSIX path component ends in a colon, at least in macOS 10.13.6.
However, if I manage to create the correct HFS path with my own code, then the script works as intended. See also this related SO answer.