py-appscript

12. Command examples

get

Get the name of every folder in the user's home folder:

# tell application "Finder" to get name of every folder of home

app('Finder').get(app.home.folders.name)

Note that if the direct parameter is omitted from the parameter list, the reference that the command is invoked on is used instead. For example, the above example would normally be written as:

app('Finder').home.folders.name.get()

Also note that the .get portion can be omitted for convenience:

app('Finder').home.folders.name()

set

Set the content of a TextEdit document:

# tell application "TextEdit" to set text of document 1 to "Hello World"

app('TextEdit').documents[1].text.set('Hello World')

count

Count the words in a TextEdit document:

# tell application "TextEdit" to count words of document 1

app('TextEdit').documents[1].words.count()

Count the items in the current user's home folder:

#tell application "Finder" to count items of home

app('Finder').home.count(each=k.item)

(Note that the each parameter is required in Finder's count command.)

make

Create a new TextEdit document:

# tell application "TextEdit" to make new document ¬
#     with properties {text:"Hello World\n"}

app('TextEdit').make(
    new=k.document,
    with_properties={k.text:'Hello World\n'})

Append text to a TextEdit document:

# tell application "TextEdit" to make new paragraph ¬
#     at end of text of document 1 ¬
#     with properties {text:"Yesterday\nToday\nTomorrow\n"}

app('TextEdit').make(
    new=k.paragraph,
    at=app.documents[1].text.end,
    with_data='Yesterday\nToday\nTomorrow\n')

Note that the make command's at parameter can be omitted for convenience, in which case the reference that the command is invoked on is used instead:

app('TextEdit').documents[1].text.end.make(
    new=k.paragraph,
    with_data='Yesterday\nToday\nTomorrow\n')

duplicate

Duplicate a folder to a disk, replacing an existing item if one exists:

# tell application "Finder" to ¬
#     duplicate folder "Projects" of home to disk "Work" with replacing

app('Finder').home.folders['Projects'].duplicate(
    to=app.disks['Work'], replacing=True)

add

Add every person with a known birthday to a group named "Birthdays":

# tell application "Contacts" to add ¬
#     every person whose birth date is not missing value ¬
#     to group "Birthdays"

app('Contacts').people[
    its.birth_date != k.missing_value
    ].add(to=app.groups['Birthdays'])