Home > Uncategorized > AppleScripts for the Keyboard Lover

AppleScripts for the Keyboard Lover

April 5th, 2009

Increasingly, I love that OS X seems to be designed to want to do what I tell it to. Don’t let me get started on the wonders of the mouse (two-finger scrolling, three-finger swiping, shrink/enlarge, screen zoom, and rotate). But in interfacing with this oh so lovely OS, there’s one thing that keeps coming back to annoy me: keyboard shortcuts.

Largely, I’ve learned those I find most useful. Minimize. Hide. Hide All. Preview. Open. Switch application (forwards and backwards). Application specific shortcuts. But always, several shortcuts were missing. Specifically, those to…

  • maximize/restore the current window
  • label files with specific colors
  • show all windows

Then AppleScript and Butler came into my life…

First, I installed the Butler tool to hook keyboard shortcuts into AppleScripts. Next, under the Hidden section of Butler’s configuration page, I created several scripts, one for each function.

Butler Configuration Page

Butler Configuration Page

  • Maximize Frontmost Window
  • Finder Labels (Container of 9 colors)
  • Show All

This is the script for Maximize Frontmost Window

tell application “System Events”
   if UI elements enabled then
      set FrontApplication to (get name of every process whose frontmost is true) as string
      tell process FrontApplication
         click button 2 of window 1
         –button 2 is the green “zoom” button for all applications
         –window 1 is always the frontmost window.
      end tell
   else
      tell application “System Preferences”
         activate
         set current pane to pane “com.apple.preference.universalaccess”
         display dialog “UI element scripting is not enabled. Check ‘Enable access for assistive devices’”
      end tell
   end if
end tell

This is a sample of the first color script (None).

property file_color : 0 –none

tell application “Finder”
   activate
   set selected to selection
   repeat with n_file in every item in selected
      set label index of n_file to file_color
   end repeat
end tell

For the other color scripts, alter the value of file_color. The file_color value / color associations are: 0/None, 2/Red, 1/Orange, 3/Yellow, 6/Green, 4/Blue, 5/Purple, 7/Gray. Note that Finder lists the colors in a totally different order (1/Red, 2/Orange, …). This affects the triggers that you will associate with these scripts.

This is the Show All script:

tell application “System Events”
   set visible of every application process whose visible is false and background only is false to true
end tell

Last, I asked Butler to associate particular triggers with these AppleScripts.

  • ^M for maximize/restore
  • Cmd+Opt+L to list the color scripts (associate this command with the container in Butler that has all the scripts beneath it)
  • Cmd+Opt+X with None for file coloration
  • Cmd+Opt+<n> with file color <n>
  • Cmd+Opt+Shift+H with Show All

And that’s a wrap!

Share and Enjoy:
  • Facebook
  • E-mail this story to a friend!
  • Reddit
  • Digg
  • del.icio.us

dulles ,

  1. April 6th, 2009 at 11:59 | #1

    Butler + AppleScript looks like a powerful method of doing things — I’ve been told to look into AppleScript before. Supposedly it has great Python bindings. :-)

    Nitpick: I think hardcore Mac users get all frowny-face when you say ‘maximize’: it’s supposed to be something like ‘zoom’ and ‘unzoom,’ which is different in that a window only fills the maximum amount of space that it considers useful. *shrug*

  1. No trackbacks yet.
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States