vy32
vy32

Reputation: 29635

How do I create a Macintosh Finder Alias From the Command Line?

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?

Upvotes: 17

Views: 12119

Answers (6)

juandesant
juandesant

Reputation: 753

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.

Upvotes: 1

mivk
mivk

Reputation: 14824

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'

Upvotes: 0

Leandros
Leandros

Reputation: 16825

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")'

Upvotes: 9

Eir Nym
Eir Nym

Reputation: 1579

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"'

Upvotes: 13

vy32
vy32

Reputation: 29635

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.

Upvotes: 2

Philip Regan
Philip Regan

Reputation: 5045

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.

Upvotes: 0

Related Questions