Appscript snippets

See all my hacks.

rb-appscript is an implementation of AppleScript for Ruby. It's great, but it's often hard to translate between the AppleScript in AppleScript Editor dictionaries, and finding the right Appscript incantations. (IRB is great for experimenting with this). So I wanted to collect all my little snippets here.

There's a whole book about rb-appscript.

Install rb-appscript

sudo gem install rb-appscript

and include it in your scripts

require 'appscript'
include Appscript

General commands

Brings app to foreground

app("AnyApp").activate

BibDesk

See my BibDesk hacks.

Open a given bibliography (also opens BibDesk, if it wasn't open before)

app("BibDesk").open("/path/to/file")

Get the current selection (an array, use [0] for the first selected citation)

app("BibDesk").document.selection.get

Search for a citation, author, etc. Returns an array of hits.

searchresult = app("BibDesk").search({:for => arg})

Get citekey for the first currently selected citation

app("BibDesk").document.selection.get[0][0].get.cite_key.get

Set the value of a custom field

app("BibDesk").document.search({:for => "citekey2003"})[0].fields["CustomField"].value.set("1")

DevonThink Pro

Creates a folder in DevonThink (and all folders above, if necessary) in the current database, and returns the folder handle

folder = app("DevonThink Pro").create_location("/Folder/Structure").get

Create a record. This one took me a while to figure out (the folder can be the container that was returned from the command above). The easiest is to create plain-text notes. Creating rich text notes is really annoying with AppleScript. HTML notes are easy, but they cannot be edited in DevonThink.

app("DevonThink Pro").create_record_with( { :name => "Name", :type_ => :text, :rich_text => "Contents", :tags => "tags", :comment => "comment", :label => 1, :creation_date => "date" }, {:in => folder} )

Chrome

Get URL of currently active window

app("Google Chrome").windows[1].get.tabs[dt.windows[1].get.active_tab_index.get].get.URL.get

Skim

Open a PDF (and start Skim, if it isn't running)

app('Skim').open("/path/to/file")

Returns array of references to open documents

document_reference = app('Skim').document.get

Returns name of currently frontmost document

name = app('Skim').document.get[0].name.get

Returns array of open document names

name = app('Skim').document.name.get

Get the current page number

page = app('Skim').document.get[0].current_page.get.index.get

Go to a specific page number (zero-index)

document_reference.go({:to => dd.pages.get[page-1]})

Save a PDF

app('Skim').save(document_reference)

Finder

Reveal a file in Finder (opens the correct folder, and selects the file)

app('Finder').reveal( MacTypes::FileURL.path("fullpath") )

Get currently selected filenames. I did this differently before (using the URL property), but this broke in OSX Lion. Thank you to LaC who answered my question on StackOverflow, this is the new solution:

app('Finder').selection.get(:result_type=>:alias).each do |item|
  url = item.path
end
Print/export