• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. AppleScript for iTunes > 86. Save Artwork to a Folder

Save Artwork to a Folder

Copy the album artwork of a selected track as aJPEG or PNG file to a specified folder.

This hack shows how to export the artwork from an iTunes track as a graphic file and save it to a selected folder, so you can start stockpiling your artwork for safekeeping or for use in other applications.

The Code

This script exports the artwork of the single selected track as a graphic file to a user-specified folder:

tell application "iTunes"
  -- get a reference to selected track
  if selection is not {} and (count items of selection) is 1 then
    set theTrack to item 1 of selection
else
  my alert_user_and_cancel("Please select one track.")
end if

-- does selected track have artwork?
if class of theTrack is not file track or ¬
  artworks of theTrack is {} then
  my alert_user_and_cancel("Selected track has no Artwork.")
end if

-- choose folder to save file to
set artFolder to (choose folder with prompt ¬
  "Where do you want to save Artwork?") as string

-- get data and format of track's artwork
try 
  set artData to (data of artwork 1 of theTrack) as picture 
  set artFormat to (format of artwork 1 of theTrack) as string

if artFormat contains "JPEG" then
  set extension to ".jpg"
else if artFormat contains "PNG" then
  set extension to ".png"
end if

-- create name for new file
set theName to (artist of theTrack & " - " & album of theTrack)

-- create paths for files
set tempArtFile to (artFolder & "temp" & extension) as string
set finalArtFile to (artFolder & theName & extension) as string

-- make the temp file
set fileRef to (open for access tempArtFile write permission 1)
write artData starting at 0 to fileRef as picture
close access fileRef

-- shell script copies vitals from temp file to final file,
-- removes temp file
do shell script "cd " & ¬
  quoted form of (POSIX path of artFolder) & ¬
  ";tail -c+223 " & ¬
  quoted form of ("temp" & extension) & "> " & ¬
  quoted form of (theName & extension) & ";rm " & ¬
  quoted form of ("temp" & extension)

 -- finished
if frontmost then
  if button returned of (display dialog ¬
    "Artwork exported" buttons {"Show File", "Thanks"} ¬
	default button 2) is not "Thanks" then
	tell application "Finder"
      reveal file finalArtFile
      activate
    end tell
  end if
end if

-- report any error and cancel
on error errM
  close access fileRef
  my alert_user_and_cancel("Unable to export track's Artwork." & ¬
    return & return & errM)
end try
end tell

to alert_user_and_cancel(message)
tell application "iTunes" to display dialog message buttons {"Cancel"} ¬ 
  default button 1 with icon 0
end alert_user_and_cancel

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint