Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create symbolic links in the Finder via AppleScript Apps
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"
    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). 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.]
    •    
  • Currently 3.17 / 5
  You rated: 1 / 5 (6 votes cast)
 
[22,263 views]  

Create symbolic links in the Finder via AppleScript | 10 comments | Create New Account
Click here to return to the 'Create symbolic links in the Finder via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create symbolic links in the Finder via AppleScript
Authored by: ctierney on Dec 01, '06 11:58:17AM

Thanks for the tip. Though for my purposes I'm glad the finder still makes olde school aliases, since they are used in application bundles to support classic apps. That said, if you want to boil it down some more, you could use the following:
<pre><code>set unixpath to POSIX path of docpath</code></pre>

--
Cole



[ Reply to This | # ]
Create symbolic links in the Finder via AppleScript
Authored by: ctierney on Dec 01, '06 12:06:42PM
Oops. Need to select html formatted. Just call me mr. Olde Schoole. :)
set unixpath to POSIX path of docpath
-- Cole

[ Reply to This | # ]
Create symbolic links in the Finder via AppleScript
Authored by: jonn8n on Dec 01, '06 12:02:09PM
That's a lot of extra, unnecessary work. Save this code as as a AppleScript application and then just drag and drop files & folders on to its icon in the Finder to create symbolic links:
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 posix_path to POSIX path of (item i of the_files)
			if posix_path ends with "/" then set posix_path to text 1 thru -2 of posix_path
			do shell script "ln -s " & quoted form of posix_path & " " & quoted form of (posix_path & ".sym")
		end try
	end repeat
end open
Jon

[ Reply to This | # ]
Create symbolic links in the Finder via AppleScript
Authored by: mistersquid on Dec 01, '06 05:44:16PM
: ) Thank you. My code is often verbose and plodding. (Psst: the word you're looking for is inelegant). I will post and credit your improvement on my blog.

[ Reply to This | # ]
Create symbolic links in the Finder via AppleScript
Authored by: mistersquid on Dec 01, '06 05:51:44PM

jonn8n,

Test driving the prompt branch of your script, I was excited for a second that one might be able to select multiple files in the prompt and thereby have batch file functionality.

How hard is such a thing to code and/or anyone out there willing to chuck it in?

I really have a thing for making symbolic links in the GUI.

msq



[ Reply to This | # ]
Create symbolic links in the Finder via AppleScript
Authored by: jonn8n on Dec 02, '06 02:02:22PM

The script will only use one file if you run it by double-clicking in the Finder (or launching it some other way such as opening from the Scripts menu). However, if you drag and drop items on to its icon in the Finder, then it will iterate through all of the items and make the symbolic links for each item.

Jon



[ Reply to This | # ]
Create symbolic links in the Finder via AppleScript
Authored by: rumirocks on Dec 01, '06 03:59:45PM

Doesn't this contextual plugin do the same thing? You can find any symbolic link in the terminal with it:
http://seiryu.home.comcast.net/symboliclinker.html



[ Reply to This | # ]
Create symbolic links in the Finder via AppleScript
Authored by: mr. applescript on Dec 02, '06 10:03:14AM
BTW, speaking of links, if you want to make an alias file to a disk item, use this approach:
set this_item to choose file
tell application "Finder"
	make new alias file to this_item at desktop with properties {name:"My Link"}
end tell
And here's how to make a link to a URL:
tell application "Finder"
	make new internet location file at desktop with properties {location:"http://www.macosxhints.com"}
end tell


[ Reply to This | # ]
Create symbolic links in the Finder via AppleScript
Authored by: mistersquid on Dec 02, '06 01:01:50PM

Wow, you didn't even read the hint at all, did you?



[ Reply to This | # ]
Create symbolic links in the Finder via AppleScript
Authored by: ddauerbach on Dec 03, '06 10:16:46AM

PathFinder, an invaluable utility (I've replaced Finder) will do this (offers a choice between an alias and a symbolic link).



[ Reply to This | # ]