« Patchwork Copyright | Main | God Save »

Make Symbolic LInks in the Finder Using AppleScript

As much time as I spend “under the hood” (as opposed to in the ’hood) in OS X, I’m surprised 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 1 and paste it into a new Applications > AppleScript > Script Editor.app window.

on open (docpath)

tell application "Finder"

activate

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”) or download a precompiled copy of the application.2 When one drops 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”. end of article


20061201 UPDATE: My hint is now up at MacOSXHints.com, and you can check yourself for useful comments and/or modifications. I will post any modifications I find helpful.

For now, the editor of MacOSXHints.com, Rob Griffiths, has collated links to several other hints helpful to those of us who “work with symbolic links and aliases in Terminal and Finder.” One of those linked hints I found particularly useful as it provides an elegant means to cd into the directories to which system aliases point.


Notes
1 I submitted the main contents of this post to MacOSXHints.com. If and when that submission gets posted, I will provide a link from here as well as detailing any MacOSXHints community contributions I consider to be improvements (e.g. batch functionality).
2 Applying Apple’s Security Update 2006-007 or something that happened between 30 November 2006 and 1 December 2006, appears to have changed the behavior of the way in which Apache uploads AppleScript MIME types or in which Safari downloads such MIME types.