Finder Alias – Create Finder Alias in Bash Script with Command-Line Arguments

aliasautomationbashfinderterminal

I'd like to write a bash script that takes 2 inputs:

  1. path to actual file
  2. path of a Finder alias file (not a unix link) to be created that refers to 1.

The bash script should pass these inputs to an automation script that creates the alias.

I've seen How to use AppleScript in a bash script to create an alias for an app? but I have not been able to adapt it for my goal.

How can I do this?

Best Answer

With minimal error handling:

#!/bin/sh

[ -f "$1" ] || exit 1
[ "$2" ] || exit 1

alias=$(basename "$2")

/usr/bin/osascript <<EOF
tell application "Finder"
    set myapp to POSIX file "$1" as alias
    make new alias to myapp at Desktop
    set name of result to "$alias"
end tell
EOF

mv ~/Desktop/"$alias" "$2"