OmniFocus clippings from Firefox

by Martin on 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.

So the situation is as follows:

  • OmniFocus lets you assign a hotkey to save whatever text is selected (in supported applications) as a Clipping. This is awesome, and if you copy i.e. from Mail.app OmniFocus will include the header info for the clipping. I want to use ⌘-F3 as my OmniFocus hotkey.
  • Firefox is my browser of choice, and I’m pretty sure that about 90% of my clippings would be from my browser. However Firefox doesn’t support the OmniFocus clippings hotkey.
  • However! OmniFocus also comes installed with a Quicksilver action appropriately named “OmniFocus: Send to Inbox”. Now we’re getting somewhere.

The fact that there’s a handy Quicksilver script is enough to cut loads of time creating a clipping. Consider this:

Creating a clipping from Firefox with/without Quicksilver

The default process to create a new clipping from Firefox:

  • 1. Select text in browser.
  • 2. Hit ⌘-C to copy the text.
  • 3. Tab over or click in dock to activate OmniFocus.
  • 4. Click the inbox.
  • 5. Hit enter.
  • 6. Hit ⌘-V to paste.
  • 7. Tab over or click in dock to activate Firefox. You’re done!

Now consider the same operation in Quicksilver:

  • 1. Invoke Quicksilver (I use ⌘-Escape ).
  • 2. Hit “.” to start typing.
  • 3. Hit ⌘-V to paste your clipping.
  • 4. Tab over to the next pane and type “Sen” if “OmniFocus: Send to Inbox” isn’t already selected.
  • 5. Hit enter. You’re back in Firefox.

A huge advantage with the second method over the first is that the Quicksilver action will also include the title of the webpage you clipped the text from. (Sadly not the URL. As mentioned: Get with the program Mozilla!) Also of course, it requires fewer steps and is faster… Not by a hell of a lot though. Let’s shave a few steps of that workflow.

Open your Quicksilver preferences and select the item “Actions” in the sidebar. In the next list select “Text”. What we’re going to do now is to drag the item “OmniFocus: Send to Inbox” to the top of the list, thus making it the default action whenever you paste or type text into Quicksilver.
Quick Silver Prefs
So that’s pretty good. 1. Copy. 2.Invoke Quicksilver, 3. Paste, 4. Hit return.
But we’re not done by a long shot. I want the one-click solution. Select text and Boom! There she flies. So let’s make that happen.

I looked around at the google but couldn’t really find a good solution for this so I broke out my trusty, somewhat rusty, Script Editor. What I’m about to do could probably be done faster in Quickeys, but I really don’t like some of the Quickeys methods I’d have to use, so AppleScript it is!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
tell application "Firefox"
  activate
end tell
tell application "System Events" to keystroke ¬
"c" using {command down}

delay 0.2
tell me to activate
set selecTxt to the clipboard as text
tell application "System Events" to keystroke ¬
(key code 53 using {command down})

tell application "System Events" to keystroke ¬
(key code 47)

tell application "System Events" to keystroke ¬
"v" using {command down}

delay 0.2
tell application "System Events" to keystroke (key code 36)
tell application "Firefox"
  activate
end tell

What we have here is an AppleScript that automates the entire 4-step process above! You select the text, invoke the script through a hotkey you define in Quicksilver and we finally have one-click clippings from Firefox!

But hold your horses. Let me show you how to set up that Quicksilver trigger, and on the way point out some problems. Open your Quicksilver preferences again and this time go to the “Triggers”-tab.
Quicksilver Trigger 1
Click the + to create a new trigger and type in the first few letters of the name of the script. When you’ve found it click the “Action” field and type “Run”. Now click the little “i” in the bottom of the window and choose a good hotkey. The hotkey should now work as advertised.
But consider this:

  • 1. We only want the script to run from Firefox. That sequence of commands could mess stuff up if it’s pressed in, say IfYouPressAnyKeyTheWorldBlowsUp.app. Now, Quicksilver has a neat little feature called “scopes” that ensure that hotkeys can only be triggered in predefined apps. How neat. Sadly it doesn’t work at all under Leopard. Why can’t shit just work!
    For this reason the script tells Firefox to activate as it’s first command.
  • 2. Now we have 2 hotkeys that serve basically the same purpose! One for all the cool apps that support OmniFocus-services and one for that weirdo Firefox which doesn’t.

Unacceptable! Let’s add some lines to the script.

 

Updated script. Will now also grab the URL of the page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
property theUrl : ""
property theNote : ""
property theDelimiter : "
------------------------
"

property sendOff : ""

if application "Firefox" is frontmost then
  tell application "Firefox" to activate

  tell application "System Events" to keystroke ¬
    "c" using {command down}
  delay 0.2

  tell me to activate
  set theNote to the clipboard as text

  tell application "Firefox" to activate
  delay 0.1
  tell application "System Events"
    keystroke (key code 37 using {command down})
    keystroke "c" using {command down}
  end tell
  delay 0.1

  tell me to activate
  tell application "System Events"
    set theUrl to the clipboard as text
    set sendOff to theUrl & theDelimiter & theNote
    set the clipboard to sendOff
  end tell
  delay 0.1

  tell application "System Events"
    keystroke (key code 53 using {command down})
    keystroke (key code 47)
    keystroke "v" using {command down}
    delay 0.2
    keystroke (key code 36)
  end tell

else
  tell application "System Events" to keystroke ¬
    (key code 99 using {control down, command down, ¬
      option down})

end if

So what’s happening here is this: I want my OmniFocus hotkey to be ⌘-F3 so what I’m gonna do is to define the hotkey in OmniFocus’ preferences to be something else. A key combo I’m not very likely to press, such as ⌘-⇧-⌥-F3. Then I assign the ⌘-F3 hotkey to the AppleScript from within Quicksilver (as showed above). Now whenever I hit ⌘-F3 is this (in pseudocode):

1
2
3
4
5
6
7
8
if Firefox is the current application

  keep running this script

if Firefox is NOT the current application


hit the following keys ?-?-?-F3

There. The script can be downloaded here. Please note that you’ll have to change the key codes according to what key combos you want to use. You can use the Freeware app Ukelele to help you figure out what the corresponding key code is for whatever you want to use.

There are 6 comments in this article:

  1. 5/08/2008Eivind Ingebrigtsen says:

    Yeah! Nice post – Didn’t know applescript looked so similar to good old actionscript 1.0

  2. 5/08/2008Martin says:

    AppleScript is extremely light-weight and, I suppose, limited but it’s absolutely awesome when it comes to scripting extra functionality into the OS.

    I have a number of small scripts I trigger from Quicksilver all the time to perform small and otherwise tedious tasks, or even give new functionality entirely.

  3. 16/04/2009chase says:

    Martin, GREAT job. You are a saint, but I can’t canonize you yet. Is there a way to make it come up in the OF quick-add box instead of go straight to the inbox? That way I could assign it to a project, due date, start date, &/or context right there.

    I’m not familiar with applescript, etc., to make that happen.

    you let me know. Ill send you a case of beer. with love from portland.

  4. 16/04/2009Martin says:

    Alright. Here you go:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
      property theUrl : ""
      property theNote : ""
      property theDelimiter : "
      ------------------------
      "

      property sendOff : ""
      property oldClipboard : ""

      delay 0.1
      --tell application "Firefox" to activate
      tell application "System Events"
        set theBrowser to name of first process whose frontmost is true
        if theBrowser is equal to "firefox-bin" then
          set theBrowser to "Firefox"
        end if
        set oldClipboard to the clipboard as text
      end tell

      tell application theBrowser to activate

      tell application "System Events"
        delay 0.1
        keystroke "c" using {command down}
        delay 0.1
        set theNote to the clipboard as text
        keystroke "l" using {command down}
        delay 0.3
        keystroke "c" using {command down}
        delay 0.1
        set theUrl to the clipboard as text
        delay 0.1
        set sendOff to "From: " & theUrl & theDelimiter & theNote
        set the clipboard to sendOff
        keystroke (key code 118 using {command down})
        delay 0.1
        keystroke "v" using {command down}
        delay 1
        set the clipboard to oldClipboard
      end tell

    That should populate the quick-add box for you. It’s even a bit cleaner than the previous script and restores your original clipboard after working its magic.

    The line you might want to change is this:

    1
    keystroke (key code 118 using {command down})

    . That’s where the hotkey for the quick-add box is triggered, so change it to whatever your hotkey is.

    So how about them beers now. Going to be an expensive package to send to Oslo. ;)

  5. 10/06/2009Joost says:

    Dear,
    how could I implement the script from Martin? Do I still need this piece of code: if Firefox is the current application…?

    Any help is appreciated.

  6. 10/06/2009Martin says:

    If you are referring to the script in comment 5 it works as a stand-alone.

    Just invoke the script using your preferred method (Quicksilver, Script Menu…) and it should activate the clipping window for you.

Write a comment: