larryy larryy - 1 year ago 142
AppleScript Question

Make alias script failing

Infrequent AppleScript user here, so this may be something incredibly basic, but I can't seem to make an extremely simple script to create a new alias file work. Here's the script in its entirety:

set source_file to "/path/to/test.txt"
set alias_file to "/path/to/test.txt alias"
tell application "Finder" to make new alias at alias_file to source_file

I've tried it with and without the "new". I've tried it with "POSIX file" in front of the file names and with "as POSIX file" as a coercion after the file names. I've tried with "at * to *" and "to * at *". Just in case the destination needed to be a containing folder I've tried that. Absolutely all variations produce the same error message:

execution error: Finder got an error: AppleEvent handler failed. (-10000)

which doesn't tell me a lot.

I've obviously replaced the actual file paths with "/path/to/" here, but I can assure that
ls /path/to/test.txt
confirms the source path is valid, and
ls "/path/to/test.txt alias"
confirms the target path does not exist.

In case it matters, I'm running Mac OS X 10.11.5. The Finder.sdef entry for make sure looks like it should do what I want:

make v : Make a new element make

new type : the class of the new element

at location specifier : the location at which to insert the element

[to specifier] : when creating an alias file, the original
item to create an alias to or when creating a file viewer window,
the target of the window

[with properties record] : the initial values for the properties of
the element → specifier : to the new object(s)

What I really want to do is run this with osascript from the command line and what I really, really want to do is invoke the osascript one-liner from Python, so the file paths will be inline, rather than in variables. But I moved first to the command line and then to the script editor since I couldn't get it to work, and every single method of invoking this code snippet produces the same error message. So hopefully when/if I get a script to work I'll be able to invoke equivalent code from osascript from Python. :}

Answer Source

You were definitely on the right track with using POSIX file since AppleScript represents file paths differently than POSIX does (in essence, colons instead of forward slashes).

You could manually translate all the paths into AppleScript paths, but casting them is, I think, a better solution in order to keep the file paths readable (and make it clear to you reading the source code that they are indeed file paths).

However, the problem with POSIX file is that it returns a file reference instead of the text path that the make new alias command is looking for. To resolve this, all you have to do is cast the returned file reference as text to make alias happy:

set source_file to (POSIX file "/path/to/test.txt") as text
set alias_file to (POSIX file "/path/to/test.txt alias") as text
tell application "Finder" to make new alias at alias_file to source_file

There's one more problem, though: the make new alias at x to y command expects y to be a path to a file and x to be a path to a directory where the alias should be placed, but you're passing a path to a file for both x and y. The destination ("at") path should just be /path/to/ – the make new alias command will automatically name the alias {original filename} alias. So, in summation:

set source_file to (POSIX file "/path/to/test.txt") as text
set alias_dir to (POSIX file "/path/to/") as text
tell application "Finder" to make new alias at alias_dir to source_file

That may have been a little long-winded, but I hope it helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download