iTunes Artwork

i know that there are probably a million threads about this, but i already have an example of how to extract artwork. I want to know what i did wrong. When ever i run this script, all it does is write a blank text file, not a picture.

here is my code

tell application "iTunes"
	set trackthing to current track
	set theArt to (data of artwork 1 of trackthing) as picture
	
	set tempartwork to ("Macintosh HD:Users:tapman90:Desktop:artwork:" & "thing.tiff") as string
	set file_reference to (open for access tempartwork write permission 1)
	write theArt starting at 0 to file_reference as TIFF picture
	close access file_reference
end tell

p.s. i no that my code needs the shell script rm thing to remove the last written file when writing a new one.

tell application "iTunes"
	set trackthing to current track
	set theArt to (data of artwork 1 of trackthing)
	
	set tempartwork to ("Macintosh HD:Users:tapman90:Desktop:artwork:thing.tiff" as string)
	set file_reference to (open for access tempartwork write permission 1)
	write theArt starting at 0 to file_reference as TIFF picture
	close access file_reference
end tell

this returns the error “iTunes got an error: File file Macintosh HD:Users:tapman90:Desktop:artwork:thing.tiff is already open.”

why?

Hi,

Type this in the Script Editor:

set f to choose file
close access f

Run it and choose your tiff file that was left open because there probably was an error.

About your script. Care should be taken when using the Standard Additions read/write commands (especially write). This part:

 set tempartwork to ("Macintosh HD:Users:tapman90:Desktop:artwork:thing.tiff" as string)
 set file_reference to (open for access tempartwork write permission 1)
 write theArt starting at 0 to file_reference as TIFF picture
 close access file_reference

should be kept out of the iTunes tell block. Nothing may go wrong, but with some apps it might.

You should use an error handler. Something like this:

set fs to (“” & (path to desktop) & “My1.tiff”) as file specification
set pd to “hello”
set rn to (open for access fs with write permission)
try
write pd to rn
close access rn
on error err_mess
close access rn
error err_mess
end try
tell application “Finder” to activate

Instead of the script quitting with an error when you’re writing, it jumps to the error handler and closes the file.

In your original script, AppleScript the data is in “PICT” format. The Standard Additions ‘write’ can’t convert “PICT” to “TIFF”. All it does is label the data as some type of data. You need to use another application or scripting addition to convert to tiff or you can write to file as pict.

Later,

thxs for helping, but it still only writes a text document:

tell application "iTunes"
	set trackthing to current track
	set theArt to (data of artwork 1 of trackthing)
end tell

set tempartwork to ("" & "Macintosh HD:Users:tapman90:Desktop:artwork:" & "thing.pict") as file specification
set file_reference to (open for access tempartwork write permission 1)
try
	write theArt to file_reference as picture
	close access file_reference
on error err
	close access file_reference
	error err
end try
tell application "Finder" to activate

anyone know why?

thxs for help,
tapman90

Hi tapman,

The script I’m using changes the file type and creator. There’s something not right with the created file though. It opens alright with Preview (my default app for PICT), but something is missing in the data. I think the original iTunes data is missing something. Here’s the script:

tell application “iTunes”
set s to item 1 of (selection)
set pd to data of first artwork of s
end tell
set fs to (“” & (path to desktop) & “MyArt.pict”) as file specification
set rn to (open for access fs with write permission)
try
write pd to rn
close access rn
on error err_mess
close access rn
error err_mess
end try

tell application “Finder”
set file type of fs to “PICT”
set creator type of fs to “???”
activate
end tell

I’m still thinking about what’s wrong with the data. Maybe it has something to do with iTunes stripping the first 512 bytes.

Later,

thxs for trying, why does your script, tunr the file in to an actual .pict file, while mine just makes a text file?

tell application "iTunes"
	set trackthing to item 1 of (selection)
	set theArt to data of artwork 1 of trackthing
