A script to convert aliases to symlinks

Oct 24, '02 09:41:07AM

Contributed by: mithras

Mac OS X's Unix programs, such as Apache and the FTP server, don't know how to read Mac OS alias files. Sometimes you encounter one where a Unix-style symlink should be used instead.

With kudos to clarkgoble at MacNN for the inspiration, and others for helpful ideas, here's a little shell script to convert aliases to symlinks.

The script:

#!/bin/sh
if [ $# -eq 0 ]; then
echo "Usage: alias2ln alias1 alias2 alias3..."
echo " where alias1, alias2, etc are alias files."
echo " Each alias file will be converted into a symlink."
fi

while [ $# -gt 0 ]; do
if [ -f "$1" -a ! -L "$1" ]; then
item_name=`basename "$1"`
item_parent=`dirname "$1"`
# Next two rows should be entered as one row #
item_parent="`cd \"${item_parent}\" 2>/dev/null
&& pwd || echo \"${item_parent}\"`"
item_path="${item_parent}/${item_name}"
line_1='tell application "Finder"'
line_2='set theItem to (POSIX file "'${item_path}'") as alias'
line_3='if the kind of theItem is "alias" then'
line_4='get the posix path of (original item of theItem as text)'
line_5='end if'
line_6='end tell'
# Next two rows should be entered as one row #
linksource=`osascript -e "$line_1" -e "$line_2" -e "$line_3"
-e "$line_4" -e "$line_5" -e "$line_6"`
if [ $? -eq 0 ]; then
if [ ! -z "$linksource" ]; then
rm "$item_path"
ln -s "${linksource}" "${item_path}"
echo "\"${1}\" -> \"${linksource}\""
fi
fi
shift
fi
done
As usual, paste this into a text file, do a chmod 755 alias2ln to make it executable, and put it in ~/bin. You can also download the script and a companion Applescript version from me.

[Editor's note: I have not tested this script.]

Comments (15)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20021024064107356