Streamlining Your Workflow with AppleScript

blue sportscarWhen developing web applications, I frequently switch between iTerm, TextMate, my browser, and my mail client. So I used AppleScript to streamline my task-switching. I’ll show you how I did this in case you’re inspired to streamline your workflow, too.

I hooked up an AppleScript to each of my function keys. First, I had to reclaim my function keys. Then I programmed each function key like so:

F1: Cycle Among Active Windows

I set this up by:

  • opening “System Preferences”
  • choosing “Keyboard & Mouse”
  • choosing “Keyboard Shortcuts”
  • assigning the F1 key to “Move focus to next window in active application”

F2: Firefox

Whenever I hit F2, all of my open windows vanish and all of my Firefox windows appear.

Here’s the AppleScript:

tell application "Finder"
  activate
  set visible of every process whose visible is true and name is not "Finder" to false
end tell
tell application "Firefox" to activate

I created a directory specifically for these AppleScripts and I named this AppleScript ShowFirefox.scpt. To hook this script up to the F2 key, I set up a QuickSilver Trigger:

  • Choose “Catalog…” from the QuickSilver menu.
  • Select “Custom” from the left pane.
  • Choose the “+” button at the bottom of the dialog and then select “File & Folder Scanner”.
  • Select the directory that you use to store your AppleScripts.
  • In the right drawer that slides out, choose “Folder Contents” from the “Include Contents” drop-down.
  • Choose the appropriate depth (the number of subdirectories to scan).
  • With your AppleScripts directory still selected, click the circular arrow button on the bottom of the dialog (the tooltip says “Rescan source”).
  • Now that QuickSilver knows about your AppleScripts, choose the “Triggers” tab at the very top of the dialog.
  • Choose the “+” button at the bottom of the dialog and then select “HotKey”.
  • Type the name of your AppleScript (in this case, ShowFirefox.scpt).
  • After selecting your AppleScript, make sure the Action says “Run” and then press Enter.
  • The trigger has been created and it highlighted. In the right column labeled “Trigger” your entry should say “None”. Click on “None”.
  • Press F2. That will bind the F2 key to this AppleScript.

F3: TextMate

I did the same exact thing for TextMate. Here is the AppleScript:

tell application "Finder"
  activate
  set visible of every process whose visible is true and name is not "Finder" to false
end tell
tell application "TextMate" to activate

F4: iTerm

I used the same strategy for iTerm.

F5: Apple Mail

I used the same strategy for Apple Mail. I also use the very cool Mail Unread Menu to display in the menu bar the number of unread mail messages.

F6: Preview

I used the same strategy for Preview. This is great for when I occasionally need to access a PDF while programming.

F7: Timesheet

I use TextMate to keep track of my time. Thanks to this AppleScript, my timesheet is one keypress away:

set theProjectname to ((path to home folder as text) & "Dropbox:Personal:InformationManagement:Projects:timesheets.tmproj")
 
tell application "TextMate"
  activate
  open theProjectname
  tell application "System Events" to keystroke "t" using {command down}
  tell application "System Events" to keystroke "thisweek"
  tell application "System Events" to keystroke return
end tell

The AppleScript is opening my TextMate timesheets project and opening the ThisWeek.time.txt file.

F8: Chrome

Sorry Firefox, it’s been real fun, but Chrome is my main man now. I use Chrome for browsing (because it’s fast) and keep Firefox around for web development (because of Firebug).

F9: To Do List

I use TextMate for my personal to-do list. It’s nice to have my daily task list one keypress away:

set theProjectname to ((path to home folder as text) & "Dropbox:Personal:InformationManagement:Projects:todo.tmproj")
 
tell application "TextMate"
  activate
  open theProjectname
  do shell script "/bin/sleep 0.1"
  tell application "System Events" to keystroke "t" using {command down}
  tell application "System Events" to keystroke "today"
  tell application "System Events" to keystroke return
end tell

Note that I insert a little delay in the AppleScript to make it work. I keystroke “today” because my to-do list is in a file called Today.list.txt.

F10: Jot

I created my own “Jot” microapp using AppleScript and Ruby. I press F10, write down my thought in the dialog box that pops up, then continue what I was doing before so that I don’t lose my train of thought. Then, at the end of the day, I open up jot.txt and sort through my notes.

Here’s the AppleScript:

on replace_chars(this_text, search_string, replacement_string)
  set AppleScript's text item delimiters to the search_string
  set the item_list to every text item of this_text
  set AppleScript's text item delimiters to the replacement_string
  set this_text to the item_list as string
  set AppleScript's text item delimiters to ""
  return this_text
end replace_chars
 
tell me to activate
set question to display dialog ("Jot:") default answer ""
set answer to text returned of question
set answer to replace_chars(answer, "'", "")
do shell script "ruby /Users/wyattgreene/Dropbox/Personal/InformationManagement/ToDo/Scripts/jotter.rb '" & answer & "'"

Note that I stripped out any apostrophes. And here’s the Ruby script:

require File.join(File.dirname(__FILE__), 'Library', 'config')
 
if command = ARGV[0]
  filename = "#{Config::PATH}/Jot.txt"
  File.open(filename, "a") do |file|
    file.puts ARGV[0]
  end
end

You’ll have to tweak the Ruby script to use it, but you get the idea.

F11: Reserved

I reserve F11 as a special TextMate key.

F12: Show Desktop

This gets me back to a blank desktop:

tell application "Finder"
  activate
  set visible of every process whose visible is true and name is not "Finder" to false
end tell
tell application "Finder"
  set theWindows to (get every Finder window)
  repeat with eachWindow in theWindows
    close eachWindow
  end repeat
end tell

Option+F1: Path Finder

And finally, I’ve hooked up the great Path Finder application to Option+F1. It’s like Apple’s Finder, only much better.

You can skip to the end and leave a response. Pinging is currently not allowed.

One Response to “Streamlining Your Workflow with AppleScript”

  1. Dan Pickett says:

    Hey Wyatt,

    This is pretty cool – I’ve played with AppleScript a bit.

    You might want to check out QuickSilver triggers – I use them frequently to switch windows. I also utilize spaces pretty extensively to separate Communication applications vs. Dev Applications vs. Time Wasters etc.

Leave a Reply