Search code examples
pythonmacossourceforge-appscriptpy-appscript

python appscript module - creating k. type objects


I'm learning and enjoying the appscript module, but I'm a little confused about how to instantiate basic k. type objects. for example, if I want to create a variable that holds a k.boolean value to use while scripting an application, how do I create it, and then pass it to the set() method of a property within that application?

let's say I'm scripting Adobe Illustrator:

il = app('Adobe Illustrator')
doc = il.current_document.get()
layers = doc.layers.get()
layer = layers[1]

in Illustrator, a layer object has a property layer.visible, which has a k.boolean value.

how do I create a variable m which is a k.boolean type, such that:

layer.visible.set(m)

will set the .visible property to a different k.boolean value?

m = k.boolean(True) # doesn't work
m = make(new k.boolean) # doesn't work

Solution

  • Appscript will perform casts from Python types to the Apple Event types internally, so you can use a normal Python bool variable:

    Make the layer visible:

    flag = True
    layer.visible.set(flag)
    

    Toggle the layer on/off:

    flag = not layer.visible.get()
    layer.visible.set(flag)
    

    The Python type -> AE type mapping can be found here.