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


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