Better (AS3) imports in TextMate

1/12/2008
Summary: In which Martin has concocted a hack to make importing packages in TextMate a wee bit easier.

TextMate TextMate TextMate. I might as well make this a pure TextMate blog soon, but I can’t help it. My love for this app is overwhelming.

I spend a lot of my time writing ActionScript, and one thing I’ve always envied users of Flex Builder (Which I avoid if I can) and FlashDevelop (which isn’t available on the Mac) is the auto-importing of classes, I.E. when you type

1
var s:Sprite;

the IDE checks whether that class is already imported, and if not imports it.

Now, what I’ve managed to do still isn’t even close to that functionality and let me say right away that the meat of this trick is the excellent work by the creator of the ActionScript 3 bundle for TextMate; Simon Gregory et al.

Simon has in his bundle included a ‘Auto complete imports’ command which searches both the packages in your project and the packages included from Adobe (flash.*) which makes it easy to get completion for any class you’d want to import.

Like so:

  • You want to import the
    1
    flash.net.URLLoaderDataFormat;

    class, which is a long-ass line to type out.

  • Instead you type ‘URL’ and trigger the auto-complete command. You’ll get a drop-down with all classes starting with “URL” and then choose the proper one.
  • 1
    import flash.net.URLLoaderDataFormat;

    is typed out for you. Yay!

However, now you have to copy that text, move on up to your import statements, paste it and then find your way back to where you were.

Boy do I wish I knew Ruby well enough to actually just augment that command to make it obey my wishes, but alas; My ruby-Fu is weak and so I turned to the weapon of the feeble TextMate hacker; Macros.

Basically the workflow now is:

  • Perform the steps as mentioned above to get the completion for your import. However; This time the output would be
    1
    import flash.net.URLLoaderDataFormat; move

    . The ‘move’ at the end there will work as a tab trigger for the macro, so the next step is to press tab, and then (hopefully) the import statement is moved up to it’s brethren, and you end up back where you were. Here’s how it works.

  • When you press tab you trigger the macro which will perform the following steps:
  • Select the line with the import and cut it.
  • Enter the word ‘asdf’ in place of the import statement.
  • Search for the last occurrence of the word ‘import’ in the document.
  • Move to the line below said last import.
  • Paste the import statement from the clipboard and indent it properly.
  • Search for ‘asdf’ in the document and delete it, leaving the caret at the position it was when you launched the macro.

Like I said; It’s more of a hack than anything else and if someone comes up with a proper solution please let me know. The main weaknesses here are:

  • You can’t just hit the combo in the process of typing out a statement. If you trigger the auto completion at the word ‘Sprite’ in
    1
    var s:Sprite = new Sprite();

    The line

    1
    import flash.display.Sprite;

    will end up in the middle of the statement. Likewise if you trigger the combo on the same line as any other code, that code will end up with the imports. The command must be triggered on a line of it’s own.

  • Dependencies: If you want to change the tab trigger to something else than ‘move’ you’ll have to change the output from the auto-complete as well.
  • Not really all that elegant. Having to do two actions (even though one is just pressing tab) seems a hassle.

With all that out of the way: Grab it if you want it!
Zip here.
Please note: I didn’t really want to distribute a modified version of Simons work, so if you want to output the ‘move’ keyword for the tab trigger you need to go into the ‘Auto Complete Imports’ command of the ActionScript 3 bundle in the Bundle Editor and make a miniscule edit at the bottom of the code.

1
2
3
4
5
6
7
8
#Change
else
  print "import " + choice + ";"
end
#to
else
  print "import " + choice + "; move"
end

That’s all folks.

2 Comments

Show / Hide Window

6/08/2008

I’ve been using Yojimbo, for a long while now but Eystein has been pestering me to give Evernote a shot. Common for both of these is that you don’t want to spend any time when you’re dropping stuff into them. I want one key that’ll toggle the visibility of the app. I want to hit my hotkey, dump stuff in, hit my hotkey again and continue what I was doing. Of course, neither of these apps come with that functionality (Well. Yojimbo does come with a “Quick input”-window, but for some reason I never got to loving it.) so here’s an AppleScript to fix that.

Just change the name in the script to whatever app you want to target, create a Quicksilver trigger for it and Bob’s your uncle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
on run
  tell application "Finder" to set en to name of first ¬
                        process whose frontmost is true
  if en = "Evernote" then
    tell application "System Events" to set visible ¬
                of process "Evernote" to false
    set notHidden to false
  else
    tell application "System Events" to set visible ¬
                of process "Evernote" to true
    activate application "Evernote"
    set notHidden to true
  end if
end run

I’m really enjoying messing around with AppleScript these last few days. For such a light-weight language the benefits of it commanding the OS are huge.

No Comments

Fix top level domain list in Mobile Safari

2/08/2008

Keyboard wtfI love my (1.gen thoroughly hax0red) iPhone and one of the nice things I discovered a while ago (I can’t remember if this is a firmware 2.0 only trick so I can’t say exactly when) is this little trick. When you’re typing a URL in Safari you can hold down the .com key and get a few more choices (.org, .net, .edu), I thought that was pretty cool and attributed the strange choice of “.edu” over say “.co.uk” to a US-centric perspective from the developers.