end tell
set tempartwork to ("" & (path to desktop) & "thing.pict") as file specification
set file_reference to (open for access tempartwork with write permission)
try
	write theArt to file_reference
	close access file_reference
on error err
	close access file_reference
	error err
end try

tell application "Finder"
	set file type of file_reference to "PICT"
	set creator type of file_reference to "????"
	activate
end tell

^doesn’t make .pict file, but text file

v does make .pict file

tell application "iTunes"
	set s to item 1 of (selection)
	set pd to data of first artwork of s
end tell
set fs to ("" & (path to desktop) & "MyArt.pict") as file specification
set rn to (open for access fs with write permission)
try
	write pd to rn
	close access rn
on error err_mess
	close access rn
	error err_mess
end try

tell application "Finder"
	set file type of fs to "PICT"
	set creator type of fs to "????"
	activate
end tell

btw: what is a creator type?

Hi tapman,

I’ve been doing some research and found that the first 512 bytes should be a header that is used by specific applications. What I did was just add a blank 512 byte header and now more can be done with the PICT file then just viewing. Here’s the script:

tell application “iTunes”
set s to item 1 of (selection)
set pd to data of first artwork of s
end tell
set fs to (“” & (path to desktop) & “MyArt.pict”) as file specification
set rn to (open for access fs with write permission)
try
set eof rn to 512
write pd to rn starting at 513
close access rn
on error err_mess
close access rn
error err_mess
end try

tell application “Finder”
set file type of fs to “PICT”
set creator type of fs to “???”
activate
end tell

It takes the selected iTunes track’s artwork and writes it out to file. From there you can convert the file using some scriptable app or addition.

The creator type or file creator is the four character code something like the file type. By looking at the code, the system knows what app to use to open the file. Apple has their own reserved ccreator codes and developers can regester their own codes. The “???” just tells the system to use the default app for that file type.

About your script not turning the file to pict, right off the bat I can’t see why.

Later,

yey! thxs for helping!

i have 3 questions though:

  1. Where did u find out the first 512 megabytes being the header?

  2. what is “eof” ?

  3. is it possible to write this image inside an application?

Hi tapman,

  1. Where did u find out the first 512 megabytes being the header?

From a previous post I knew that stripping the first 512 bytes of pict data allows insertion of the remaining data into a tracks artwork data. In that post Jon suggested, to the poster, trying his script that takes artwork from somewhere and using it in iTunes. I read the script and someone told Jon to do the stripping, although we didn’t know why that needed to be done. Now, assuming that the data taken from iTunes is missing the 512 bytes, I searched for “PICT” and didn’t find specifics about the data. I remembered writing some quickdraw 3dmf from pre-osx, so searched for quickdraw and found a lot of info on the header mixed with the bitmapping and pict resource. This mixed with the fact that I couldn’t convert the file without the header made me suspect that I had to add one.

  1. what is “eof” ?

the eof (end of file) marker is a pointer to the last byte of a file. It is one of the Standard Additions read/write commands. When AppleScript first started, the commands were separate scripting additions, but now they are parts of the File read/write commands. You can find these commands in the dictionary for Standard Additions. There is further documentation in the ScriptingAdditions.pdf manual.

  1. is it possible to write this image inside an application?

I haven’t tried other apps yet, but I know it works with Preview. Preview isn’t scriptable, so it might be better to use one of the others. Fell asleep last night, but if you want to try there’s QuickTime, Image Capture Scripting, iMagine Photo, and Image Events (post-Jaguar I don’t have this). You probably have all of these except iMagine Photo (freeware?). I might finish the script when I have time.

Later,

Hi tapman,

I’ve been looking through those apps I mentioned to convert the pict file to tiff and it seems the only scriptable app I have that can do that is AppleWorks. You might want to start a new thread on how to convert pict to tiff.

gl,

where is this pdf?

what i meant, is i’m making an app. Is it possible to write the picture in the ‘Resources’ folder of my app, and then display the picture? if so, what would the file path be?

