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


Click here to return to the 'Automatically create app-launching aliases in bash' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically create app-launching aliases in bash
Authored by: koncept on Jul 12, '06 01:54:33PM
Made a few modifications to this (excellent) script:
- This script uses spotlight to find applications for those of us with spotlight functionality
- I didn't require separate files for each processed directory so I lumped everything into one sorted file
- I modified the alias name substitution so that created alias names would exclude characters outside the range [a-z]
# ==========================
# = .bash_profile contents =
# ==========================
declare -r	ALIASDIR=$HOME/.osxaliases
declare -xr	ALIASFILE="$ALIASDIR/appAliases"
declare -i	UPDATE=0

# Add directories to seach with spotlight here...
OSXDIRLIST=(/Applications /Developer/Applications $HOME/Applications)

mkdir -p "$ALIASDIR"

for dir in ${OSXDIRLIST[@]}; do
	if [ -d "$dir" -a "$ALIASFILE" -ot "$dir" ]; then
		UPDATE=1
		SPOTLIGHT_SEARCH="${SPOTLIGHT_SEARCH}--onlyin "$dir" "
	fi
done

if (($UPDATE)); then
	echo "Updating aliases..."
	mdfind 
		$SPOTLIGHT_SEARCH 
		"kMDItemContentType == 'com.apple.application-bundle'" 
	| sort | awk '{
		appl = $0;
		count = split(appl,path,//|.app/);
		name = path[count-1];
		name = tolower(name);
		gsub(/[^a-z]/, "", name);
		gsub(/'''/, "'''\''''", appl);
		gsub(/"/, "\"", appl);
		printf "alias %s="open -a %c%s%c"n",name,39,appl,39;
		}' >| "$ALIASFILE"
else
	echo "Application aliases are up to date."
fi

source $HOME/.bashrc
Now to get this working, I add one line to my .bashrc
# ==============================================
# = Create application aliases using spotlight =
# ==============================================
source "$ALIASFILE"


[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: koncept on Jul 12, '06 02:19:26PM

It looks like my line continuation characters and escapes have been eaten by this forum! How do I add them within code or pre-formatted blocks?



[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: koncept on Jul 12, '06 02:43:04PM
This should work now:
# ==========================
# = .bash_profile contents =
# ==========================
declare -r	ALIASDIR=$HOME/.osxaliases
declare -xr	ALIASFILE="$ALIASDIR/appAliases"
declare -i	UPDATE=0

# Add directories to seach with spotlight here...
OSXDIRLIST=(/Applications /Developer/Applications $HOME/Applications)

mkdir -p "$ALIASDIR"

for dir in ${OSXDIRLIST[@]}; do
	if [ -d "$dir" -a "$ALIASFILE" -ot "$dir" ]; then
		UPDATE=1
		SPOTLIGHT_SEARCH="${SPOTLIGHT_SEARCH}--onlyin \"$dir\" "
	fi
done

if (($UPDATE)); then
	echo "Updating aliases..."
	mdfind \
		$SPOTLIGHT_SEARCH \
		"kMDItemContentType == 'com.apple.application-bundle'" \
	| sort | awk '{\
		appl = $0;\
		count = split(appl,path,/\/|\.app/);\
		name = path[count-1];\
		name = tolower(name);\
		gsub(/[^a-z]/, "", name);\
		gsub(/'\''/, "'\''\\'\'\''", appl);\
		gsub(/\"/, "\\\"", appl);\
		printf "alias %s=\"open -a %c%s%c\"\n",name,39,appl,39;\
		}' >| "$ALIASFILE"
else
	echo "Application aliases are up to date."
fi

source $HOME/.bashrc


[ Reply to This | # ]
Automatically create app-launching aliases in bash
Authored by: porkchop_d_clown on Jul 12, '06 07:39:14PM
I like the idea of using mdfind; some of the other changes you made don't seem to be an improvement, though. The way you update SPOTLIGHT_SEARCH means that if one directory changes, only that directory will written to the new alias file - losing the aliases from the other directories.

Anyway, I took your version and combined it with mine. For example, the only reason I used multiple alias files was to reduce the time spent running find. Since that isn't needed with mdfind, we don't need an entire directory to hold the aliases, and since it serves no purpose, we don't need the sort. Actually, we also don't need a bunch of terminal backslashes either, since they are inside a quoted string.

Here's the newest version:

# -------------------------
# Build application aliases
# -------------------------
export ALIASFILE="$HOME/.osxaliases"
OSXDIRLIST=(/Applications /Developer/Applications $HOME/Applications)

UPDATE=0
for dir in ${OSXDIRLIST[@]}; do
    SPOTLIGHT_SEARCH="${SPOTLIGHT_SEARCH} --onlyin \"$dir\""
    if [[ -d "$dir" && "$ALIASFILE" -ot "$dir" ]]; then
        UPDATE=1
    fi
done

if (( $UPDATE )); then
    echo "Updating aliases..."
    mdfind \
        $SPOTLIGHT_SEARCH \
        "kMDItemContentType == 'com.apple.application-bundle'" \
        | awk\
            '{
                appl = $0;
                count = split(appl,path,/\/|\.app|\.dock/);
                name = path[count-1];
                name = tolower(name);
                gsub(/ |\(|\)|\"|'\\\''/, "", name);
                gsub(/'\''/, "'\''\\'\'\''", appl);
                gsub(/\"/, "\\\"", appl);
                printf "alias %s=\"open -a %c%s%c\"\n",name,39,appl,39;
            }' >| $ALIASFILE
else
    echo "Aliases are up to date."
fi

source $HOME/.bashrc

---
Everyone loves a clown, but no one will lend him money!

[ Reply to This | # ]

Automatically create app-launching aliases in bash
Authored by: koncept on Jul 13, '06 12:05:58AM

Nice stuff! :)



[ Reply to This | # ]