Before firmware 2.0 I had the dictionary on my phone turned off because it just interfered with my typing, but now that switching between English and Norwegian dictionaries is just a tap away I’ve really come to like it. I was also pleased to see that when I activated the Norwegian keyboard “.no” was now added to the list of domains, and baffled to see that “.net” had been removed while “.edu” was stil there…

Seriously. I can’t remember once typing a .edu URL into a browser. Seeing that I’m on my “Fix everything that bothers me” crusade nowadays I traced down the offending .plist on the phone and fix0red it good. Here’s a two-step guide to get this fixed. You will of course need a Jailbroken iPhone you can SSH into. If that means nothing to you you’ll have to go do some searches on “JailBreak iPhone” and “PwnageTool” first. Come back when you’re ready.

Path to Language FileSSH into your iPhone and navigate to

System/Library/Frameworks/UIKit.framework

Find the file corresponding to your language. Norwegian would be “Keyboard-nb.plist” (Norsk Bokmål. Hence the “nb” instead of “no” folks).

Open this file in a plist-editor, such as “Property List Editor” which you’ll have if you installed the developer tools with OS X. If not just google it and get an app that’ll do the job. Scroll to the line that says UI-TopLevelDomain and expand it. You’ll have to change the values in two places as pictured below.
Plist

That’s it. Save the file, reboot Safari (just hold the Home button for a while) and you’ve banished that pesky .edu, or whatever you set out to do.

1 Comment

OmniFocus clippings from Firefox

1/08/2008
Summary: In which Martin has created an AppleScript to remedy Firefox’ poor support for OmniFocus‘ “Send to inbox” hotkey. This is a lengthy post with lots of dreary explanations, so you might want to jump to the finished script if you don’t care about how stuff works.

Edit: An alternative version brings up your Quick-add dialog and populates it. Check this comment if that sounds more appealing.

Fire Omni Quick

To get this to work you’ll need Quicksilver. But you’ve got that haven’t you? Sure you do.

I’m trying out OmniFocus nowadays for my obsessive-but-not-GTD-level organizing needs. It’s still a tad overkill for what I need, but they have the best iPhone sync so far, and I assume I’ll just be assimilated into the cult of David Allen sooner or later so what the hell.

One of the things I like about OmniFocus is the “Create Clipping” functionality. OmniFocus allows you to at a keystroke (♥) send whatever text is currently selected to your Inbox as a clipping.
However this isn’t working in Firefox 3. Mozilla practically stripped all the scripting abilities in v2 and i suppose that’s what goes for a “feature” nowadays. This needs some serious fixing. Let’s get our hands not-really-dirty-but-maybe-a-bit-smudged.

Edit: I’ve updated the script with a new version that also grabs the URL of the clippings origin.

Read the rest of this article »

6 Comments

Firebug hotkey fix

30/06/2008

I might be the only one with this problem, but in case that isn’t true I thought I’d write up a short one.

I love (Nay; Depend on!) Firebug for my HTML/CSS/JS work and I also hate having to use the mouse if I can get stuff done with a keystroke instead. (I’ll be writing a series of posts on how to avoid the mouse as much as possible in the near future.)

Firebug does come with a predefined hotkey to open/close the panel, but sadly that hotkey is F12 which I’ve already mapped to Exposé and am unwilling to give up. In theory This should be easy to fix by going to the System Preferences -> Keyboard And Mouse -> Keyboard Shortcuts panel and remapping there, but however much I tried I couldn’t get this to work. The solution? Risky and potentially ruinous hardcoding hackery of the plugin itself.

What I did was navigate to the .xul file within the Firebug folder buried deep within the user Library. The path below shows you where to find this file.

~Library/Application Support/Firefox/Profiles/nil9olmf.default/Extensions/firebug@software.joehewitt.com/content/firebug/browserOverlay.xul

Having opened this file in a text editor (preferably TextMate) I just did a search for the string “F12″, and lo and behold:

1
2
3
4
5
6
<keyset id="mainKeyset">
<key id="key_toggleFirebug" keycode="VK_F12"
command="cmd_toggleFirebug"/>
<key id="key_detachFirebug" keycode="VK_F12" modifiers="accel"
command="cmd_detachFirebug"/>
</keyset>

This line seemed pretty much what I was looking for. Crossing my fingers and with a silent prayer I merely changed the “VK_12″s to “VK_F2″s and Bob was, as they say, my uncle.

As is pretty much the case with anything I perpetrate against my Mac I don’t recommend you do this unless you’re ready to face the consequences. (I can’t imagine they’d be any more severe than having to reinstall Firebug, but still…) Chances are the problem only showed up in the first place because of my irresponsible messing about with stuff I should’ve left alone, but if you do have the same problem, here’s the fix.

Be aware also that you’ll have to repeat this procedure every time you update Firebug, so you may want to bookmark this blog and come back every day for the rest of your life.

4 Comments