[Pythonmac-SIG] appscript fails to notice System Events is not running

has hengist.podd at virgin.net
Mon Mar 29 23:18:24 CEST 2010


Noah Coccaro wrote:

> I have a long running server that uses appscript to send keystroke commands to System Events. 
> It seems that System Events does not run constantly, or frequently dies. 
> Quite often, appscript seems to be in a state where it incorrectly thinks System Events is running, and still sends commands to it, resulting in failures.

Appscript makes no assumptions as to whether an application is running or not, except for the first time you use a newly created app object. By default, that is the only time that appscript will automatically launch an application for you if it is not already running. This is different to AppleScript's behaviour but is quite deliberate: if a target application crashes halfway through your script, you generally want to know about this as any documents your script may have been working on will be lost, so there is no point in trying to manipulate them further.

In some cases, the default appscript behaviour may not be what you want. In this case, you have several options:

1. Create a new app object before sending a series of commands. This is a fairly cheap option as you are only doing a search of currently running processes for the one you want. (The main cost in creating app objects is in parsing the application dictionary, and appscript caches parsed dictionaries for efficiency.) One downside is that this won't affect existing references that your script created before the application quit, but chances are these references will no longer be valid anyway so no great loss in practice.

2. Send a 'run' or 'launch' command before sending a series of commands. By default, these commands will automatically relaunch the application if necessary and update the app object to point to the current process.

3. Create an app object, then set its 'relaunchmode' property to "always". This will allow the app object to relaunch and reconnect to the target application before sending any command (i.e. equivalent to the AppleScript behaviour).

See chapter 7 of the appscript manual for more information.

HTH

has

-- 
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net



More information about the Pythonmac-SIG mailing list