appscript_mac_os_x 

Send to Kindle
home » snippets » python » appscript_mac_os_x



Mapping AppleScript sentences to method calls

See http://appscript.sourceforge.net/py-appscript/examples.html

The Ruby examples are more extensive. See some at http://appscript.sourceforge.net/rb-appscript/doc/appscript-manual/11_applicationcommands.html

"make new"

# tell app "Finder" to make new folder ¬
#       at folder "Documents" of home ¬
#       with properties {name:"My Folder"}

from appscript import app
app('Finder').make(new=k.folder,
    at=app.home.folders['Documents'],
    with_properties={k.name: 'My Folder'})

Get an app object from the pid

from osax import app
p = app(pid=12345)

From Appscript & OSAX

About the only downside to the osax module is that it’s not that well documented. So you typically you have to do a bit of guessing based upon how it is done in Applescript. So the Applescript

set the clipboard to i

becomes

sa.set_the_clipboard_to( i )

where “sa” is the scripting addition object set from the osax module.

So to speak an item you’d use this code:

from osax import OSAX
sa = OSAX()
sa.say("Speaking text from python")
sa.display_dialog("A dialog box")

MacScripter has a nice collection of other osax plugins for Applescript that you should be able to call with the osax module. As I said though, with a few exceptions, there are typically better Python modules for most functions. There are some that are nice for quick and dirty UI features.

Here are a few useful functions in osax.

beep( int ) – beeps a specified number of times. (If not specified then once)

sa.clipboard_info() – information about what’s on the clipboard. (Mainly types)

sa.set_the_clipboard_to() – sets the clipboard

sa.the_clipboard() – gets the contents of the clipboard

sa.list_disks() – gets mounted disks.

sa.path_to() – get path to various objects. (k.scripts_folder, k.me, k.frontmost_application, k.current_application, k.applications_folder, k.application_support, k.desktop, k.documents_folder, k.downloads_folder, k.trash) (Lots more you should be able to figure out from here)

sa.open_location( url ) – open an url in the default browser

sa.get_volume_settings() – get info on the current volume

sa.set_volume( int ) – set the volume

sa.summarize( text ) – summarizes the text with Apple’s summarizer

sa.choose_application() – brings up a dialog allowing an application to be selected

sa.choose_file() – brings up a dialog allowing a file to be selected (and returns the path)

sa.choose_file_name() – brings up a save dialog and returns the path to the file

sa.choose_from_list( python_array ) – brings up a dialog with the items of a python array and returns the selected one.

sa.display_alert() – displays an alert

sa.display_dialog() – displays a dialog box

sa.say( text ) – speaks text

Now I’d suggest looking at Apple’s documentation for the above. Many have optional arguments. The only think to remember is that constants for osax you merely prepend a “k.” and replace spaces with the underscore character (_). So it the constant in Applescript is say “scripts folder” your python would simply be k.scripts_folder. Note you have to worry about namespaces. For argument names you just pass the name replacing spaces with underscores. So consider the following Applescript:

display dialog "Enter your text here..." buttons {"Choice 1", "Choice 2"}

In Python, including the module loading, it would be:

import osax
sa = osax.OSAX()

sa.display_dialog("Enter your text here...", buttons = ["Choice 1", "Choice 2"])

Here’s a more complex example. (Note the explicit namespace for constants)

sa.display_dialog("Please enter some text", with_title="My Title", with_icon=osax.k.caution, default_answer="Default")

Play around with this module as it really is amazing useful.