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

A script to reveal alias paths in the Terminal UNIX
Here is a script that returns the original file referenced by a Mac OS GUI alias file. Enter the following script into a UNIX file named apath (or whatever you want to call it), and make it executable by typing chmod 755 apath. Make sure the file is in a directory in your command path. If you execute the command apath alias, where alias the a Mac OS alias file, the path to the original file is returned.

Now go to a directory that has an alias to another directory and enter the command cd `apath aliasd`, where 'aliasd' is the name of the alias to the directory. Your working directory changes to the original directory. You can use the same trick to execute any command on the alias. Be careful - you may have to include double quotes to get around spaces in the file name, as in cd "`apath Directory With Spaces`".

Please note that I borrowed heavily from this hint.
#!/bin/sh
if [ $# -eq 0 ]; then
echo ""
echo "Usage: apath alias"
echo " where alias is an alias file."
echo " Returns the file path to the original file referenced by a"
echo " Mac OS X GUI alias. Use it to execute commands on the"
echo " referenced file. For example, if aliasd is an alias of"
echo " a directory, entering"
echo ' % cd `apath aliasd`'
echo " at the command line prompt would change the working directory"
echo " to the original directory."
echo ""
fi
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'
orig=`osascript -e "$line_1" -e "$line_2" -e "$line_3" -e "$line_4" -e "$line_5" -e "$line_6"`
echo "$orig"
fi
[Editor's note: I tested this script, and it does exactly what it claims to do.]
    •    
  • Currently 3.60 / 5
  You rated: 3 / 5 (5 votes cast)
 
[13,583 views]  

A script to reveal alias paths in the Terminal | 21 comments | Create New Account
Click here to return to the 'A script to reveal alias paths in the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
#092; is a backslash
Authored by: mstoops on Dec 18, '02 12:56:55PM
It took me a minute to figure why when I pasted this into an editor, the script wouldn't run. On the line


item_parent="`cd #092;"${item_parent}#092;" 2>/dev/null && pwd || echo #092;"${item_parent}#092;"`"


substitute the #092; with a backslash (\). There should be four substitutions:


item_parent="`cd \"${item_parent}\" 2>/dev/null && pwd || echo \"${item_parent}\"`"


Hope this helps.


[ Reply to This | # ]
#092; is a backslash
Authored by: googoo on Dec 18, '02 01:27:03PM

You are exactly correct. I thought I submitted the original (text file) posting with backslashes. Maybe something got lost in the translation to HTML.

-Mark



[ Reply to This | # ]
no dice for me
Authored by: uurf on Dec 18, '02 01:32:04PM
there is an alias to the program iPulse on my desktop. I get the following errors:
[local:~/Desktop] username% apath iPulse
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
execution error: Can't make file "Macintosh HD:iPulse" into a alias. (-1700)


[ Reply to This | # ]
fixed
Authored by: uurf on Dec 18, '02 01:50:52PM

#092; -> \ worked.



[ Reply to This | # ]
still no love for directories though
Authored by: uurf on Dec 18, '02 01:56:01PM
I have aliases of Home and Documents in my Favorites folder.
[local:~/Library/Favorites] username% apath Documents/
[local:~/Library/Favorites] username% apath Documents
[local:~/Library/Favorites] username% apath Home
[local:~/Library/Favorites] username%
as you can see the directory never changes

[ Reply to This | # ]
oops RTM, stil;l doesn't work on directories
Authored by: uurf on Dec 18, '02 02:03:12PM
[local:~/Library/Favorites] username% cd 'apath Home'
apath Home: No such file or directory.
[local:~/Library/Favorites] username% cd "apath Home"
apath Home: No such file or directory.
[local:~/Library/Favorites] username% ls
ALEPH                     Documents                 Home
Connect to Server 2...                        Icon?
Connect to Server...             
[local:~/Library/Favorites] username% cd "'apath Home/'"
'apath Home/': No such file or directory.
[local:~/Library/Favorites] username% cd "'apath Home'"
'apath Home': No such file or directory.
[local:~/Library/Favorites] username% 


[ Reply to This | # ]
oops RTM, stil;l doesn't work on directories
Authored by: JT on Dec 18, '02 03:06:55PM

dont use quotes. use those back tick thinggies '`'.



[ Reply to This | # ]
My fault!!!
Authored by: robg on Dec 18, '02 01:52:29PM

The script was right as submitted. When I edited it this morning, though, I had to recreate the backlsashes ... and I left the ampersand out of the backslash entity (#092;), so that's what you all saw.

It's fixed now, and I apologize for the troubles...

-rob.



[ Reply to This | # ]
Simpler Script
Authored by: signals on Dec 18, '02 03:31:07PM

I took your script and modified it a little to do less work. It no longer uses multiple calls to "echo" to display text, it uses a single call to "cat." Most of the variable assignments have been removed since many were only used once, and the AppleScript is cat'ed into osascript now instead of multiple "-e" arguments. There seemed to be no reason to assign the output of osascript to a variable and then echo it, so I removed that and I've redirected stderr to /dev/null so I don't get the

## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)

message. And I also removed the "|| echo \"${item_parent}\"" from the 'determine absolute path' part, as it didn't seem necessary. I can't think of a way the OR could be executed and still have the script work. I also used "$0" in the usage display so you can call the script whatever you want without having to edit it so the usage: display says the right name.

Someone please fix anything I might have broken.


Here it is:

----Begin Script----

#!/bin/sh

# Usage Display
if [ $# -eq 0 ]; then
cat <<EOF
Usage: ${0} alias
where alias is an alias file.
Returns the file path to the original file referenced by a
Mac OS X GUI alias. Use it to execute commands on the
referenced file. For example, if aliasd is an alias of
a directory, entering
% cd \`${0} alias\`
at the command line prompt would change the working directory
to the original directory.

EOF
fi

# Main routine
# If it's a file and not a link, we continue
if [ -f "$1" -a ! -L "$1" ]; then
# Find the absolute path to the file
item_parent=`dirname "$1"`
item_path="`cd "${item_parent}" 2>/dev/null && pwd`/`basename "$1"`"

# Run the AppleScript to decode the alias.
cat <<EOF | osascript 2>/dev/null
tell application "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
EOF
fi

----End Script----


-Signals



[ Reply to This | # ]
Simpler Script
Authored by: signals on Dec 18, '02 03:33:07PM

Oops, sorry... I lost the indenting when I pasted the script into the web browser. It should still work fine, it just looks funny.

-Signals



[ Reply to This | # ]
A cleaner and safer alternative
Authored by: _merlin on Dec 19, '02 08:07:16AM
The original script uses far too many variables, and more commands than it should. It is also unreliable. The AppleScript needs more parentheses. The cleaned up version in the comments isn't much better; it still fails most of the time. Some of the problems with the alias path scripts are:
  • osascript honours the working directory, so the dirname/pwd/basename thing is not needed.
  • Sending the standard error output to the alligator in /dev/null is not helpful. Most people like to know why their script failed.
  • Redirecting the input to osascript through a whole lot of variables, or through the cat command is unnecessary and confusing.
  • AppleScript performs the "as" operator before the "of" operator, causing the script to fail.
  • The scripts continue to check whether the first argument is a file, even if it doesn't exist.
Here's a cleaned up version that should work in all situations:
#!/bin/sh

# Usage display
if [ $# -eq 0 ]; then
cat << EOF
Usage: ${0} alias
where alias is an alias file.
Returns the file path to the original file referenced by a
Mac OS X GUI alias. Use it to execute commands on the
referenced file. For example, if aliasd is an alias of
a directory, entering
% cd \`${0} alias\`
at the command line prompt would change the working directory
to the original directory.
EOF

# Main routine
# If it's a file and not a link, we continue
elif [ -f "$1" -a ! -L "$1" ]; then
# Run the AppleScript to decode the alias.
osascript << EOF
tell application "Finder"
set theItem to (POSIX file "${1}") 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
EOF
fi
[Editor's note: _merlin, aka Vasantha Crabb, has written a number of AppleScripts for the site and knows this stuff much better than I do! Thanks for providing a cleaned up version of the script!]

[ Reply to This | # ]
A cleaner and safer alternative
Authored by: signals on Dec 19, '02 08:52:39AM

[linktest]$ ./apath2 ./linktestalias
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
/Users/mccarthy/ntp.conf
[linktest]$


I still get that strange message without the "2> /dev/null" on the call to osascript. I wonder why I couldn't get the "osascript <<EOF" to work when I was playing with it? It would complain about a syntax error in the AppleScript, the only way I got it to work was to switch to a "cat <<EOF | osascript." Oh well, this one works...

So where would you suggest someone learn AppleScript? This is the first Mac I've ever had to work with, so I know almost nothing about AppleScript, except that it looks even wordier than COBOL.

-Signals



[ Reply to This | # ]
A cleaner and safer alternative
Authored by: _merlin on Dec 19, '02 05:23:50PM

I don't know why you're getting that message, I haven't got anything like it in my tests. It won't affect things like "cd `dirname `apath an_alias``", because only the standard output will be fed through the commands, the error output will just be printed on the terminal.

When you used "osascript << EOF", did you remember to place "EOF" on a line by itself with no spaces (including no indentation)? Any spaces before or after "EOF" will break it.



[ Reply to This | # ]
A cleaner and safer alternative
Authored by: pmccann on Dec 19, '02 09:17:20PM

If I remember rightly this is caused by a Toast titanium file that's put in the quicktime folder... hmm,
here we go...

Go into /Library/QuickTime and move 'Toast Video CD Support.qtx' somewhere else (err, maybe the Trash!!).

Cheers,
Paul



[ Reply to This | # ]
A cleaner and safer alternative
Authored by: signals on Dec 20, '02 08:13:04AM

> Go into /Library/QuickTime and move 'Toast Video CD Support.qtx' somewhere else (err, maybe the Trash!!).

That seems to have fixed it. Thanks.

-Signals



[ Reply to This | # ]
A cleaner and safer alternative
Authored by: googoo on Dec 19, '02 09:35:11AM

Thanks to everyone for cleaning up my script! I just modified another script to get the result I was after, but you have shown me a more efficient way to do it.

-Mark



[ Reply to This | # ]
A cleaner and safer alternative
Authored by: englabenny on Dec 19, '02 01:24:12PM

It's smooth!
really interesting script, funny when sb sorted out all the sh-scripts, applescript was the solution. :D

only downside is applescript requires finder, and starts it. :(



[ Reply to This | # ]
A cleaner and safer alternative
Authored by: jimTucson on Sep 28, '05 10:01:42AM

Nice solution, but it does require running Finder. Is there any way to access this information via python (or perl, or whatever) and direct system calls rather than by invoking Finder? Having to run a GUI-based app to get information used in a command-line script seems inelegant at best. If you can get at this information via a C program it should be available via something like python (if someone has written the necessary code).



[ Reply to This | # ]
A cleaner and safer alternative
Authored by: lar3ry on Dec 01, '06 01:00:01PM
I was thinking the same thing, actually, when I first started using Tiger.

The closest command line tool that can help is Tiger's mdls command, which uses Spotlight's database.

Unfortunately, while mdls can show you that the file is an alias (kMDItemContentType = "com.apple.alias-file" or kMDItemKind = "Alias"), Spotlight's database doesn't seem to have the location where the alias points to, which seems to me to be a weakness in Spotlight.

I still have the remnants of the perl script that I started writing back then to do what this particular script does. I'm not happy having to run oascript to get the reference to an alias...

(By the way, the Spotlight attributes can be used in the shell script above to verify that the file passed to apath is indeed an alias.)

—lar3ry

[ Reply to This | # ]

Kick A**
Authored by: GaelicWizard on Dec 23, '02 06:51:26PM

This is a very kool script. I'm using it as part of another script so that calls on the commandline to use an alias as a symlink work (better sometimes than never :))!

JP



[ Reply to This | # ]
successful bash alias of this command
Authored by: GlowingApple on Mar 23, '05 11:22:59PM
Old hint I know, but I finally figured out that if I set a bash alias:
alias cdapath='cd < /bin/apath'
then I can type cdapath alias in the terminal and it displays the dir of the alias and cd's to it. Simple idea, but it took me a while to try to figure out how to do it since there's no way to alias
cd `apath dir`
as far as I know. Then, I was attempting to cd within the script (and obviously failing) and finally came upon this alias. So I thought I'd share it in case anyone else is in the same predicament.

---
Jayson

When Microsoft asks you, "Where do you want to go today?" tell them "Apple."

[ Reply to This | # ]