Alfred: Folders & Projects → Sublime, Xcode, IDEA

I often find myself following a pattern: open Terminal, cd to a folder, then use the subl or open command to open the folder in Sublime or Xcode.

I figured that I must be able to use Alfred to shortcut this particular workflow. Low and behold, it’s pretty simple. I thought I’d need to write some code, but Alfred’s Workflow feature has everything you need built-in.

This does mean that, before starting, you need to buy Alfred’s Powerpack. This allows you to use Workflows, which are needed for this shortcut.

Open folder in Sublime

  1. First create a new workflow. To do this, open Alfred’s preferences and go to the Workflow tab. From the lower-left, click +, Templates, Files and Apps, File filter from keyword and open. Fill in the info. Alfred’s workspace will appear with a workflow consisting of two boxes connected together: File Filter and Open File.
  2. Double-click the File Filter workflow item. Enter the keyword subl and leave the with space field checked. Add folder as Placeholder title. Drag a folder from Finder into the File types box. Save.
  3. Double-click the Open File item. Drag Sublime Text into the box on the right. Save.

You can now type subl folder-name to open the folder in Sublime.

Open project or workspace in Xcode

It’s very similar.

  1. First create the new workflow by opening Alfred’s preferences and going to the Workflow tab. From the lower-left, click +, Templates, Files and Apps, File filter from keyword and open. Fill in the info. Alfred’s workspace will appear with a workflow consisting of two boxes connected together: File Filter and Open File.
  2. Double-click the File Filter workflow item. Enter the keyword xcode and leave the with space field checked. Add folder as Placeholder title. Open a folder containing an Xcode workspace and project in finder and drag and drop the project and workspace into the File types box.
  3. Double-click the Open File item. Drag Xcode into the box on the right. Save.

You can now type xcode project-name to open a project or workspace in Xcode.

Open project in IDEA

IDEA is more complicated, you need to use a slightly modified version of this bash script. The bash script takes a directory and pokes around inside for an IDEA project to open. We can adapt this to take the output of a File Filter and push it into the modified script.

  1. Follow steps (1) and (2) from the Sublime recipe. Change the keyword to idea but everything else is the same – we want to get a folder out of the File Filter.
  2. Delete the Open File workflow item by right-clicking it. Use the + in the top right of the workflow workspace to add a script action, Actions, Run script.
  3. The Language is /bin/bash, run sequentially is fine. Uncheck all the boxes in Escaping.
  4. Enter the following as the script itself:

     #!/bin/sh
     # check for where the latest version of IDEA is installed
     IDEA=`ls -1d /Applications/IntelliJ\ * | tail -n1`
     # were we given a directory?
     if [ -d "{query}" ]; then
         wd=`ls -1d "{query}" | head -n1`
         # let's check for stuff in our working directory.
         pushd $wd > /dev/null
         # does our working dir have an .idea directory?
         if [ -d ".idea" ]; then
           open -a "$IDEA" .
         # is there an IDEA project file?
         elif [ -f *.ipr ]; then
           open -a "$IDEA" `ls -1d *.ipr | head -n1`
         # Is there a pom.xml?
         elif [ -f pom.xml ]; then
           open -a "$IDEA" "pom.xml"
         # can't do anything smart; just open IDEA
         else
           open "$IDEA"
         fi
         popd > /dev/null
     fi
    

Clearly you can create about a zillion more things following the same pattern for whatever your editors of choice are.

.:.