[Pythonmac-SIG] Appscript

Conan C. Albrecht conan at warp.byu.edu
Fri Dec 12 08:30:35 CET 2008


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.  The python script will show up on the system- 
wide scripts menu, but not on an application's menu.  I changed the  
file extension to .scpt, and it still won't show up.

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.  I  
think it is because "do shell script" runs it in an invisible shell  
that can't access the main GUI.  So the dialog comes up, but is not  
visible to the user.  The calling application (Word or BibDesk)  
freezes since the dialog is "up" but can't be responded to by the user.

Is there a better way to do this?  How can I run Python scripts from  
the Scripts menu in a program?

Thanks in advance.

=================================
-- Get this script's directory and name
tell application "Finder" to (path to me) as Unicode text
set scriptname to POSIX path of result

-- Remove the .scpt extension, and append .py instead
set AppleScript's text item delimiters to {".scpt"}
set textItems to text items of scriptname
set AppleScript's text item delimiters to {""}
set scriptname to textItems as string
set scriptname to scriptname & ".py"

-- Run the script
do shell script "python '" & scriptname & "'"
===================================

____________________________________
Conan C. Albrecht, Ph.D.
Information Systems Department
Brigham Young University
Email: conan at warp.byu.edu
Phone: +1-801-805-1615
Web/Blog: http://warp.byu.edu/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2727 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pythonmac-sig/attachments/20081212/c25129d2/attachment.bin>


More information about the Pythonmac-SIG mailing list