"Unknown object" error when trying to capture artwork from a pict file and embed it into a track

953 Views Asked by At

I'm trying to capture artwork from a pict file and embed it 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.

3

There are 3 best solutions below

2
On

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.

1
On

This is an older question, but since I was having trouble doing this same thing now, I thought I'd post my solution in case someone else might benefit.

selected = appscript.app('iTunes').selection.get()

for t in selected:
    myArt = open(/path/to/image.jpg,'r')
    data = myArt.read()
    t.artworks[1].data_.set(data) # no need to remove header but one-indexed as has said earlier
    myArt.close()

Hope this helps.

0
On

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.