17

I am trying to create a Finder Alias from the command line or from a Python program. I have searched the web[1][2][3] and found this bit of applescript:

$ osascript -e 'tell application "Finder" to make new alias at POSIX file "/Users/vy32/cyber.txt" to POSIX file "/Users/vy32/a/deeper/directory/cyber.txt"'

Unfortunately, it gives me this error:

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

[1] http://hayne.net/MacDev/Bash/make_alias

[2] https://discussions.apple.com/thread/1041148?start=0&tstart=0

[3] http://hintsforums.macworld.com/showthread.php?t=27642

What am I doing wrong?

2

6 Answers 6

13

about your message, try to look at Console.app. May be source file does not exists. This helps me when I try to make Xcode 4.3+ applications visible. I've found working code:

$ osascript -e 'tell application "Finder" to make alias file to POSIX file "/file/to/make/link/from" at POSIX file "/folder/where/to/make/link"'

for example:

$ osascript -e 'tell application "Finder" to make alias file to POSIX file "/Applications/Xcode.app/Contents/Applications/OpenGL ES Performance Detective.app" at POSIX file "/Users/mylogin/Applications"'
4
  • This gives an error as pointed out by @Leandros, whose solution actually works... Jan 12, 2016 at 9:12
  • @MarkSetchell can you tell more about your environment?
    – Eir Nym
    Jan 12, 2016 at 9:15
  • Hi there. I am running OSX El Capitan on iMac and I get exactly the same error Leandros points out. Jan 12, 2016 at 9:17
  • I see. I've got it working with osascript -e 'tell application "Finder" to make alias file to POSIX file "/Applications/Xcode.app/Contents/Applications/Instruments.app" at POSIX file "/Users/mylogin/Applications"'
    – Eir Nym
    Jan 12, 2016 at 12:55
8

For everyone struggling with the AppleEvent handler failed error:

make alias doesn't work like ln -s, you don't have to specify the destination file, you have to specify the destination directory, the filename of the alias is the name of the source file/folder.

Example:

osascript -e 'tell application "Finder" to make alias file to (POSIX file "/Applications/Mail.app") at (POSIX file "/Users/leandros/Desktop")'
1
  • More clarifications: The first argument is the original file or folder. The second argument is the folder where the alias will be created. This folder must already exist. You cannot name the alias, it will simply be given the name of the file or folder in the first argument (you can rename it afterwards).
    – wisbucky
    Sep 9, 2019 at 19:28
1

This approach works from the command line:

osascript -e 'tell application "Finder" to make alias file to alias "imac:Users:vy32:current:cyber.txt" at "imac:Users:vy32:foobar"'

Where foobar is a directory in my homedir.

1
  • The other examples wouldn't work for me, but this one, with old style "Mac OS" file paths does. Thanks.
    – user1985657
    Apr 16, 2014 at 17:28
1

This is a function (tested with zsh; might work with bash) that can be used to create aliases for files in the same folder as the original exists, and in the Downloads folder in case the Finder does not have permissions to write in the same folder (i.e., for aliases to apps that come from the /System/Applications, instead of the /Applications folder).

function mfalias {
  for file in $*; do  
    original_path=$file
    alias_path=$(dirname $original_path)
    alias_name="Alias to $(basename $original_path)"
    #echo "Aliasing \"$original_path\" at \"$alias_path\" with name \"$alias_name\""
    osascript -e "tell application \"Finder\"" \
              -e "  try" \
              -e "    make new alias to (POSIX file \"$original_path\") at (POSIX file \"$alias_path\") with properties {name:\"$alias_name\"}"\
              -e "  on error errorMsg"\
              -e "    make new alias to (POSIX file \"$original_path\") at (path to downloads folder) with properties {name:\"$alias_name\"}"\
              -e "  end try"\
              -e "end tell"
  done
}

You can remove the comment on the echo line if you want to see explicitly file and path names.

0

The working command in Applescript:

tell application "Finder"
    make new alias at POSIX file "/Path/to/location" to POSIX file "/Path/to/file.ext"
end tell

Add salt to taste.

7
  • @Phillip Regan Doesn't it require actual alias references though? Aug 16, 2011 at 0:27
  • @fireshadow52: Nope. I grabbed the path to a random file and used my home folder (where I only store the OS defaults), and it just worked (no pun intended); an alias appeared at the location I specified. Aug 16, 2011 at 1:02
  • I'd really like to be able to do this from the command line, but when I take your code and change it to osascript -e 'tell application "Finder" to followed by your second line, it doesn't work... ?
    – vy32
    Aug 16, 2011 at 1:16
  • 1
    @vy32 Did you try changing " to \"? Aug 16, 2011 at 1:20
  • 1
    @fireshadow52, the " is inside a ' so it gets passed through without change.
    – vy32
    Aug 16, 2011 at 1:21
0

Here is a generic way, using variables, to do it at the command-line or from a shell script. And it also sets the name of the created alias.

The problem when using variables, is that you cannot use single quotes in the osascript commands, so the double quotes in the script need to be escaped.

file="$HOME/some_file.txt"
alias_dir="$HOME/Desktop/my aliases"
alias_name="alias_to_some_file"
 
osascript -e "tell application \"Finder\" to make alias file to (POSIX file \"$file\") at (POSIX file \"$alias_dir\") with properties {name:\"$alias_name\"}"

You can also use separate -e parameters which are then treated as separate Applescript lines. This makes the syntax slightly different:

osascript -e 'tell application "Finder"' \
  -e "make alias file to (POSIX file \"$file\") at (POSIX file \"$alias_dir\") with properties {name:\"$alias_name\"}" \
  -e 'end tell'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.