Opening Finder window by POSIX file

I cannot for the life of me figure out why these work:

tell application "Finder"
	try
		open POSIX file "/Users/user/Desktop"
	end try
	activate
end tell
set dest to POSIX file "/Users/user/Desktop"
tell application "Finder"
	try
		open dest
	end try
	activate
end tell

But this gives me an error:

set dest to "/Users/user/Desktop"
tell application "Finder"
	try
		open (POSIX file dest)
	end try
	activate
end tell

Error: no such object (e.g. specifier asked for the 3rd, but there are only 2) (errAENoSuchObject:-1728)

Can anyone explain?

I can’t explain it, but you can make it work with

open dest as POSIX file

All three of the scripts work fine for me on Sonoma, so I guess it’s a bug in whichever version you are on.

EDIT: Nevermind, Finder was just getting activated to the Desktop, not actually running the open command, and no error was showing, as @emendelson mentioned.

You won’t see the error on Sonoma because the open line is in a try block without an on error line, so the error won’t be visible. Try this version (or simply remove the try and end try lines in the original post):

set dest to "/Users/user/Desktop"
tell application "Finder"
	try
		open (POSIX file dest)
	on error err
		display dialog err
	end try
	activate
end tell

1 Like

In your first two snippets, the POSIX file specifier acts on an explicit value, namely the string containing the path to the desktop. Therefore, Finder already has a fully resolved file reference that it can operate on as one would expect.

In the final snippet, however, the string path is assigned to a variable, and the POSIX file specifier now has to act on a quantity that remains unknown until execution reaches that line. Since the line is housed inside a Finder tell block, the resolution of the file reference is attempted by Finder, which chokes because the POSIX file specifier doesnt belong to it, and so it doesnt know what to do.

However, if you make it clear that the specifier belongs to the script rather than Finder, that should solve the issue:

set dest to "/Users/user/Desktop"
tell application "Finder"
		try
				open (my POSIX file dest)
		end try
		activate
end tell

Or, more preferably:

set dest to "/Users/user/Desktop"
tell application "Finder"
		tell (my POSIX file dest) to if ¬
				it exists then open it
		activate
end tell

Or, even more preferably:

set dest to "/Users/user/Desktop"
tell application "Finder"
		tell (my POSIX file dest) to if ¬
				it exists then make new ¬
				Finder window to it
		activate
end tell

As @emendelson already observed, using POSIX file as a type class and performing a coercion also works, because a coercion takes precedence over any operation Finder will be performing, which it will therefore end up doing with a fully resolved file reference.

1 Like

Ah, thanks @CJK, that makes sense!