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


Click here to return to the 'Enable 'cd' into directory aliases from the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Enable 'cd' into directory aliases from the Terminal
Authored by: klktrk on Dec 01, '06 10:56:52AM

This is a great hint. Thanks for this.

I wanted to modify this so i could run it on any mac i encounter whether I have been able to install the c-code above or not, so I decided to use a related hint* to replace the getTruePath portion of the code.


# teach shell to treat aliases like symbolic links rather than files
function cd {
	if [ ${#1} == 0 ]; then
		builtin cd
	elif [[ -d "${1}" || -L "${1}" ]]; then	# regular link or directory
		builtin cd "${1}"
	elif [ -f "${1}" ]; then	# file: is it an alias?
		# Redirect stderr to dev null to suppress OSA environment errors
		exec 6>&2 # Link file descriptor 6 with stderr so we can restore stderr later
		exec 2>/dev/null # stderr replaced by /dev/null
		path=$(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
)
		exec 2>&6 6>&-      # Restore stderr and close file descriptor #6.
		
		if [ "$path" == '' ];then # probably not an alias, but regular file
			builtin cd "${1}"	# will trigger regular shell error about cd to regular file
		else	# is alias, so use the returned path of the alias
			builtin cd "$path"
		fi
	else	# should never get here, but just in case.
		builtin cd "${1}"
	fi
}

I put this in my .bash_profile, which I have checked in to a CVS repository. Then, from any Mac that I am working on, I simply check out my bash environment and either source it or overwrite the current .bash_profile. This gives me instant access from anywhere to all the modification I make to my bash environment.

Thanks to the original authors and developers of the hint(s) this is based on. I hope someone finds this useful.

(Unfortunately, using it at first was triggering these messages:
CFLog (21): dyld returns 2 when trying to load /Library/ScriptingAdditions/QXPScriptingAdditions.osax/Contents/MacOS/QXPScriptingAdditions
so that's why I had to supress the errors.)

____________________
* A script to reveal alias paths in the Terminal



[ Reply to This | # ]