Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Script to convert Aliases to SymLinks?

Script to convert Aliases to SymLinks?
Thread Tools
clarkgoble
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Oct 17, 2002, 08:45 PM
 
Hey, as many of you know FTP doesn't recognize Aliases. So I was going to write either a shell script, python script or applescript to convert them into SymLinks. Then I discovered that I can't figure a way to tell if something is an alias!

Anyone have a clue how to do this?
     
clarkgoble  (op)
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Oct 17, 2002, 09:01 PM
 
Never mind - I figured it out. I'll post the script in a little bit since I know a few others were interested.
     
absmiths
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Oct 18, 2002, 11:01 AM
 
Originally posted by clarkgoble:
Hey, as many of you know FTP doesn't recognize Aliases. So I was going to write either a shell script, python script or applescript to convert them into SymLinks. Then I discovered that I can't figure a way to tell if something is an alias!

Anyone have a clue how to do this?
Please do - but do you loose the function of Aliases when you convert them into symlinks?
     
clarkgoble  (op)
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Oct 18, 2002, 01:23 PM
 
I'm redoing it in Applescript - which I'm just learning. The problem with Python was that macfs still uses the HFS paths (Volume:Users:user:folder) but when I call the Python script I have only the Posix path (/Users/usr/folder) I couldn't figure out how to get that initial Volume. I'm sure there was a way but I didn't have enough time to wade through searches.

Symlinks have most the functionality of Aliases so in practice it won't make that much difference. If you are dealing with directories (which was my problem with FTP) then you should notice any differences at all. Some situations with files will show some differences though.
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Oct 18, 2002, 08:46 PM
 
Sorry, but I couldn't help chuckling: that exact problem tripped me up during a scripting project a while back, and I've read of several other folks who have run into the same thing. **How do you work out what the boot volume is called using nothing but "pure unix"?** I guess the answer is that the name of the boot volume is not accessible to anything I'd consider "pure unix". (ie, no cheating via osascript et al.) It's a royal pain!

Anyone want to have a go at slicing through this Gordian knot? Or at least definitively declaring it impossible?

Cheers,
Paul
     
Mactoid
Grizzled Veteran
Join Date: Sep 2000
Location: Springfield, MA
Status: Offline
Reply With Quote
Oct 18, 2002, 10:35 PM
 
It's defiantly not impossible, but nothing nice and clean either. Here's a start:
disktool -l | grep \\'/\\'

Parsing out the volume name I will leave as an exercise for the reader.
We hope your rules and wisdom choke you / Now we are one in everlasting peace
-- Radiohead, Exit Music (for a film)
     
clarkgoble  (op)
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Oct 19, 2002, 02:36 PM
 
It is a common enough need that Apple really ought to provide some command for it.
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Oct 20, 2002, 05:02 AM
 
Here's a start:
disktool -l | grep \\'/\\'

Parsing out the volume name I will leave as an exercise for the reader. [/B]
Thanks for the pointer Mactoid: here's an ugly attempt at getting the volname...

disktool -l | perl -ne "print \$1,\"\n\" if m|'/'.*volName = '(.+)'|"

Blrrk: ugly quotes everywhere!

Cheers,
Paul
     
Mactoid
Grizzled Veteran
Join Date: Sep 2000
Location: Springfield, MA
Status: Offline
Reply With Quote
Oct 20, 2002, 01:20 PM
 
Originally posted by Paul McCann:
Thanks for the pointer Mactoid: here's an ugly attempt at getting the volname...
No problem. And thank you for the perl code. I'd say you found a fairly elegant solution considering the messy input (and perl code in general )
We hope your rules and wisdom choke you / Now we are one in everlasting peace
-- Radiohead, Exit Music (for a film)
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Oct 20, 2002, 09:51 PM
 
Or, you could use osascript, like so:

osascript -e 'tell application "Finder"' -e 'name of startup disk' -e 'end tell'

A little easier to read...though not as cool as using disktool, I'd suppose.

Edit: ...though I'd probably use AppleScript, as it handles HFS-style paths, and has the nice POSIX path property for files and folders...
( Last edited by Wevah; Oct 20, 2002 at 10:03 PM. )
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Oct 21, 2002, 09:30 AM
 
Originally posted by Wevah:
Or, you could use osascript, like so:

osascript -e 'tell application "Finder"' -e 'name of startup disk' -e 'end tell'

A little easier to read...though not as cool as using disktool, I'd suppose.
Aah, so you missed that "no cheating via osascript et al" up above!

Yuck to multiple -e's:

osascript -e 'tell app "Finder" to return the name of the startup disk'

and much cooler (if you can look past the trailing colon... urgh, bad mental image there)

osascript -e '(path to startup disk) as string'

Cheers,
Paul
     
absmiths
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Oct 21, 2002, 11:06 AM
 
Originally posted by Wevah:
Or, you could use osascript, like so:

osascript -e 'tell application "Finder"' -e 'name of startup disk' -e 'end tell'

A little easier to read...though not as cool as using disktool, I'd suppose.

Edit: ...though I'd probably use AppleScript, as it handles HFS-style paths, and has the nice POSIX path property for files and folders...
When I run that it complains that it can't connect to the WindowServer - does AppleScript initialize the window system whether it needs to or not?

Edit: Never mind, I think it's because the script tells Finder, and Finder isn't running unless someone is logged in.
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Oct 21, 2002, 03:40 PM
 
Yeah, you're right...I wasn't paying attention...but by the same token, wouldn't using any other OS X-specific binary be cheateing too?

;P
     
clarkgoble  (op)
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Oct 21, 2002, 04:35 PM
 
Does anyone else out there use Python? When I load macfs it gives me the error macfsn can't load. That is some special version of the macfs library that sometimes is loaded. I know that the Mach-O version of Python largely borrows the application version of Python's libraries. However there seems to be a problem between the two, keeping my shell script from working.

Anyone else have problems converting between the two forms of Python?
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Oct 21, 2002, 11:09 PM
 
Originally posted by Wevah:
Yeah, you're right...I wasn't paying attention...but by the same token, wouldn't using any other OS X-specific binary be cheateing too?

;P
Yep. But it cheats a lot quicker than the applescript solutions, so I'm prepared to look the other way! In some sense I guess that there's really no way that a "generic" unix command could reach outside of its known universe and grab that piece of data. (ie, "the world begins in a slash").

Of course the "pure/generic unix" concept is decidely fuzzy, so I'll bow out now rather than sink into that bed of quicksand. The "disktool" command operates nicely via ssh from another machine --without having to open any vulnerabilities such as applescript over IP-- and that is another plus.

[[clarkgoble: I don't use python, but if you post the script skeleton it might be translated. Actually, I have an applescript that does something like what you're seeking, but not having touched it since 10.0 ish it'll need a bit of spring cleaning and testing. 10.2, for example, reports symlinks as aliases when using applescript. I think this is relatively new, but have no way of checking anymore.]]

Cheers,
Paul
     
Vash
Dedicated MacNNer
Join Date: Apr 2002
Location: Boston, MA
Status: Offline
Reply With Quote
Oct 22, 2002, 03:00 PM
 
I had that problem with FTP aliases as well. Is what you other guys posted a solution to it in perl?? I am no expert unix user or anything like that so I can't tell. But I would appreciate somebody posting a solution of some sort to convery aliases to Systemlinks or whatever FTP needs. If nobody know how, could someone tell me how to make a Systemlink?
Revenge is a meal best served cold.
     
clarkgoble  (op)
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Oct 22, 2002, 05:31 PM
 
I don't know Perl or the Perl libraries. So I can't speak there. The problem with Python is that the libraries that allow one to deal with Aliases aren't working. I'm hoping this isn't due to installing Fink, as it installed its own version of Python. Anyway, I can't get macfs to load in a self running script.

With Applescript I can't seem to get the Alias filter to filter only Aliases. I'm not Applescripter so I probably just did something wrong.

As is though it may not be doable. I'd be interested if there is a Perl script to do it.
I sent some feedback to Apple on this and would encourage everyone else to as well.
     
Mithras
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Oct 22, 2002, 06:01 PM
 
Here, I wrote an Applescript to do the conversion:

You can get the script from my little downloads page. It's too long to profitable post here, I think.

You drag-and-drop a folder onto it, and it converts any aliases within the folder into symlinks.

Annoyingly, Applescript 'auto-resolved' aliases, so it doesn't work to just drop an alias on it. Perhaps that could be fixed somehow, I dunno.

I might try to work up a command-line version too - that'd be handy, no?

cheers
mithras

EDIT: I fixed the script to cope nicely with existing symlinks - earlier it bonked on them
( Last edited by Mithras; Oct 22, 2002 at 08:28 PM. )
     
Mithras
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Oct 22, 2002, 07:04 PM
 
Here's a very ugly but functional command-line script to do the same thing:

Code:
#!/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}" line_1='tell application "Finder"' line_2='set theItem to (POSIX file "'${item_path}'") as alias' line_3='if the kind of theItem is "alias" then' line_4='get the posix path of (original item of theItem as text)' line_5='end if' line_6='end tell' linksource=`osascript -e "$line_1" -e "$line_2" -e "$line_3" -e "$line_4" -e "$line_5" -e "$line_6"` if [ $? -eq 0 ]; then if [ ! -z "$linksource" ]; then rm "$item_path" ln -s "${linksource}" "${item_path}" echo "\"${item_path}\" -> \"${linksource}\"" fi fi shift fi done
This one acts directly on an alias file, rather than a folder full of them. I'd love some fixes for making it play nicer.

I put it in the download too.

cheers
mithras
( Last edited by Mithras; Oct 22, 2002 at 08:44 PM. )
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Oct 24, 2002, 08:37 PM
 
Hi Mithras,

thanks a lot for posting the links to those scripts: I particularly liked the applescript version, which contained just the right kick in the pants for my attempt at the same thing (namely the "try... on error" construct to deal with existing symlinks: very nice).

Re the shell script: just a couple of comments. That osascript monstrosity can be considerably simplified. The trick is to use backticks to capture the output of a heredoc; took me a while to get right, but having done so it's surprisingly robust and helpful. Something like

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`

is a lot prettier than that multi-line construction. The heredoc contents need be little more than a paste the original applescript. Note the loss of the extra quote marks around the variable in there as well.

Only other suggestion I'd make is to use "ln" to overwrite existing aliases. That is, instead of deleting them yourself, just use

ln -fs whereeveritis whateveritisyouwanttocallit

to "force" the creation.

Cheers,
Paul
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:50 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,