Home

Awesome

py-applescript -- An easy-to-use Python wrapper for NSAppleScript, allowing Python scripts to communicate with AppleScripts and AppleScriptable applications.

Features

Requirements

Installation

To install the applescript package, cd to the py-applescript-x.x.x directory and run:

python setup.py install

or:

python3 setup.py install

Interface

The applescript package exports four classes - AppleScript, ScriptError, AEType and AEEnum - plus one constant, kMissingValue, and one module, kae.

AppleScript

Represents a compiled AppleScript. The script object is persistent; its handlers may be called multiple times and its top-level properties will retain current state until the script object's disposal.

Properties:

source : str -- the script's source code

Methods:

__init__(self, source=None, path=None)
	source : str | None -- AppleScript source code
	path : str | None -- full path to .scpt/.applescript file

	Notes:
		- Either the path or the source argument must be provided.
		- If the script cannot be read/compiled, a ScriptError is raised.

run(self, *args) -- Run the script, optionally passing arguments to its run handler.
	args : anything -- arguments to pass to script, if any; see also supported type mappings documentation
	Result : anything | None -- the script's return value, if any

	Notes:
		- The run handler must be explicitly declared in order to pass arguments.
		- AppleScript will ignore excess arguments. Passing insufficient arguments will result in an error.
		- If execution fails, a ScriptError is raised.

call(self, name, *args) -- Call the specified user-defined handler.
	name : str -- the handler's name (case-sensitive)
	args : anything -- arguments to pass to script, if any; see documentation for supported types
	Result : anything | None -- the script's return value, if any
	
	Notes:
		- The handler's name must be a user-defined identifier, not an AppleScript keyword; e.g. 'myCount' is acceptable; 'count' is not.
		- AppleScript will ignore excess arguments. Passing insufficient arguments will result in an error.
		- If execution fails, a ScriptError is raised.

ScriptError

Indicates an AppleScript compilation/execution error.

Properties:

message : str -- the error message
number : int | None -- the error number, if given
appname : str | None -- the name of the application that reported the error, where relevant
range : (int, int) -- the start and end points (1-indexed) within the source code where the error occurred

AEType

An AE type. Maps to an AppleScript type class, e.g. AEType(b'utxt') <=> 'unicode text'.

Hashable and comparable, so may be used as keys in dictionaries that map to AE records.

Properties:

code : bytes -- four-char code, e.g. b'utxt'

Methods:

__init__(self, code)
	code : bytes -- four-char code, e.g. b'utxt'

AEEnum

An AE enumeration. Maps to an AppleScript constant, e.g. AEEnum(b'yes ') <=> 'yes'

Properties and methods are same as for AEType.

kMissingValue

Convenience constant. Contains AEType(b'msng'), i.e. AppleScript's 'missing value' constant.

kae

This module contains common AE constants auto-generated from OS X header files. For example: from applescript import kae; kae.typeUnicodeText.

Usage

import applescript

1. Run script:

applescript.AppleScript('say "Hello AppleScript"').run()

2. Call run handler and user-defined handlers with/without arguments:

scpt = applescript.AppleScript('''
	on run {arg1, arg2}
		say arg1 & " " & arg2
	end run
	
	on foo()
		return "foobar"
	end foo
	
	on Bar(x, y)
		return x * y
	end bar
''')

print(scpt.run('Python', 'Calling')) #-> None
print(scpt.call('foo')) #-> "foobar"
print(scpt.call('Bar', 3, 5)) #-> 15

3. A compiled script's state persists until the AppleScript instance is disposed:

scpt = applescript.AppleScript('''
	property _count : 0
	
	on run
		set _count to _count + 1
	end run
''')

print(scpt.run()) #-> 1
print(scpt.run()) #-> 2
print(scpt.run()) #-> 3

4. Errors will be reported:

applescript.AppleScript('this is not a valid script')
# applescript.ScriptError: A identifier can't go after this identifier. (-2740) range=12-19

Supported type mappings

The following Python <=> AppleScript mappings are supported:

None <=> [no value; see Limitations]
bool <=> boolean
int <=> integer [or 'real' in some cases; see Limitations]
float <=> real
str <=> text [a.k.a. string, Unicode text]
bytes <=> data
list <=> list
tuple => list [one-way mapping only]
dict <=> record [with restrictions on keys; see Limitations]
str <= alias/POSIX file [one-way mapping only; see Limitations]
pyapplescript.AEType <=> type class [keyword]
pyapplescript.AEEnum <=> constant [keyword]
datetime.datetime <=> date [no timezone support; see Limitations]

Limitations:

Known issues

See Also

Copyright

py-applescript is released into the public domain. No warranty, E&OE; use at own risk, etc.