[Pythonmac-SIG] Okay folks--show me how and I'll write it up...

Michael J. Barber mjb@uma.pt
Thu, 28 Nov 2002 11:40:18 +0000


On Thursday, November 28, 2002, at 10:27  AM, Jack Jansen wrote:

>
> On woensdag, nov 27, 2002, at 22:36 Europe/Amsterdam, Daniel Lord wrote:
>
>> [Daniel wants to script iTunes]
> Osam is really a stopgap. The preferred way to control OSA 
> applications  from Python is to use the fullblown OSA interface, this 
> gives you  complete access to all features from Python, in a "pythonic" 
> way.  Unfortunately you first have to create the interface module for 
> the  specific program you want to control. This is done with  
> Mac/scripts/gensuitemodule.py. For some common applications the suites  
> are pre-generated, you'll find these in Mac/Lib/lib-scriptpackages.
>
> There is some minimal documentation, see Mac/Demo/applescript.html.
>
I would like very much to agree with this suggestion, but I just tried 
it out and gensuitemodule doesn't let me select iTunes as a "package 
generation target." This is MacPython 2.2.1, more recent versions may 
work. I ran into exactly this problem when trying to come up with a 
replacement example for the documentation that Jack mentions. It seems 
that the Mac OS X package structure interferes with gensuitemodule in a 
way that I don't understand. I don't know whether this should be viewed 
as a bug, or just a sign of limited understanding on my part.

Daniel, hopefully you won't have this problem, but if you do, it's well 
worth taking a look at how the packages are set up in one of the 
existing OSA packages (maybe for the Finder, it's got a rich enough 
structure to show some of the potential problems). The automatically 
generated OSA interfaces are a good model for what you will want to do.

>> 5) any other docs, examples, etc. on how to use Applescript with  
>> Python--send me a pointer please.
>
> Apart from the things mentioned above, also look at  
> Mac/Lib/findertools, or Mac/Lib/mkcwproject for examples that use the  
> OSA stuff.
>
I'll second this suggestion without reservations! The approach taken in 
findertools seems to be the most common one for OSA scripting from 
python: do as much in Python as possible, and only use OSA when you 
really need to communicate with another application. The functions in 
findertools are also pretty short and easy to understand.

Other sources:
The archives for this list. There have been periodic discussions of 
AppleScript/OSA on the mailing list. There was a discussion in January 
which I learned quite a lot from, and a more recent discussion in 
August. There have been others, too; I just happen to know when those 
two took place, because I took part in the discussions.

I have an example for OSAm that I provided in response to a list message 
with the subject 'Determining the name of the "root" drive (startup 
disk)'. You need to adjust your path to include the OSAm module, and 
using 'dir' is probably the easiest way to find out what functions 
you've got available.
Python 2.2.1 (#134, Apr  9 2002, 21:16:52)  [CW CARBON GUSI2 THREADS GC]
Type "copyright", "credits" or "license" for more information.
MacPython IDE 1.0.1
 >>> import OSAm
 >>> OSAm.CompileAndExecute('tell app "Finder" to return the name of the 
startup disk')
'"Macintosh HD"'
 >>>

Finally, I'd like to make a suggestion for Daniel and anyone else who 
takes part in this project: document what you're doing and turn it into 
a tutorial. I struggled when learning how to do OSA scripting from 
Python, others have said that they struggled, and I've seen the painful 
learning process that at least one person has gone through on this 
mailing list. Python is a lot nicer than AppleScript for OSA scripting, 
but it's pretty hard to get started. In my opinion, this is in part due 
to the small number of examples.