I’ve recently switched to development in two Vagrant virtual machines (VMs), one VM running Nginx and one running Apache. This means that are two locations I might need to open files from, with two sets of directories representing the document roots of my various development sites. Eventually I got bored of using the terminal to “subl ~/Vagrants/nginx/www/wordpress-whatever/” and decided to write an Alfred Workflow. Now I just trigger Alfred, hit “v client“, and Alfred shows me all my projects in directories with “client” in the name.
The core of the workflow is a script filter which contains the following Bashscript bound to the v keyword. I’ve commented the code fairly well, so hopefully it makes sense; or you can simply download the whole Workflow.
The structure of the workflow looks like this:
#!/bin/bash # We could do this in any script type, but I'm using Bash # My two Vagrant locations for sites are as follows # This code reads the immediate child directories into a Bash array NGINX_FILES=($(find /Users/simonwheatley/Vagrants/ubuntu-precise-32-nginx -iname *{query}* -type d -maxdepth 2)); APACHE_FILES=($(find /Users/simonwheatley/Vagrants/ubuntu-precise-32-apache -iname *{query}* -type d -maxdepth 2)); # Concatenate the Nginx and Apache arrays FILES=("${NGINX_FILES[@]}" "${APACHE_FILES[@]}"); # get length of an array LENGTH=${#FILES[@]}; # We have to return the results as XML in a certain format # http://www.alfredforum.com/topic/5-generating-feedback-in-workflows/ echo '<?xml version="1.0"?><items>'; # use for loop read all filenames for (( I=0; I<${LENGTH}; I++ )); do # Use Bash string manipulation to get the directory name # http://stackoverflow.com/a/2664746 DIR =${FILES[$I]##*/}; # Work out if we're Nginx or Apache, and set the icon appropriately # If seems that to detect a substring, we have to run it through Grep. Weird. if echo "${FILES[$I]}" | grep -q "apache"; then ICON="apache.png"; else ICON="nginx.png"; fi # Each selectable result is an item in the XML echo "<item uid='$I' arg='${FILES[$I]}' type='file'><title>${DIR}</title><subtitle>${FILES[$I]}</subtitle><icon>${ICON}</icon></item>"; done echo '</items>';
This feeds into a simple one line Bashscript:
subl {query}
Any suggestions or comments?
(Photo: Bowler hat lamps in the jazz corner of Cafe Belgique, © Hornbeam Wildlife Studio)
Leave a Reply