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


Click here to return to the 'A cleaner and safer alternative' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
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 | # ]