Attempting to update iTunes Album Artwork from file

Hey-
I am trying to write a script (that I am going to embed into a cocoa application) that takes an image from a file and loads it into iTunes as the album artwork. I have searched through these forums and pieced together the following script:

tell application "iTunes"
	set currentArtwork to my getImage("/Users/ryanpatrick/Pictures/butterfly_fish.jpg")
	set data of artwork 1 of current track to currentArtwork
end tell

on getImage(pathToImage)
	tell application "Image Events"
		launch
		set thisImage to open file pathToImage
		return thisImage
	end tell
end getImage

The error that I get is the following:
Can’t make <> “butterfly_fish.jpg” of application “Image Events” into the expected type.

I am fairly new to using Applescript but am a programmer by trade. If anyone has a better way of doing this, please let me know…

Thanks in advance.
-Ryan

Hi Ryan,

I don’t have a good solution and don’t have Image Events. Just writing this to give you some info.

When you take a picture and drag or paste it into the artwork, iTunes converts this to PNG and I think adds some other stuff. You can see this by comparing the data of the original picture and the data of the artwork. If you get the kind of the artwork, it shows png. The raw data is surrounded by chevrons and looks something like this:

<<data PICT… a bunch of hex digits here …>>

This is what you set the data of the artwork to. Since I don’t have Image Events, I don’t know if or how you can get this data. To see how this data looks try running the following script on a track that has artwork:

property track_name : “Time Passages”
property track_kind : “MPEG audio file”
(* – to get data from a file as PICT
set the_file to choose file with prompt “Choose a picture file:”
set file_type to file type of (info for the_file)
set d1 to (read the_file as file_type) – raw data
*)
tell application “iTunes”
set lib_pl to first library playlist
set track_ref to first track of lib_pl whose ¬
name is track_name and ¬
kind is track_kind
set artwork_ref to first artwork of track_ref
set d2 to data of artwork_ref
end tell

You can use this data to set the data of artwork of other tracks because it is converted to whatever form it is that iTunes knows. How to get this from the original picture, I don’t know.

My bad solution was by creating a macro type script using System Events, but I think Image Events can do it somehow.

Wish I knew how iTunes converts the artwork.

gl,

You can take a look at my script Get Artwork from Amazon for some tips on getting artwork files into iTunes. This script is free and editable and although it relies on QuickTime Player for the conversion, it could be modified to use Image Events instead.

Jon

Thanks for the help. I will take a look at both of your suggestions.

Jon- its funny that you mention getting album artwork from Amazon, because that is exactly what my application is doing. It does not use Applescript at all to perform this. I just need a way to upload the album art into iTunes and I thought that Applescript would be the easiest way to go…

Thanks again
-Ryan

Hi Jon,

I had tried converting a picture file to picture earlier, but that didn’t do it. It was the stripping of the first 512 bytes! Did Yoel ever tell you why this needs to be done?

The strange part is that when iTunes converts the user picture, it also adds a few bytes at the beginning for certain file (“PICT” I think). Still don’t know why it does that.

Thanks a lot, my mind is back to normal again. Worked almost all day yesterday on this.

gl,

Hi,

Here’s a short script to just get the data from file. No error checking, cleanup, or options:

property track_name : “Time Passages” – change this
property track_kind : “MPEG audio file”
– takes a user picture file and creates or replaces iTunes artwork for an mp3 with name track_name
set the_pic to choose file with prompt “Choose a picture file:”
set desk_path to (path to desktop) as string
set file_spec to (desk_path & “NewPic.pict”) as file specification
tell application “QuickTime Player”
launch
activate
open the_pic
tell front movie
stop
rewind
end tell
export front movie to file_spec as picture
end tell
– read data from byte 513
set d1 to (read file_spec from 513 as “PICT”) – raw data
tell application “iTunes”
set lib_pl to first library playlist
set track_ref to first track of lib_pl whose ¬
name is track_name and ¬
kind is track_kind
set artwork_ref to first artwork of track_ref
set data of artwork_ref to d1
end tell

Thanks again to Jon and Yoel,

I used Image Events to accomplish the same effect, but took a slightly different approach. With Image Events, I save the input image file out to PICT format. I noticed that no other format works except PICT- not sure why. My application uses Obj-C to save an image to file which is then read by applescript. The path to the image is fed through Obj-C. If anyone wants to see the Obj-C code, let me know; I did not think that this was the place for it. Enough talk, here’s some code:


set pathToImage to "/image/path"
tell application "Image Events"
launch
set imageFile to (open pathToImage) 
save imageFile as PICT
end tell
tell application "iTunes"
set currentArtwork to (read file pathToImage from 513 as artwork)
set currentArtist to artist of current track
set currentAlbum to album of current track
set allSongsOnAlbum to (file tracks of library playlist 1 whose (artist is currentArtist and album is currentAlbum))
repeat with nextTrack in allSongsOnAlbum
set data of artwork 1 of nextTrack to currentArtwork
end repeat
end tell

Good pickup on the ‘from 513’ part. I would love an explanation…

-Ryan

Glad I found this thread, I was working on something similar and wound up with the following script that lets the user choose an album and an art file and adds it to the artwork:

set AppleScript's text item delimiters to {} --can mess up the script path if not reset
--load the sort library
set scriptPath to (path to scripts folder from user domain) & "Script Library:"
--uses the quicksort routine from my Nite Flite library
--available on Scriptbuilders
set sortLib to load script ((scriptPath as text) & "sortLib.scpt") as alias
-- get album list from iTunes
set shortAlbumList to {}
tell application "iTunes" to tell (get album of tracks of library playlist 1) to set {albumList, tcount} to {it, count it}
repeat with i from 1 to tcount
	tell my albumList's item i to if it is not in my shortAlbumList and it ≠ "" then set end of my shortAlbumList to it
end repeat
--get an album from the user
set shortAlbumList to sortLib's quickSort(shortAlbumList)
set theAlbum to choose from list shortAlbumList with prompt "Add artwork to what album?" without multiple selections allowed
--Get the artwork file
set theArt to choose file with prompt "Choose art file" without invisibles and multiple selections allowed
--convert the image
tell application "Image Events"
	launch
	set imageFile to (open theArt)
	set theArt to (theArt as text) & ".PICT"
	save imageFile as PICT in theArt
end tell
--add the artwork to the album
tell application "iTunes"
	set theArtwork to (read file theArt from 513 as artwork)
	repeat with myTrack in (every file track of the library playlist 1 whose name is theAlbum)
		set data of artwork 1 of myTrack to theArtwork
	end repeat
end tell

I wanna, I wanna, . :wink: :smiley: