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

A script to convert aliases to symlinks System
Mac OS X's Unix programs, such as Apache and the FTP server, don't know how to read Mac OS alias files. Sometimes you encounter one where a Unix-style symlink should be used instead.

With kudos to clarkgoble at MacNN for the inspiration, and others for helpful ideas, here's a little shell script to convert aliases to symlinks.

The script:
#!/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"`
# Next two rows should be entered as one row #
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'
# Next two rows should be entered as one row #
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 "\"${1}\" -> \"${linksource}\""
fi
fi
shift
fi
done
As usual, paste this into a text file, do a chmod 755 alias2ln to make it executable, and put it in ~/bin. You can also download the script and a companion Applescript version from me.

[Editor's note: I have not tested this script.]
    •    
  • Currently 4.00 / 5
  You rated: 2 / 5 (4 votes cast)
 
[20,575 views]  

A script to convert aliases to symlinks | 15 comments | Create New Account
Click here to return to the 'A script to convert aliases to symlinks' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Symbolic names vs numbers
Authored by: gidds on Oct 24, '02 11:10:35AM
Why does everyone insist on using those opaque, confusing octal numbers for chmod, when the symbolic forms are much easier to understand and write, and much more flexible? In this case, a simple chmod +x would do the job just as well - and would also preserve the group and other permissions.

[ Reply to This | # ]
Symbolic names vs numbers
Authored by: AlanCE on Oct 24, '02 11:51:55AM

maybe because many of us were born and raised on the numericals and they are second nature by now



[ Reply to This | # ]
Symbolic names vs numbers
Authored by: mithras on Oct 24, '02 12:49:38PM
Well, I wrote the hint (despite the random other name at the top?) and I also prefer the letter style.

But Rob changed a chmod a+x to a chmod 755 in the last hint I wrote, so I figure I'd go with the flow.

Maybe Rob can chime in with why prefers the numbers?

[ Reply to This | # ]
Symbolic names vs numbers
Authored by: mervTormel on Oct 24, '02 01:16:40PM

this is the second instance i've seen of a hint attributed to the wrong submitter.

rob, is there a bug in yer tools?



[ Reply to This | # ]
Names vs. numbers
Authored by: robg on Oct 24, '02 01:30:05PM

1) Hmm, I don't recall changing an "a+x" to a "755", but it's entirely possible. I'm not sure why I would have done this, however, so if I did, sorry! I don't personally prefer one method over the other. Mia culpa.

2) Argh ... very very sorry on the attribution. That's no bug in the software, it's a bug in the operator (me). I basically have to recreate every story, and I typoe'd the user ID. It's fixed now. The new Geeklog will solve this problem, as it doesn't insist on posting stories with the date of submission, which is how the current version works.

Sorry!

-rob.



[ Reply to This | # ]
Names vs. numbers
Authored by: mithras on Oct 24, '02 04:39:38PM

hey, no problem. thanks for the explanations. Certainly no offense was taken in either case - irrelevent glitches. Thanks so much for all your work.

be well,
mithras



[ Reply to This | # ]
Symbolic names vs numbers
Authored by: k2r on Oct 25, '02 10:03:47AM

I can of course just speak for myself, but I learned it this way
and I find it easyer to memorize a number for specific filetypes
aequivalent letters.

The numbers are very logical if you get used to thinking in bitsgrups.

And I always forget whether "o" means "other" or "owner" :-)

But you are right - if you teach some end-user about chmod using the
letter-switches is better to understand.
If you're programming on the other hand, bitgroups are more handy.

k2r



[ Reply to This | # ]
Seems to Work Beautifully
Authored by: dr_turgeon on Oct 24, '02 04:10:52PM
I've briefly tested both the drop-script and the shell script. I'm impressed :-) Thanks, Mithra!

[ Reply to This | # ]
Kudos
Authored by: ClarkGoble on Oct 24, '02 09:34:02PM

Many thanks to Mithras for his script. I've already made use of it extensively since I know little Applescript and was trying to do the same thing using only online documentation.

Paul McCann over on MacNN Forums suggested the following modifications to the script. (I'll snip the relevant portions of his post)

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



[ Reply to This | # ]
Kudos
Authored by: bluehz on Oct 25, '02 08:15:52AM

Script does work great! Thanks!

Not specific to this script - but does anyone know how to get rid of the msgs generated by OSAScript within the terminal. For example - in this script - run from the terminal you get:

## Component Manager: attempting to find symbols in a component alias of type (imco/SVQ3/SMI )
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
## Component Manager: attempting to find symbols in a component alias of type (imdc/MP42/MSFT)
ASW CFM Init; App: osascript
2002-10-25 08:13:57.370 osascript[12594] WARNING: _wrapRunLoopWithAutoreleasePoolHandler got kCFRunLoopExit, but there are no autorelease pools in the stack.
ASW CFM Terminate; App: osascript

Not that its a big deal - script still runs fine. Just an esoteric, anal thang....



[ Reply to This | # ]
Component Manager warnings
Authored by: hayne on Oct 25, '02 12:30:21PM

I think I recall that the Component Manager warnings are related to Toast.
Do a search on the forums or on google if needed and you'll see what I mean. I don't recall exactly but I think the fix was as simple as removing a cache file or something.



[ Reply to This | # ]
shifty
Authored by: mervTormel on Oct 25, '02 11:26:36AM

the shift is in the wrong place ?

should be after the last fi , methinks.



[ Reply to This | # ]
No way to get file type alias from the shell?
Authored by: klktrk on Mar 05, '05 05:59:25PM

Darn. I guess this hint tells me what I feared: Apparently there's no way to test a file to see if it's an alias from the shell if the Finder is not loaded. I'm looking for a way to test a file for being an aliases when in single user mode. I can test if a file is a link or not, but no way to see if it's an alias. Argh. It would be nice if there were something like get info for the shell. Like this:
getinfo filename

which would output everything HFS+ knows about the file in a parameter=value style listing.

Anyone know of such a tool?



[ Reply to This | # ]
No way to get file type alias from the shell?
Authored by: Anonymous on Mar 05, '05 09:11:29PM
Apple's developer tools includes a command line program called GetFileInfo. Its -a argument tells you if a file is an alias file or not.
/Developer/Tools/GetFileInfo -aA 'Some Alias File'
returns
1
if 'Some Alias File' identifies a file which is an alias file (and it'll return 0 otherwise).

[ Reply to This | # ]
Still works under Mavericks
Authored by: aaronrp on Feb 10, '14 04:47:26PM

This still works under Mavericks. It is especially useful now that the alias files are so very large.



[ Reply to This | # ]