1995-02-05 16:59:27 +00:00
|
|
|
"""Tools for use in AppleEvent clients and servers.
|
|
|
|
|
|
|
|
pack(x) converts a Python object to an AEDesc object
|
|
|
|
unpack(desc) does the reverse
|
|
|
|
|
|
|
|
packevent(event, parameters, attributes) sets params and attrs in an AEAppleEvent record
|
|
|
|
unpackevent(event) returns the parameters and attributes from an AEAppleEvent record
|
|
|
|
|
|
|
|
Plus... Lots of classes and routines that help representing AE objects,
|
|
|
|
ranges, conditionals, logicals, etc., so you can write, e.g.:
|
|
|
|
|
|
|
|
x = Character(1, Document("foobar"))
|
|
|
|
|
|
|
|
and pack(x) will create an AE object reference equivalent to AppleScript's
|
|
|
|
|
|
|
|
character 1 of document "foobar"
|
|
|
|
|
1995-07-17 11:42:23 +00:00
|
|
|
Some of the stuff that appears to be exported from this module comes from other
|
|
|
|
files: the pack stuff from aepack, the objects from aetypes.
|
|
|
|
|
1995-02-05 16:59:27 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from types import *
|
1995-01-30 11:53:55 +00:00
|
|
|
import AE
|
1995-07-17 11:42:23 +00:00
|
|
|
import AppleEvents
|
1995-01-30 11:53:55 +00:00
|
|
|
import MacOS
|
1998-01-16 14:40:10 +00:00
|
|
|
import sys
|
1995-02-05 16:59:27 +00:00
|
|
|
|
1995-07-17 11:42:23 +00:00
|
|
|
from aetypes import *
|
|
|
|
from aepack import pack, unpack, coerce, AEDescType
|
1995-02-05 16:59:27 +00:00
|
|
|
|
1996-09-20 15:28:28 +00:00
|
|
|
Error = 'aetools.Error'
|
|
|
|
|
1995-02-05 16:59:27 +00:00
|
|
|
# Special code to unpack an AppleEvent (which is *not* a disguised record!)
|
1995-07-17 11:42:23 +00:00
|
|
|
# Note by Jack: No??!? If I read the docs correctly it *is*....
|
1995-01-30 11:53:55 +00:00
|
|
|
|
|
|
|
aekeywords = [
|
|
|
|
'tran',
|
|
|
|
'rtid',
|
|
|
|
'evcl',
|
|
|
|
'evid',
|
|
|
|
'addr',
|
|
|
|
'optk',
|
|
|
|
'timo',
|
|
|
|
'inte', # this attribute is read only - will be set in AESend
|
|
|
|
'esrc', # this attribute is read only
|
|
|
|
'miss', # this attribute is read only
|
|
|
|
'from' # new in 1.0.1
|
|
|
|
]
|
|
|
|
|
|
|
|
def missed(ae):
|
|
|
|
try:
|
|
|
|
desc = ae.AEGetAttributeDesc('miss', 'keyw')
|
|
|
|
except AE.Error, msg:
|
|
|
|
return None
|
|
|
|
return desc.data
|
|
|
|
|
|
|
|
def unpackevent(ae):
|
|
|
|
parameters = {}
|
|
|
|
while 1:
|
|
|
|
key = missed(ae)
|
|
|
|
if not key: break
|
|
|
|
parameters[key] = unpack(ae.AEGetParamDesc(key, '****'))
|
|
|
|
attributes = {}
|
|
|
|
for key in aekeywords:
|
|
|
|
try:
|
|
|
|
desc = ae.AEGetAttributeDesc(key, '****')
|
|
|
|
except (AE.Error, MacOS.Error), msg:
|
1998-01-16 14:40:10 +00:00
|
|
|
if msg[0] != -1701 and msg[0] != -1704:
|
1995-01-30 11:53:55 +00:00
|
|
|
raise sys.exc_type, sys.exc_value
|
|
|
|
continue
|
|
|
|
attributes[key] = unpack(desc)
|
|
|
|
return parameters, attributes
|
|
|
|
|
|
|
|
def packevent(ae, parameters = {}, attributes = {}):
|
|
|
|
for key, value in parameters.items():
|
|
|
|
ae.AEPutParamDesc(key, pack(value))
|
|
|
|
for key, value in attributes.items():
|
|
|
|
ae.AEPutAttributeDesc(key, pack(value))
|
|
|
|
|
1995-07-17 11:42:23 +00:00
|
|
|
#
|
|
|
|
# Support routine for automatically generated Suite interfaces
|
1995-10-03 14:35:58 +00:00
|
|
|
# These routines are also useable for the reverse function.
|
1995-07-17 11:42:23 +00:00
|
|
|
#
|
|
|
|
def keysubst(arguments, keydict):
|
|
|
|
"""Replace long name keys by their 4-char counterparts, and check"""
|
|
|
|
ok = keydict.values()
|
|
|
|
for k in arguments.keys():
|
|
|
|
if keydict.has_key(k):
|
|
|
|
v = arguments[k]
|
|
|
|
del arguments[k]
|
|
|
|
arguments[keydict[k]] = v
|
|
|
|
elif k != '----' and k not in ok:
|
|
|
|
raise TypeError, 'Unknown keyword argument: %s'%k
|
|
|
|
|
|
|
|
def enumsubst(arguments, key, edict):
|
|
|
|
"""Substitute a single enum keyword argument, if it occurs"""
|
|
|
|
if not arguments.has_key(key):
|
|
|
|
return
|
|
|
|
v = arguments[key]
|
|
|
|
ok = edict.values()
|
|
|
|
if edict.has_key(v):
|
|
|
|
arguments[key] = edict[v]
|
|
|
|
elif not v in ok:
|
|
|
|
raise TypeError, 'Unknown enumerator: %s'%v
|
|
|
|
|
|
|
|
def decodeerror(arguments):
|
|
|
|
"""Create the 'best' argument for a raise MacOS.Error"""
|
|
|
|
errn = arguments['errn']
|
1997-08-08 15:00:03 +00:00
|
|
|
err_a1 = errn
|
1995-07-17 11:42:23 +00:00
|
|
|
if arguments.has_key('errs'):
|
1997-08-08 15:00:03 +00:00
|
|
|
err_a2 = arguments['errs']
|
|
|
|
else:
|
|
|
|
err_a2 = MacOS.GetErrorString(errn)
|
1995-07-17 11:42:23 +00:00
|
|
|
if arguments.has_key('erob'):
|
1997-08-08 15:00:03 +00:00
|
|
|
err_a3 = arguments['erob']
|
|
|
|
else:
|
|
|
|
err_a3 = None
|
|
|
|
|
|
|
|
return (err_a1, err_a2, err_a3)
|
1995-07-17 11:42:23 +00:00
|
|
|
|
|
|
|
class TalkTo:
|
|
|
|
"""An AE connection to an application"""
|
|
|
|
|
1998-01-16 14:40:10 +00:00
|
|
|
def __init__(self, signature, start=0, timeout=0):
|
1995-07-17 11:42:23 +00:00
|
|
|
"""Create a communication channel with a particular application.
|
|
|
|
|
|
|
|
Addressing the application is done by specifying either a
|
|
|
|
4-byte signature, an AEDesc or an object that will __aepack__
|
|
|
|
to an AEDesc.
|
|
|
|
"""
|
1996-09-20 15:28:28 +00:00
|
|
|
self.target_signature = None
|
1995-07-17 11:42:23 +00:00
|
|
|
if type(signature) == AEDescType:
|
|
|
|
self.target = signature
|
|
|
|
elif type(signature) == InstanceType and hasattr(signature, '__aepack__'):
|
|
|
|
self.target = signature.__aepack__()
|
1995-07-29 15:31:10 +00:00
|
|
|
elif type(signature) == StringType and len(signature) == 4:
|
1995-07-17 11:42:23 +00:00
|
|
|
self.target = AE.AECreateDesc(AppleEvents.typeApplSignature, signature)
|
1996-09-20 15:28:28 +00:00
|
|
|
self.target_signature = signature
|
1995-07-17 11:42:23 +00:00
|
|
|
else:
|
|
|
|
raise TypeError, "signature should be 4-char string or AEDesc"
|
|
|
|
self.send_flags = AppleEvents.kAEWaitReply
|
|
|
|
self.send_priority = AppleEvents.kAENormalPriority
|
1998-01-16 14:40:10 +00:00
|
|
|
if timeout:
|
|
|
|
self.send_timeout = timeout
|
|
|
|
else:
|
|
|
|
self.send_timeout = AppleEvents.kAEDefaultTimeout
|
1996-09-20 15:28:28 +00:00
|
|
|
if start:
|
|
|
|
self.start()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Start the application, if it is not running yet"""
|
1998-01-16 14:40:10 +00:00
|
|
|
_launch(self.target_signature)
|
1996-09-20 15:28:28 +00:00
|
|
|
|
1995-07-17 11:42:23 +00:00
|
|
|
def newevent(self, code, subcode, parameters = {}, attributes = {}):
|
|
|
|
"""Create a complete structure for an apple event"""
|
|
|
|
|
|
|
|
event = AE.AECreateAppleEvent(code, subcode, self.target,
|
|
|
|
AppleEvents.kAutoGenerateReturnID, AppleEvents.kAnyTransactionID)
|
|
|
|
packevent(event, parameters, attributes)
|
|
|
|
return event
|
|
|
|
|
|
|
|
def sendevent(self, event):
|
|
|
|
"""Send a pre-created appleevent, await the reply and unpack it"""
|
|
|
|
|
|
|
|
reply = event.AESend(self.send_flags, self.send_priority,
|
|
|
|
self.send_timeout)
|
|
|
|
parameters, attributes = unpackevent(reply)
|
|
|
|
return reply, parameters, attributes
|
|
|
|
|
|
|
|
def send(self, code, subcode, parameters = {}, attributes = {}):
|
|
|
|
"""Send an appleevent given code/subcode/pars/attrs and unpack the reply"""
|
|
|
|
return self.sendevent(self.newevent(code, subcode, parameters, attributes))
|
|
|
|
|
1996-01-29 15:45:09 +00:00
|
|
|
#
|
|
|
|
# The following events are somehow "standard" and don't seem to appear in any
|
|
|
|
# suite...
|
|
|
|
#
|
1995-07-17 11:42:23 +00:00
|
|
|
def activate(self):
|
|
|
|
"""Send 'activate' command"""
|
|
|
|
self.send('misc', 'actv')
|
1996-01-29 15:45:09 +00:00
|
|
|
|
1997-08-08 15:00:03 +00:00
|
|
|
def _get(self, _object, as=None, _attributes={}):
|
|
|
|
"""_get: get data from an object
|
1996-01-29 15:45:09 +00:00
|
|
|
Required argument: the object
|
|
|
|
Keyword argument _attributes: AppleEvent attribute dictionary
|
|
|
|
Returns: the data
|
|
|
|
"""
|
|
|
|
_code = 'core'
|
|
|
|
_subcode = 'getd'
|
|
|
|
|
|
|
|
_arguments = {'----':_object}
|
1997-08-08 15:00:03 +00:00
|
|
|
if as:
|
|
|
|
_arguments['rtyp'] = mktype(as)
|
1996-01-29 15:45:09 +00:00
|
|
|
|
|
|
|
_reply, _arguments, _attributes = self.send(_code, _subcode,
|
|
|
|
_arguments, _attributes)
|
|
|
|
if _arguments.has_key('errn'):
|
1996-09-20 15:28:28 +00:00
|
|
|
raise Error, decodeerror(_arguments)
|
1996-01-29 15:45:09 +00:00
|
|
|
|
|
|
|
if _arguments.has_key('----'):
|
|
|
|
return _arguments['----']
|
1998-01-16 14:40:10 +00:00
|
|
|
|
|
|
|
# Tiny Finder class, for local use only
|
|
|
|
|
|
|
|
class _miniFinder(TalkTo):
|
|
|
|
def open(self, _object, _attributes={}, **_arguments):
|
|
|
|
"""open: Open the specified object(s)
|
|
|
|
Required argument: list of objects to open
|
|
|
|
Keyword argument _attributes: AppleEvent attribute dictionary
|
|
|
|
"""
|
|
|
|
_code = 'aevt'
|
|
|
|
_subcode = 'odoc'
|
|
|
|
|
|
|
|
if _arguments: raise TypeError, 'No optional args expected'
|
|
|
|
_arguments['----'] = _object
|
|
|
|
|
|
|
|
|
|
|
|
_reply, _arguments, _attributes = self.send(_code, _subcode,
|
|
|
|
_arguments, _attributes)
|
|
|
|
if _arguments.has_key('errn'):
|
|
|
|
raise aetools.Error, aetools.decodeerror(_arguments)
|
|
|
|
# XXXX Optionally decode result
|
|
|
|
if _arguments.has_key('----'):
|
|
|
|
return _arguments['----']
|
|
|
|
#pass
|
|
|
|
|
|
|
|
_finder = _miniFinder('MACS')
|
|
|
|
|
|
|
|
def _launch(appfile):
|
|
|
|
"""Open a file thru the finder. Specify file by name or fsspec"""
|
|
|
|
_finder.open(_application_file(('ID ', appfile)))
|
|
|
|
|
|
|
|
|
|
|
|
class _application_file(ComponentItem):
|
|
|
|
"""application file - An application's file on disk"""
|
|
|
|
want = 'appf'
|
1995-07-17 11:42:23 +00:00
|
|
|
|
1998-01-16 14:40:10 +00:00
|
|
|
_application_file._propdict = {
|
|
|
|
}
|
|
|
|
_application_file._elemdict = {
|
|
|
|
}
|
1995-07-17 11:42:23 +00:00
|
|
|
|
1995-02-05 16:59:27 +00:00
|
|
|
# Test program
|
1995-07-17 11:42:23 +00:00
|
|
|
# XXXX Should test more, really...
|
1995-02-05 16:59:27 +00:00
|
|
|
|
1995-01-30 11:53:55 +00:00
|
|
|
def test():
|
1998-01-16 14:40:10 +00:00
|
|
|
target = AE.AECreateDesc('sign', 'quil')
|
1995-01-30 11:53:55 +00:00
|
|
|
ae = AE.AECreateAppleEvent('aevt', 'oapp', target, -1, 0)
|
|
|
|
print unpackevent(ae)
|
1995-02-05 16:59:27 +00:00
|
|
|
raw_input(":")
|
|
|
|
ae = AE.AECreateAppleEvent('core', 'getd', target, -1, 0)
|
|
|
|
obj = Character(2, Word(1, Document(1)))
|
|
|
|
print obj
|
|
|
|
print repr(obj)
|
|
|
|
packevent(ae, {'----': obj})
|
|
|
|
params, attrs = unpackevent(ae)
|
|
|
|
print params['----']
|
|
|
|
raw_input(":")
|
1995-01-30 11:53:55 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test()
|
1998-01-16 14:40:10 +00:00
|
|
|
sys.exit(1)
|