views:

251

answers:

2

I'm trying to capture artwork from a pict file and embed into a track on iTunes using python appscript. I did something like this:

imFile = open('/Users/kartikaiyer/temp.pict','r')
data = imFile.read()
it = app('iTunes')
sel = it.current_track.get()
sel.artworks[0].data_.set(data[513:])

I get an error OSERROR: -1731 MESSAGE: Unknown object

Similar applescript code looks like this:

tell application "iTunes"
    set the_artwork to read (POSIX file "/Users/kartikaiyer/temp.pict") from 513 as picture
    set data of artwork 1 of current track to the_artwork
end tell

I tried using ASTranslate but it never instantiates 'the_artwork' and then throws an error when there is a reference to the_artwork. Can anyone help. I'm new to appscript and python in general.

A: 

At a quick guess, Appscript references, like AppleScript references, use 1-indexing, not zero-indexing like Python lists. So you probably need to write:

it.current_track.artworks[1].data_.set(...)

(Incidentally, the extra get command in your original script is unnecessary, though harmless in this case.)

As for ASTranslate, you need to enable the 'Send events to app' checkbox if you want it to actually send commands to applications and scripting additions and receive their results. As a rule, it's best to disable this option so that you don't have any unfortunate accidents when translating potentially destructive commands such as set or delete, so only to enable it if you really need it, and be careful what code you run when you do.

has
Appscript uses zero-indexing because it's Python, not AppleScript.
wbg
Appscript uses one-indexing because that's what the Apple Event Manager uses. Appscript code may look Pythonic, but the resemblance is quite superficial - just a thin layer of syntactic sugar around the Apple Event Manager APIs, which determine the actual semantics.
has
A: 

The read command is part of Scripting Additions, which ASTranslate doesn't translate to. Use ASDictionary to create a glue for Scripting Additions, by clicking "Choose Installed Scripting Additions" Under the Dictionary menu, and then selecting "Scripting Additions" from the list.

demonslayer319