py-appscript

8. Examples

Identifying Applications

# application "Macintosh HD:System:Applications:TextEdit.app"
textedit = aem.Application('/System/Applications/TextEdit.app')

# application "TextEdit"
textedit = aem.Application(aem.findapp.byname('TextEdit'))

# application "TextEdit" of machine "eppc://my-mac.local"
textedit = aem.Application('eppc://my-mac.local/TextEdit')

Building References

# name (of application)
aem.app.property(b'pnam')

# text of every document
aem.app.elements(b'docu').property(b'ctxt')

# end of every paragraph of text of document 1
aem.app.elements(b'docu').byindex(1).property(b'ctxt').elements(b'cpar').end

# paragraphs 2 thru last of first document
aem.app.elements(b'docu').first.elements(b'cpar').byrange(
        aem.con.elements(b'cpar').byindex(2), 
        aem.con.elements(b'cpar').last)

# paragraphs of document 1 where it != "\n"
aem.app.elements(b'docu').byindex(1).elements(b'cpar').byfilter(aem.its.ne('\n'))

Sending Events

# quit TextEdit
textedit.event(b'corequit').send()

# name of TextEdit
print textedit.event(b'coregetd', {b'----': aem.app.property(b'pnam')}).send()

# count documents of TextEdit
print textedit.event(b'corecnte', {b'----': aem.app.elements(b'docu')}).send()

# make new document at end of documents of TextEdit
textedit.event(b'corecrel', {
        b'kocl': aem.AEType(b'docu'), 
        b'insh': aem.app.elements(b'docu').end
        }).send()