[Pythonmac-SIG] Applescript Confusion?

Michael J. Barber mjb@uma.pt
Tue, 5 Mar 2002 10:40:37 +0000


On Monday, March 4, 2002, at 06:41  PM, Schollnick, Benjamin wrote:

> 	I'd like to setup a Python program to automatically run some
> 	Applescript commands here....
>
> 	(Freehand 10, Open command...)
>
Does Freehand 10 imply OS X? I'll assume it does...

> 	But I don't see any way to do the equivalent of:
>
> 		tell application "Macromedia Freehand 10"
> 			open "filename"
> 		end tell
>
> 	I suspect it's possible, but I'm not sure how to do it in
> 	python...
>
> 	(And I've got the OPEN syntax wrong, but my Applescript
> 	book is at home.... Anyone want to fix it?)
>
I think you just need to replace 'open "filename"' by 'open alias 
"filename"'. I don't have Freehand around to try it, but that is at 
least how you can use open from a number of other apps.


> 	The entire idea, is that I could use SYS.ARGV to change the
> 	name of the application, and the open target...
>
> 	Thus our automation becomes a little simpler...
>

Bill Bedford has already explained a way to do this using an OSA 
interface package created by gensuitemodule. This is a good approach in 
general, but might be awkward if you have a large number of applications 
that you want to allow for. I'll suggest an alternative that MacPython 
won't like, but that you can use in a Unix Python: osascript.

Store your script in a string and pass it to osascript through a pipe. 
It should look something like:

import os

scriptTemplate = "osascript -e 'tell app \"%s\" to open alias \"%s\"'"
app = "Macromedia Freehand 10"
fn = "filename"

osapipe = os.popen(scriptTemplate % (app, fn), 'r')
osapipe.close()

Since you have to deal with quotes for AppleScript, Python, and the 
shell, you'll probably have to escape some of the quotes, like I did 
above. Hopefully, there are no unpleasant line-breaks inserted by 
Mail.app, but it's a simple enough script to figure out even if it did.