Create symbolic links in the Finder via AppleScript

Dec 01, '06 07:30:01AM

Contributed by: mistersquid

As much time as I spend "under the hood" (as opposed to in the 'hood) in OS X, I'm surprised that I haven't encountered this problem before, which is: aliases made in the Finder (with Command-L) are not navigable from the command line. I was shocked. Having need for a quick-and-dirty way to make symbolic links in the GUI, I whipped up the following AppleScript. Copy the following code and paste it into a new Script Editor window.

on open (docpath)
  tell application "Finder"
    set docpath to docpath as text
    set search_string to ":"
    set replacement_string to "/"
    set oldDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to the search_string
    set docpath to text items of docpath
    set AppleScript's text item delimiters to the replacement_string
    set docpath to text items of docpath as text
    set unixpath to "/Volumes/" & docpath
    set AppleScript's text item delimiters to the oldDelims
  end tell
  if last text item of (unixpath as text) is equal to "/" then
    set unixpath to (text 1 through 
     ((length of unixpath) - 1) of unixpath)
  end if
  do shell script "ln -s " & quoted form of unixpath & " " & 
   quoted form of unixpath & ".sym"
end open
Save the file as a Run Only application (with or without Startup Screen). When you drop a file or folder onto the resulting application, a symbolic link will be made in the same directory as the file, with the extension .sym. I'm sure there are ways to improve on this script (e.g. batch functionality), and I look forward to what other MacOSXHints community has to say.

[robg adds: This worked as described in my testing. If you work with symbolic links and aliases in Terminal and Finder, there are a few other hints here that might be of interest: A script to reveal alias paths in the Terminal, A script to convert aliases to symlinks, Enable 'cd' into directory aliases from Terminal.]

Comments (10)

Mac OS X Hints