nvALT to OmniFocus Service

Description

Automator Workflow Screenshot

Michael Schechter of A Better Mess was looking for a way to clip items from nvALT into OmniFocus and created a Keyboard Maestro macro to do it.

He wasn't able to make the URL linking work correctly and Ken Case from OmniFocus offered an AppleScript to help. I was able to come up with something that automatically fixes the URL and I decided to roll it all into an OS X Service so that people who don't own Keyboard Maestro can use it as well.

USAGE

Highlight the text you want to clip within nvALT and trigger the Service by:

  • Control-Clicking (a.k.a., "right clicking") the highlighted text. When the context menu opens, select "Services" => "Clip to OmniFocus from nvALT";
  • From the main nALT menubar, select "nvALT" => "Services" => "Clip to OmniFocus from nvALT";
  • Open System Preferences and choose Keyboard > Keyboard Shortcuts [Services]. In the right window scroll down until you find your newly created service. Now double-click to the right of the Service name. When the text field appears, press the keys which you would like to become your shortcut. Also, make sure that the check box next to your Service is ticked — this activates the Service. (You can always return to this window and turn off the check box to disable this Service.)

REQUIREMENTS

THIS CODE REQUIRES THAT YOU ENABLE UI SCRIPTING!

Before you can use UI scripting, you have to activate it in your System Preferences. To do that, go to System Preferences, Universal Access and check the box to "Enable Access For Assistive Devices". Universal Access

Download Link

CLICK HERE TO DOWNLOAD THE SERVICE

Installation

Unzip the file and click on the enclosed service to install it. Alternately, you can manually place it in your User's folder path for /Library/Services

Suggestions?

I've left the code pretty much as Ken wrote it but, if you have a brilliant idea to make this work better, please leave a comment below or send me a message on Twitter or on App.net!

The Code

(*
◸ Veritrope.com
nvALT to OmniFocus
VERSION 1.01
September 3, 2012

// UPDATE NOTICES
     ** Follow @Veritrope on Twitter, Facebook, Google Plus, and ADN for Update Notices! **

// SUPPORT VERITROPE!
     If this AppleScript was useful to you, please take a second to show your love here:
     http://veritrope.com/support

// SCRIPT INFORMATION AND UPDATE PAGE
     http://veritrope.com/code/nvalt-to-omnifocus

// REQUIREMENTS
     More details on the script information page.

// CHANGELOG
    1.1     Bug Fix
    1.0     Initial Release

// RECOMMENDED INSTALLATION INSTRUCTIONS:
    1.) Place this code in a "Run AppleScript" block in Automator and Save.

    2.) If you downloaded the service, place it in your User's folder of /Library/Services

// TERMS OF USE:
     This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

     To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.


*)


on run {input}
   
    (*
======================================
// USER SWITCHES (YOU CAN CHANGE THESE!)
======================================
*)

   
    set TaskName to ""
    set TaskLinkName to "Open in nvALT"
   
    (*
======================================
// MAIN PROGRAM
======================================
*)

    set TaskNote to input
    set nvURL to ""
   
    --GET nvALT URL
    tell application "nvALT" to activate
    tell application "System Events"
        tell its process "nvALT"
            set frontmost to true
            click menu item "Copy URL" of its menu "Edit" of its menu bar 1
           
            -- FOR SOME REASON, YOU HAVE TO DO THIS TWICE
            -- TO GET THE VALUE TO WORK.
            -- IN THE WORDS OF KURT VONNEGUT,
            -- "AND SO IT GOES…"
            click menu item "Copy URL" of its menu "Edit" of its menu bar 1
           
            set theURL to the clipboard
            set nvURL to my replaceText("nv:", "nvalt:", theURL)
        end tell
    end tell
   
    -- CREATE QUICK ENTRY IN OMNIFOCUS
    tell application "OmniFocus"
        tell quick entry
            -- OPEN THE QUICK ENTRY WINDOW
            open
           
            -- CREATE A NEW INBOX ITEM
            set NewInboxItem to (make new inbox task with properties {name:TaskName, note:TaskNote})
           
            -- ADD A LINK TO THE TOP OF THE NOTE
            tell note of NewInboxItem
                insert TaskLinkName & return & return at before first character
                set value of attribute "link" of style of paragraph 1 to nvURL
            end tell
           
            -- EXPAND THE NOTE SO IT'S VISIBLE IN THE QUICK ENTRY WINDOW
            set note expanded of last leaf to true
           
            -- Start editing the task name
            select last leaf -- Select the new item
            activate -- Make the app active so it gets our scripted keystrokes
            delay 0.25 -- Give it a chance to activate
            tell application "System Events"
                if UI elements enabled is true then
                    tell process "OmniFocus"
                        keystroke "e" -- Starts editing the current row
                        keystroke "a" using command down -- Command-A selects the text
                    end tell
                end if
            end tell
        end tell
    end tell
end run

(*
======================================
// UTILITY SUBROUTINES
======================================
*)

on replaceText(find, replace, someText)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set someText to text items of someText
    set text item delimiters of AppleScript to replace
    set someText to "" & someText
    set text item delimiters of AppleScript to prevTIDs
    return (someText as text)
end replaceText