py-appscript manual | 10. Reference examples

#

## 10. Reference examples

### Application objects

    from appscript import *

    # application "Finder"
    app('Finder')

    # application "Macintosh HD:System:Applications:TextEdit.app:"
    app('/System/Applications/TextEdit.app')

### Property references

    # a reference to startup disk of application "Finder"
    app('Finder').startup_disk

    # a reference to name of folder 1 of home of application "Finder"
    app('Finder').home.folders[1].name

    # a reference to name of every item of home of application "Finder"
    app('Finder').home.items.name

    # a reference to text of every document of application "TextEdit"
    app('TextEdit').documents.text

    # a reference to color of character 1 of every paragraph of text ¬
    #     of document 1 of application "TextEdit"
    app('TextEdit').documents[1].text.paragraphs.characters[1].color

### All elements references

    # a reference to disks of application "Finder"
    app('Finder').disks

    # a reference to every word of every paragraph of text of every document ¬
    #     of application "TextEdit"
    app('TextEdit').documents.text.paragraphs.words

### Single element references

    # a reference to disk 1 of application "Finder"
    app('Finder').disks[1]

    # a reference to file "ReadMe.txt" of folder "Documents" of home of application "Finder"
    app('Finder').home.folders['Documents'].files['ReadMe.txt']

    # a reference to paragraph -1 of text of document 1 of application "TextEdit"
    app('TextEdit').documents[1].text.paragraphs[-1]

    # a reference to middle paragraph of text of last document of application "TextEdit"
    app('TextEdit').documents.last.text.paragraphs.middle

    # a reference to any file of home of application "Finder"
    app('Finder').home.files.any

### Relative references

    # a reference to paragraph before paragraph 6 of text of document 1 of application "TextEdit"
    app('TextEdit').documents[1].text.paragraphs[6].previous(k.paragraph)

    # a reference to paragraph after character 30 of document 1 of application "Tex-Edit Plus"
    app('Tex-Edit Plus').documents[1].characters[30].next(k.paragraph)

### Element range references

    # a reference to words 1 thru 4 of text of document 1 of application "TextEdit"
    app('TextEdit').documents[1].text.words[1:4]

    # a reference to paragraphs 2 thru -1 of text of document 1 of application "TextEdit"
    app('TextEdit').documents[1].text.paragraphs[2:-1]

    # a reference to folders "Documents" thru "Music" of home of application "Finder"
    app('Finder').home.folders['Documents':'Music']

    # a reference to text (word 3) thru (paragraph 7) of document 1 of application "Tex-Edit Plus"
    app('Tex-Edit Plus').documents[1].text[con.words[3]:con.paragraphs[7]]

### Filter references

    # a reference to every document of application "TextEdit" whose text is "\n"
    app('TextEdit').documents[its.text == '\n']

    # a reference to every paragraph of document 1 of application "Tex-Edit Plus" ¬
    #     whose first character is last character
    app('Tex-Edit Plus').documents[1].paragraphs[
        its.characters.first == its.characters.last]

    # a reference to every file of folder "Documents" of home of application "Finder" ¬
    #     whose name extension is "txt" and size < 10240
    app('Finder').home.folders['Documents'].files[
        (its.name_extension == 'txt').AND(its.size < 10240)]

### Insertion location references

    # a reference to end of documents of application "TextEdit"
    app('TextEdit').documents.end

    # a reference to before paragraph 1 of text of document 1 of application "TextEdit"
    app('TextEdit').documents[1].text.paragraphs[1].before