[Pythonmac-SIG] apple script and MacPython code

has hengist.podd at virgin.net
Wed Oct 26 22:49:24 CEST 2005


Zhi Peng wrote:

>I strongly believe that MacPython can be used to write similar code to perform Applescript code functions. I try to find corresponding MacPython code for apple script. For example following adobe apple script code
> 
>tell application "Adobe InDesign CS2"
>    set myDoc to make document
>    tell myDoc
>        tell page 1
>        set myTextFrame to make text frame
>        set geometric bounds of myTextFrame to {"6p0","6p0","18p0","18p0"}
>        set contents of myTextFrame to "HELLO"
>       end tell
>    end tell
>end tell

Python equivalent:

#!/usr/bin/pythonw
from appscript import *

id = app("Adobe InDesign CS2")
myDoc = id.make(new=k.document)
myTextFrame = id.make(new=k.text_frame, at=myDoc.pages[1])
myTextFrame.geometric_bounds.set(["6p0","6p0","18p0","18p0"])
myTextFrame.contents.set("HELLO")


>I can run this script and set text frame on ID document. Now the questions are:
>1. we can create MacPython obj say cs = app("Adobe InDesign CS2")
>2. Then from cs we could have myDoc=cs.open(filename)
>    How do we make a new document by using MacPython code??
>    From appscript help, we can get some description like :
> 
>    application.make(...) ---- make new element
>    [new=Type]  ---- The class of the item to be created
>    [at =InsertionRef] ----The location at which to insert the item
>    [with_data=record] --- the initialial data for the item
>    [with_properties=Record] -- Initialial value for the properties of the new object
>    result reference
> 
>    The question is "Do I need fill all 4 paramters in order to create a item?"

You supply the same parameters as you would if you were using AppleScript.


>    On Adobe InDesign manual, there is no make method for application, I wonder if application will accept the commands.

All commands are ultimately handled by the application object. Both AppleScript and appscript allow various bits of syntactic jiggery-pokery to make it look like you're sending a command to other objects, e.g.

	tell application "TextEdit"
		tell document 1
			set text to "hello"
		end tell
	end tell


	app('TextEdit').documents[1].text.set('hello')


but these are merely convenience forms and under the surface both languages translate this to (pseudocode):

	application("TextEdit").set(
		root.elementByIndex('document', 1).property('text'),
		to="hello")

This stuff is explained in the appscript manual, so if you've not read it already then now's a good time to do so. If you have trouble understanding the manual, please let me know what's missing or unclear so I can improve it for the next release.


>3. I can write something like
> 
>     cs.active_document.pages[1].text_frames[1]
> 
>   and execute it without error.
> 
>   But how can one get the information from
>        cs.active_document.pages[1].text_frames[1].contents
>   here cs is app("Adobe InDesign CS2")
> 
> 
>  If I print cs.active_document.pages[1].text_frames[1].contents, it should print out empty string or some string based on content.

No, it won't. This is covered in the manual, and I've just explained it again in the 'get references' thread. You're making incorrect assumptions based on your prior knowledge of how AppleScript and/or Python OOP references work. Regardless of any visual similarity, appscript references are NOT the same thing and they *follow their own, well-defined rules*.

Appscript's rules regarding get() and set() are logical and consistent and work well in practice [1], but you do need to learn those rules in order to use it effectively, otherwise you'll just confuse the heck out of yourself. (FWIW, you're not the first person to make this mistake and you'll no doubt be the last, but I am working on making the documentation clearer about this issue.)

has

p.s. Please keep further discussion of your get() and set() problems with InDesign to the existing 'get references' thread to avoid confusion.

-----

[1] In fact, it's impossible for appscript to do 'implicit gets' a-la AppleScript due to the way the Python interpreter operates. AppleScript manages to pull such tricks only through deep magic that runs throughout the core language, but there's no way I'm hacking Python's bytecode interpreter to emulate it.
-- 
http://freespace.virgin.net/hamish.sanderson/


More information about the Pythonmac-SIG mailing list