
To run the following examples using the interactive Ruby interpreter, open a new window in Terminal.app, type irb and hit return. Once the interpreter is running, import the appscript module's contents using require 'appscript'; include Appscript
If using the appscript gem, don't forget to require 'rubygems' before requiring appscript, otherwise the appscript gem won't load.
Create a new "Hello World!" document in TextEdit:
app('TextEdit').documents.end.make(
:new => :document,
:with_properties => {:text => "Hello World!\n"}
)
Get the names of folders in user's Home folder:
# tell app "Finder" to get name of every folder of home
app('Finder').home.folders.name.get # Ruby
Count the words in the front TextEdit document:
# tell app "TextEdit" to count words of document 1
app('TextEdit').documents[1].words.count
Delete all the empty paragraphs in the front TextEdit document:
# tell app "TextEdit" to delete ¬
# (every paragraph of document 1 where it = "\n")
app('TextEdit').documents[1].paragraphs[its.eq("\n")].delete
Make an empty folder named 'My Folder' in Documents folder:
# tell app "Finder" to make new folder ¬
# at folder "Documents" of home ¬
# with properties {name:"My Folder"}
app('Finder').make(:new=>:folder,
:at=>app.home.folders['Documents']
:with_properties=>{:name=>'My Folder'})
Get the name of everyone in Address Book who has the email address 'foo@bar.com':
# tell application "Address Book" to get name of every person ¬
# where value of every email contains "foo@bar.com"
app('Address Book').people[
its.emails.value.contains('foo@bar.com')
].name.get
Additional examples are included in the appscript package.