« God Save | Main | A Game of Love »

Symbolic Links in the GUI via AppleScript, PLUS

Members of the MacOSXHints community have commented and provided some improvements to my original code and the editor of MacOSXHints.com, Rob Griffiths, has posted a brief webography of related scripts and utilities in the editor’s comments of my hint submission.

First, a much improved version written by jonn8n which has (drag and drop) batch functionality. As before, paste into a new AppleScript Editor window and save it as an application, with or without startup screen.

on run

 open {choose file with prompt "Choose a file to create a symbolic link:" without invisibles}

end run

on open the_files

 repeat with i from 1 to (count the_files)

  try

   set posixpath to POSIX path of (item i of the_files)

   if posixpath ends with "/" then set posixpath to text 1 thru -2 of posixpath

   do shell script "ln -s " & quoted form of posixpath & " " & quoted form of (posixpath & ".sym")

  end try

 end repeat

end open

I think jonn8n’s version is different enough to warrant a new name. That is, I don’t consider it to be my program, and I decided to name my local copy “symbolic.app”.

Second, apparently an oldie-but-not-quite-forgotten (2002!) shell script to convert aliases to symlinks replaces command line-troublesome aliases with symbolic links.1

#!/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"`

  item_parent="`cd \"${item_parent}\" 2>/dev/null && pwd || echo \"${item_parent}\"`"

  item_path="${item_parent}/${item_name}"

  linksource=`osascript<<EOS

tell app "Finder"

 set theItem to (POSIX file "${item_path}") as alias

 if the kind of theItem is "alias" then

  get the posix path of (original item of theItem as text)

 end if

end tell

EOS`

  if [ $? -eq 0 ]; then

   if [ ! -z "$linksource" ]; then

   ln -fs "${linksource}" "${item_path}"

   echo "\"${1}\" -> \"${linksource}\""

   fi

  fi

 shift

 fi

done

Paste that code 2 into a text editor and save it as (I did) “alias2sym”. Make the script executable using, for example, “chmod 755 alias2sym”. Put the code somewhere in your $PATH, perhaps (as I did, and if you have administrator privileges) /usr/local/bin/.3

Finally, In an update to my last post regarding this topic, I provided a link to a hint which explains how to add system alias navigability to the command line. That hint requires the presence of Apple’s Developer Tools. Before embarking on adding such functionality, n00bs should be aware that in Mac OS 10.4.8 the bash function “cd” should be articulated in “.bash_profile” as its presence in “.bashrc” does nothing.

That is all. end of article

Notes
1 As tbdavis, the author of “Enable 'cd' into directory aliases from the Terminal,” notes, system aliases have features (such as labels) which symbolic links do not. So instead of replacing system aliases with symbolic links, you may want to give yourself the ability to follow aliases from the command line, which the next part of this PLUS entry (after this note) explains how to do.
2 This code is a modification of tbdavis’s original according to suggestions made by ClarkGoble.
3 Yes, it is not, technically, a binary.