[Pythonmac-SIG] Appscript

has hengist.podd at virgin.net
Fri Dec 12 22:58:29 CET 2008


Conan C. Albrecht wrote:

> I'm using appscript to script Word and BibDesk.  I need to run the  
> script from the "Scripts" menu in one of these programs.  However,  
> my .py script won't show up on their menus, even if the script is in  
> the right directory.

I assume you're referring to BibDesk. I'm not familiar with it, but as  
far as I can tell it uses the OSA API to run scripts from its script  
menu. This means that it can only run scripts written in OSA languages.

In theory, you can turn any scripting language into an OSA component;  
in practice, packaging most popular interpreters as OSA components is  
a complete PITA due to design shortcomings in both. With Python, you  
have two very limited options:

- Philip Aker's PythonOSA component, which provides basic OSA support  
only (load/compile/run/store). It has no Apple event integration, so  
while PythonOSA scripts can use appscript to control other  
applications, it can't send or receive events to/from the process  
running the script, greatly limiting its usefulness.

- My PyOSA component, which supports the majority of the OSA API,  
including sending and receiving events. It is developer-release- 
quality only and no longer under development (I've stopped working on  
it due to the limitations of the CPython interpreter that make it less  
than ideal for in-process use). Unsupported; use entirely at own risk,  
etc.


>  The python script will show up on the system-wide scripts menu, but  
> not on an application's menu.

The system-wide scripts menu supports both OSA and shell scripts.


> To fix this, I added a simple AppleScript to the same (scripts)  
> directory. I've included this at the bottom, if it helps.  So the  
> AppleScript runs first, then it calls "do shell script" to run the  
> Python program.  My problem is that the script can't call any GUI  
> functions, such as the following:
>
> import osax
> sa = osax.OSAX()  # allows use of standard additions (like display  
> dialog)
> sa.display_dialog("Hi world")
>
> If I run from the command line, the dialog shows up fine.  If I run  
> from within Word or BibDesk, the dialog doesn't show up at all.

- Try calling sa.activate() to bring the Python process frontmost  
before telling it to display the dialog.

- If you are using Python 2.3, make sure you run the script with  
pythonw, not python (which can only perform GUI operations on Python  
2.4 and later).

- If your Python script is sending commands back to the application  
that's running the AppleScript, you will need to detach the Python  
process so that the AppleScript can immediately return control to the  
host application without waiting for the Python script to complete.  
Otherwise the AppleScript will block the application's main event  
loop, preventing the application handling events from the Python  
process and deadlocking everything until the event times out.


HTH

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



More information about the Pythonmac-SIG mailing list