Converting a POSIX path containing colons (slash) in a folder name component into an HFS path (alias) is not working

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.

This may not be what you want to hear, but a quick test here under 10.14.6 shows your scripts working fine.

I’m going to presume this undocumented method is based on the same ultimate code:

use framework "Foundation"
use framework "OSAKit"
use scripting additions

set thePath to current application's NSString's stringWithString:"/Slash:"
thePath's valueForKey:"_osa_hfsPath"

Huh, I did also test my scripts on 10.14.6, even on a different computer and found that they did not work with file names containing a slash, whereas they worked with other file names. Just to make sure - you did try the third script with the set x to POSIX file "/Slash:" line, and even though you had a Folder named “Slash/” in your root dir, the script just ran and returned the same name? And you did create that folder, right? Without the folder, the bug won’t trigger.

To other readers: Can any of you reproduce the issue I’ve described?

Which undoc’d method do you refer to? _osa_hfsPath? When I run that, with the “Slash/” folder present, I get the same incorrect result on 10.14.6, i.e. I get “Slash” and not “Slash/”.

I did it with a file called Slash/, but it fails when I do it with a folder called Slash/.

Jeff Johnson found a solution.

A conversion to Unicode text is needed in between, like this:

tell application "Finder"
	set pp to "/Slash:"
	set theFile to (pp as POSIX file)
	set HFSPath to (theFile as Unicode text)
	open information window of alias HFSPath
end tell

Huh, so it only happens with folders, not with files. I should’ve checked.

You might also want to test with 10.15. In the early betas, this:

POSIX path of (path to desktop)

was returning something like:

"/Users/shane/Desktop:/"

It’s since been fixed, but it suggests someone’s been fiddling with the underlying code in this very area.