Creating Unix-style links instead of aliases

While listening to the Mac Observer’s Mac Geek Gab podcast tonight, a caller asked why we couldn’t create unix hard links from the Finder. After I thought about it, I conjured up the following script. It creates symbolic links for folders (the standard in unix) and hard links for files.

--ask for clarification
display dialog "Do you want to create a link to a file or folder?" buttons {"Cancel", "Folder", "File"} default button 3
set btn to button returned of the result
--get the Source item
if btn is "Folder" then
	set theSource to POSIX path of (choose folder with prompt "Select the target folder" default location (path to home folder) with invisibles)
	set symlink to "s"
else if btn is "File" then
	set theSource to POSIX path of (choose file with prompt "Select the target file" default location (path to home folder) with invisibles)
	set symlink to ""
end if
--get the target item
set theTarget to POSIX path of (choose file name with prompt "Create the target file" default name "newlink" default location (path to desktop folder))
set lnCmd to "ln -vh" & symlink & space & quoted form of theSource & space & quoted form of theTarget

--do the command
try
	set theResult to do shell script lnCmd
on error the error_message number the error_number
	display alert "Error: " & the error_number & ". " message the error_message buttons {"Cancel"} default button 1 as warning
end try
if theResult contains "=>" then
	display alert "File Link Created" message "The ln command returned: " & return & theResult as informational
else if theResult contains "->" then
	display alert "Folder Link Created" message "The ln command returned: " & return & theResult as informational
else
	display alert "Link Failed" message "Ln returned: " & theResult as informational
end if

Neat, Kevin, but why do I want to do it?

If you use Terminal or X11 very much (and programs that can be gotten with Fink or MacPorts), then making these links helps a lot, since the “unix-y” side of the Mac can’t understand the Finder’s aliases. They just don’t work from Terminal or an XTerm window or other unix program.

Interestingly, however, the unix-style links that this script creates ARE recognized in the Finder (since they operate at a much lower level of the file system than the Finder).