Today I'm going to describe how I can send the contents of the currently selected Tweet in Twitterrific to OmniFocus using AppleScript.
The first step is to pull the necessary information from Twitterrific. In my case, I'm going to use the name of the poster as the title of the task, with the text of the Tweet as the note on the task. While this seems a bit weird at first, I couldn't think of a better way to handle it - while the content makes sense as the title at first, links in task titles aren't clickable. Plus, that could result in some pretty long tasks titles. This seems like the most automatic way of handling the situation.
In Twitterrific's AppleScript dictionary, there is a tweet object that contains a variety of information about the tweet. Today, only two properties are of interest: text (which contains the content of the tweet) and user name (which is the real name of the poster (and yes, AppleScript properties don't have to be just one word, which takes a bit of getting used to)).
For me, the hardest part of understanding AppleScript is the syntax. While it is my understanding that Apple developed it so that AppleScript would be easy for most anyone to use, for me this is hardly the case. Maybe it is because I'm use to more traditional programming languages, but AppleScript's English-like syntax strikes me as rather odd. I'm starting to get used to it, finally, but it's extremely different from any other programming language that I've used.
To pull information from a running application, you tell the application what information you want. To get the text of the currently selected tweet, the following AppleScript will do just that:
tell application "Twitterrific" to set tweetText to the text of selection
This sets the contents of the variable tweetText to the value of the text property of the currently selected tweet.
If multiple actions are desired against the same application, then you can use a multi-line tell:
tell application "Twitterrific"
set tweetText to the text of selection
set tweetPoster to the user name of selection
end tell
In addition to getting the content of the tweet, it also sets the real name of the poster to the tweetPoster variable.
Now that we have the information that we need from Twitterrific, we need to send that information to OmniFocus by creating a new task. This is a bit more complicated, but not too bad.
All projects, contexts, and tasks in OmniFocus are saved in a document. The document that is opened by default when running OmniFocus can be retrieved by getting the value of the default document property of the OmniFocus application. Since we're going to be asking multiple things of the document, I'm going to create a multi-line tell for it:
tell application "OmniFocus"
tell default document
-- Interact with the document here
end tell
end tell
(In AppleScript, -- is the comment character. Any text after that line is treated as a comment, and is ignored when the script is executed.)
In order to create a task, the following information is needed:
- Task Name
- Context
- Project
- Notes
We have the name and note for the task already - those are stored in the tweetPoster and tweetText variables, respectively. The context and project for the task can be retrieved from the OmniFocus document that we just set up.
First, the project. Each OmniFocus document has a projects collection that can be used for this. In my case, I have a "Follow Up" project that I use to keep any tasks that I want to look into later. To get this project from AppleScript, use the following:
set theProject to project "Follow Up"
To use a different project name, just change the text in the quotes.
Getting the context can be a bit more tricky. If you want to use a top level context (that is, a context that is not within another context), then it is as simple as getting the project, only replace project with context:
set theContext to context "Online"
However, in my case, the context that I want to use is within two other contexts. To get my context, you just chain them together:
set theContext to context "Online" of context "Mac" of context "Home"
Now that we have all of the necessary parts of the task, we can tell OmniFocus to create. This is done using the make action:
set newTask to make new inbox task
with properties {name:tweetPoster,
note:tweetText,
context:theContext}
set assigned container of newTask to theProject
(You do not need to have the first two lines on separate lines - I only wrote it that way so that it would look nicer here.)
The project of the new task can't be set using properties, but instead it is set to the assigned container property of the task.
While the new task is now created, it is still sitting in the OmniFocus Inbox - even though it has a project and context defined. While you could press the Clean Up button manually in OmniFocus, that isn't very automatic now, is it? ;) Thankfully, this too can be accomplished via AppleScript:
compact
And that's it! The contents of the current tweet in Twitterrific are now safely stored in OmniFocus to be processed later.
Click the "more" link below for the full text of the AppleScript described here. If you have any questions, comments, or suggestions about this, please don't hesitate to ask. :)