TextMate theme-switcher script

Summary: In which Martin ponders solutions on how to quickly switch themes in TextMate and comes up with a half-assed AppleScript / TM Command solution. Jump to the half-assed solution.

Edit: As these things usually go I had a bit of a revelation just minutes after posting this post. Completely by accident I typed the keystroke ⇧-⌃-⌥-T, which of course is mapped to the “Select Theme” – command in the Experimental Bundle and which I had completely and utterly purged from my mind. I’m pretty sure you want to use that solution rather than the one I outline in this post, but I’ll keep this around for posterity’s sake anyway. Original post follows.


I’m about to start teaching a 5 week class in ActionScript 3 and if there’s any possible way to avoid it, I won’t spend much time in the IDE code editor, because frankly; It sucks. So in order to make the differences between what the students will see on the screen (You guessed it. TextMate.) and what they’ll be working in themselves, at least initially, I played around with one of the themes in TextMate to make it look more like the Flash IDE editor.

From my beloved Twilight theme:
Tm Theme Twilight

To this IDE clone based on IDLE:
Tm Theme Idle

So since I’ll keep using my main theme for all my professional work I wanted a fast way of switching between the two themes (I’ll do a lot of work to save myself from dealing with a drop down menu). A bit of googling (actually a fair bit of googling) turned up this gem from a spanish speaking TextMate google group. Now, before you even start; Yes I feel slightly dirty about GUI-scripting as well. Making a script that simulates clicks rather than one that just tells the OS to perform whatever task associated with said click just feels wrong. But since not even a journeyman when it comes to the arcane arts of programming TextMate I’m just going to bite the bullet until someone (you?) comes up with a better solution.


So without further ado; Here’s the AppleScript solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
tell application "System Events"
  activate application "TextMate"
  keystroke "," using {command down}
  tell application "System Events"
    tell process "TextMate"
      click button "Fonts & Colors" of ¬
        tool bar 1 of window 11
      click pop up button 1 of window 1
      keystroke "Twilight"
      keystroke return
      keystroke "w" using {command down}
    end tell
  end tell
end tell

It’s dirty, will open and close the necessary windows in an epilepsy-inducing flash to do it’s thing, but it works. So what we’ve got here is a script that you could save and assign to a Quicksilver trigger to switch between themes. Bollocks to that! I can’t be having with one trigger for each theme I want to switch to. Let’s refine that puppy a bit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
tell me to activate
display dialog "What theme please?" buttons ¬
{"Twilight", "IDLE"} default button 1

set theTheme to the button returned of the result

if theTheme is "Twilight" or theTheme is "IDLE" then
  tell application "System Events"
    activate application "TextMate"
    keystroke "," using {command down}
    tell application "System Events"
      tell process "TextMate"
        click button "Fonts & Colors" ¬
        of tool bar 1 of window 1
        click pop up button 1 of window 1
        keystroke theTheme
        keystroke return
        keystroke "w" using {command down}
      end tell
    end tell
  end tell
end if

Ok. So now we have one script that’ll show a dialog box with a button for each of the themes you can choose between. A slightly better solution, but still not exactly what I’m looking for. There’s a couple of problems with this approach. Firstly, I don’t want to have to click a button! That’s what this whole thing started with. I want to keep my hands on the keyboard, and in as few strokes as possible have the theme switch. The dialog box from the above script does let me define one of the options as the default, but for some blasted reason I can’t get it to listen for key events to trigger those actions. Googling this puzzler only turned up some references to a mythical beast called Jon’s Commands, but these are terribly outdated and won’t even work on Intel Macs.

Enter XCode.
So this thing is growing way out of proportions. I just wanted to switch some goddamn themes quickly! But I’m not the kind of man to stop what I’m doing just because I realize I’m working with the wrong tools from the wrong approach. No sir.
Banging my head against my now ill-abused cinema display figuring out how the Interface Builder works I manage to somehow construct a miniature app that will show a prettier version of that same dialog but also accept keyboard shortcuts for the different options (keys 1 through 9 specifically) , and will shut itself down after you’ve made a choice. So now I’ve gone from one unsatisfactory solution to another. I don’t want to activate Quicksilver and start an app every time I want to switch themes! Ideally I want this functionality to work with a tab trigger from within TextMate. Luckily TextMate can launch apps via AppleScript.

Theme SwitcherThe finished solution can be downloaded here. Put the ThemeSwitcher application in your Applications folder, and doubleclick the Switch Themes.tmCommand file to install the TextMate tab trigger to launch the app. It’s set to trigger on the keyword “themes” but you can change that easily in the Bundle Editor.

To sum up what’s going on here. You’ve got an application in your Applications directory called “ThemeSwitcher”. From within TextMate you call a tab trigger command that runs a miniscule AppleScript to open said application. When you make a choice in the ThemeSwitcher app it performs the necessary GUI-Scripting to switch your themes and shuts itself down.

As I’ve mentioned this is not a very elegant solution. I don’t care much for GUI-Scripting, and I’m sure this could even somehow be done purely with TextMate commands if someone more capable than me set their mind to it. If you do just that; Please leave your solution in a comment as I’d happily link it more prominently at the top of this post.

Quick list for the lazy ones that skipped here from the top.
 

  • 1. Download this file and unzip it.
  • 2. Drag the “ThemeSwitcher” application to your Applications folder.
  • 3. Doubleclick the “Switch Themes.tmCommand” file to install the TextMate tab trigger.
  • 4. Type “themes” followed by the tab key (⇥) to activate the Theme Switcher.

And of course; Full source here.

Possibly related posts:

  1. OmniFocus clippings from Firefox Summary: In which Martin has created an AppleScript to remedy...
  2. Better (AS3) imports in TextMate Summary: In which Martin has concocted a hack to make...
  3. Some Path Finder AppleScripts How’s that for a TitleCased title? So I’ve been checking...
  4. Show / Hide Window I’ve been using Yojimbo, for a long while now but...
  5. The Parallax WordPress theme open sourced I’ve been meaning to release the theme of this blog...

One Comment

  1. Posted September 11, 2008 at 11:15 | Link

    Thank you very much for this excellent article and the source code to the ThemeSwitcher application.

    Keep up the good work.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*