thxs for help!

You could use Image Events:

set thePath to (((path to desktop) as text) & "albumArt.pict") as file specification

tell application "iTunes" to set theArt to data of first artwork of item 1 of (selection)

set fileRef to (open for access thePath with write permission)
try
	set eof fileRef to 512
	write theArt to fileRef starting at 513
	close access fileRef
on error errorMsg
	try
		close access fileRef
	end try
	error errorMsg
end try

tell application "Finder" to set creator type of thePath to "????"

tell application "Image Events"
	set theImage to open thePath
	save theImage as TIFF in (((path to desktop) as text) & "albumArt.tiff")
end tell

Model: Mac mini
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)

that anwsers one of my questions!

but is still need to know:

  1. is it possible to write this image inside an application?

what i meant, is i’m making an app. Is it possible to write the picture in the ‘Resources’ folder of my app, and then display the picture? if so, what would the file path be?

thxs for help!

Hi tapman,

Oh! That’s what you meant by that. It’s easy to write inside a package. You need to use the .app extension for the package. The following will write a blank file in the Script Editor’s Resources folder:

set file_spec to ¬
(“Macintosh HD:Applications:AppleScript:Script Editor.app:Contents:Resources:MyFile”) as file specification
set ref_num to (open for access file_spec)
close access ref_num
tell application “Finder”
activate
reveal file_spec
end tell

gl,

so if i put this script in the resources of my application, is there a code to get the file path to the script, so i can write the picture in the same directory as the script?

thxs for help!

so if i put this script in the resources of my application, is there a code to get the file path to the script, so i can write the picture in the same directory as the script?

thxs for help!

Hi tapman,

You can use the ‘path to’ command to get a reference to your app. I already got rid of the other example so used a TextEdit file “MyFile.txt” located in the “Resources” folder. Something like this:

set app_path to (path to application “Script Editor”) as string
set file_ref to (app_path & “Contents:Resources:” & “MyFile.txt”) as alias
tell application “Finder”
open file_ref
end tell

Note that if your app is not running when you use the ‘path to app “AppName”’, then it will probably launch. I think the app needs to be in one of the Applications folders, but maybe not.

gl,

thxs, now i’m trying to make a script that will remove the file after writing it.

i have script for it, but how do i convert a file path from:

Macintosh HD:Users:tapman90:Desktop:

to

Macintosh HD/Users/tapman90/Desktop/

btw i’m using:

do shell script "rm ~/Desktop/thing.pict"
set thepath to "Macintosh HD:Users:tapman90:Desktop:"
set theposixpath to (POSIX path of (thepath as alias)) as string
do shell script "rm " & theposixpath & "thing.pict"

thxs! so i got that working, kinda.

Problem: the first time i click the button, the artwork loads. Then after the song changes, i do click it again to update it, but it doesn’t update it

why?
thxs for all of your help, this is my first exploration into the wonderful world of shell scripts and writing files

on mouse up theObject event theEvent
	set tempartwork to ("" & (path to application "AtomTunes") & "Contents:Resources:" & "thing.pict") as file specification
	set tempartwork1 to ("Macintosh HD:" & (path to application "AtomTunes") & "Contents:Resources:" & "thing.pict" as string)
	try
		set theposixpath to (POSIX path of tempartwork1)
		do shell script "rm " & theposixpath
	end try
	tell application "iTunes"
		set trackthing to current track
		set theArt to data of artwork 1 of trackthing
	end tell
	set file_reference to (open for access tempartwork with write permission)
	try
		set eof file_reference to 512
		write theArt to file_reference starting at 513
		close access file_reference
	on error err
		close access file_reference
		error err
	end try
	
	
	set image of image view "update" of window "artworkWindow" to load image "Update"
	set image of image view "artworkP" of window "artworkWindow" to load image "thing"
	
	
end mouse up