Artwork Assistance

GO TO ADMIN PANEL > ADD-ONS AND INSTALL VERTIFORO SIDEBAR TO SEE FORUMS AND SIDEBAR

mj_1903

New member
Joined
Aug 5, 2003
Messages
22
Points
0
Well, my skills in Applescript are now a little better. Thanks everyone for the help with the strings, Cocoa and Applescript do work nicely together, but in that instance Cocoa managed to forget what an array was...even though its a programmed function. Whoops?

Anyways, I have been doing stuff like retrieving artwork from iTunes and saving it to disk so my Cocoa apps can handle it. I was wondering, how would you go about using Applescript to add artwork to tracks?

For instance, I have my Cocoa app reading data from Amazon and saving the resulting artwork cover to the hard drive. I know that I have to set this image to a data variable and then tell iTunes to add that artwork for the selected song....just wondering how to go about the first part...

Also, the file is in TIFF format, would this be an issue with iTunes at all? I know I retrieve files as pict from iTunes when saving to disk.

Thanks again for your help,

Mat
 

WoodenBrain

New member
Joined
Jun 8, 2005
Messages
245
Points
0
Website
beam.to
Looking for some canned open source code of this nature -- get artwork from amazon, save to mp3 file. surely someone's gotta have this canned by now.
 

WoodenBrain

New member
Joined
Jun 8, 2005
Messages
245
Points
0
Website
beam.to
i know i can piece something together from here and there, i just want something canned that get's one and only one matching image from somewhere on the internet, preferably amazon, but i guess if it were reliable then anywhere. i don't want it to open in safari or anything of the sort. i just want a url to an image, and a routine to make the image into picture data.

don't wanna spend forever on it!
 

deeg

New member
Joined
Jul 1, 2003
Messages
394
Points
0
Location
Swampland, UK
well what do you know... spent last night coding something up.. got as far as retrieving the image urls via Amazon Web services and started hunting for an xml parser.. anyway, while surfing i came across a link for XMLTools.. which inturn linked to "Get Amazon Pix" available here...

http://www.dougscripts.com/itunes/scripts/scripts11.php?page=1#getamazonpix

does the entire net end up back at Doug's site ? :confused:

have not tried Greg's script but it appears to do what you want and more so..
 

awmajic

New member
Joined
Jun 14, 2005
Messages
24
Points
0
Location
United Kingdom
I just get all of my artwork by right clicking and saving picture as from play.com and if i cant find it there i use google image. Simple enough for me.
 

WoodenBrain

New member
Joined
Jun 8, 2005
Messages
245
Points
0
Website
beam.to
Deeg, thanks a lot. Somehow I missed that, I was searching for "art" and "artwork" not "pix" or "amazon" on doug's site. I knew there was something out there. unfortunately, it's a lot more complicated than I thought it would be-- I need it to be fast. I'm trying to put it into the new <a href="http://www.versiontracker.com/dyn/moreinfo/macosx/27109">salling clicker controller I just released version 1.0 of.</a>. It can only take a 3 or 4 seconds to grab a picture or it would be useless. perhaps i can find a way to cache pictures for upcoming tracks. Anyway that's a good start. Thanks.

(It does appear to be outdated though--the developer tolken thing is no longer called that--they appear to be called associates or something. anyone know anything about that? i hate having to read legaleese. if i'm developing an application can i use my developer tolken for what i distribute? it looks like most scripts and such require you to sign up for your own developer tolken).
 
Last edited by a moderator:

deeg

New member
Joined
Jul 1, 2003
Messages
394
Points
0
Location
Swampland, UK
I am continuing with my coding, I'm using a curl'd REST query rather than a SOAP call that Greg uses.. don't know enough about either method to say which one is faster but REST looked easier :) Also trying to avoid the osax requirement if possible. I'll post back with an example for you to play with.. If this is ending up being used in Salling would you prefer the small image being retrieved rather than the larger ones if they exist ? the small ones are 75x75, medium are 160x160, large 300x300 ?

i think you can use your dev id in your app.. i did not read the legalise either but i have not had to get a dev id for other apps i have tried such as "iTunes catalog".
 

WoodenBrain

New member
Joined
Jun 8, 2005
Messages
245
Points
0
Website
beam.to
deeg said:
If this is ending up being used in Salling would you prefer the small image being retrieved rather than the larger ones if they exist ? the small ones are 75x75, medium are 160x160, large 300x300 ?


hmm i'm not sure about that. definitely the small one for the purposes of sending it to the device and network speed, but i was also thinking about the option of copying to the track and / or the album tags (by command), in which case the 300 size ones better. actually over bluetooth the size wouldn't matter much. the network connection is probably the limiting factor. wouldn't it be a matter of changing a line in the code? for now i'd include all 3 options and comment out the others.

have you checked out my salling controller yet? it already uses the cache files for Clutter and iTunes Catalog, so right now i'm thinking if people want more artwork on their bluetooth device, run clutter or ITC. but if you/me can do it super fast, maybe do it via scripting.

thaks for the effort, deeg.
 

deeg

New member
Joined
Jul 1, 2003
Messages
394
Points
0
Location
Swampland, UK
heres something to play with... not finished yet, itunes is throwing a -116 error when i try to add the artwork to it...note it currently creates an image file on the desktop for each retrieved image.. am looking into keeping it all internal to the script or if not you can change the default path to the Application Helper file in ~/library or some such.. you'll need to add your aws subscription id where specified...yet to add the store location selector for various regional amazon stores..

Code:
[size=1]
global theCurrentTrack

tell application "iTunes"
	set theCurrentTrack to current track
	tell theCurrentTrack to copy {artist, album} to {theArtist, theTitle}
end tell
my amazon_Search(theArtist, theTitle)

-- do a rest requet via curl to Amazon Web services for artist and Album of playing track
on amazon_Search(theiTunesArtist, theiTunesTitle)
	-- construct theArtist
	set theArtist to my encode_text(theiTunesArtist, true, true)
	
	--construct the title
	set theTitle to my encode_text(theiTunesTitle, true, true)
	
	-- Define parts of the REST request.
	set theSubscriptionID to "INSERT YOUR SUBSCRIPTION ID HERE" -- (note: add  an '&' to end of id)
	
	-- Assemble the REST request URL.
	
	set theRequest to "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=" & theSubscriptionID &  ¬ 
"Operation=ItemSearch" & "&Artist=" & theArtist & "&Title=" & theTitle & "&SearchIndex=Music&ResponseGroup=Images" as string
	
	-- do the REST request via curl
	
	set theResults to (do shell script "curl \"" & theRequest & "\" ") as Unicode text
	
	-- Check the results
	
	my process_the_Results(theResults, theiTunesArtist, theiTunesTitle)
	
end amazon_Search

-- subroutine for the results of the REST request it returns the FIRST small image, Medium Image, Large image URLS

on process_the_Results(theResults, theiTunesArtist, theiTunesTitle)
	if theResults contains "We did not find any matches for your request" then
		beep
	else
		set ourURLs to my xml_strip(theResults)
		if item 1 of ourURLs is not "" then
			set theSMallImage to my Download_the_image(item 1 of ourURLs, "SmallImage")
		end if
		if item 2 of ourURLs is not "" then
			set theMediumImage to my Download_the_image(item 2 of ourURLs, "MediumImage")
		end if
		if item 3 of ourURLs is not "" then
			set theLargeImage to my Download_the_image(item 3 of ourURLs, "largeImage")
			set theImage to read theLargeImage as picture
			tell application "iTunes"
				if (count of artwork of theCurrentTrack) is 0 then
					delay 5
					tell theCurrentTrack
						set data of artwork 1 to theImage
					end tell
				end if
			end tell
		end if
	end if
end process_the_Results

-- subroutine to download the image to a holder...

on Download_the_image(theURL, theImageType)
	set theFile to ((path to desktop) & theImageType & ".jpg") as string
	tell application "Finder"
		if file theFile exists then
			set oldFile to true
		else
			set oldFile to false
		end if
	end tell
	tell application "URL Access Scripting"
		activate
		set dlURL to theURL
		try
			if oldFile = true then
				set x to download dlURL to file theFile replacing yes with progress
			else
				set x to download dlURL to file theFile with progress
			end if
		on error
			set theFile to ""
		end try
		try
			quit
		end try
	end tell
	
	return theFile as alias
end Download_the_image

-- subroutine to process returns xml.. looking for SMALL,MEDIUM,LARGE image  would be 
--nice to use System Events but that appear to work on files only not variables...
--or i could not get it to work on a variable.. dont want to use an oasx either...

on xml_strip(the_xml)
	set the_xml to the_xml as string
	set theSmallURL to ""
	set theMediumURL to ""
	set theLargeURL to ""
	set AppleScript's text item delimiters to return
	repeat with b from 1 to count of text items of the_xml
		set thisLine to text item b of the_xml
		if thisLine contains "<SmallImage>" then
			set theResult to my get_URL(thisLine, "<SmallImage><URL>", "</URL><Height>75")
			if theResult is not "" then
				set theSmallURL to theResult
				set theResult to ""
			end if
		end if
		if thisLine contains "<MediumImage>" then
			set theResult to my get_URL(thisLine, "<MediumImage><URL>", "</URL><Height>160")
			if theResult is not "" then
				set theMediumURL to theResult
				set theResult to ""
			end if
		end if
		if thisLine contains "<LargeImage>" then
			set theResult to my get_URL(thisLine, "<LargeImage><URL>", "</URL><Height>300")
			if theResult is not "" then
				set theLargeURL to theResult
				set theResult to ""
			end if
		end if
	end repeat
	set AppleScript's text item delimiters to {""}
	return {theSmallURL, theMediumURL, theLargeURL}
end xml_strip

-- Subroutine to strip data from a string

on get_URL(the_string, start_delim, end_delim)
	set offset_start to (offset of start_delim in the_string) + (count of characters of start_delim)
	set offset_end to (offset of end_delim in the_string) - 1
	return text offset_start thru offset_end of the_string
end get_URL


-- this sub-routine is used to encode a character and is available at [url]http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.10.htm[/url]
on encode_char(this_char)
	set the ASCII_num to (the ASCII number this_char)
	set the hex_list to ¬
		{"0", "1", "2", "3", "4", "5", "6", "7", "8", ¬
			"9", "A", "B", "C", "D", "E", "F"}
	set x to item ((ASCII_num div 16) + 1) of the hex_list
	set y to item ((ASCII_num mod 16) + 1) of the hex_list
	return ("%" & x & y) as string
end encode_char

-- this sub-routine is used to encode a text string and is available at [url]http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.10.htm[/url]

on encode_text(this_text, encode_URL_A, encode_URL_B)
	set the standard_characters to ¬
		"abcdefghijklmnopqrstuvwxyz0123456789"
	set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
	set the URL_B_chars to ".-_:"
	set the acceptable_characters to the standard_characters
	if encode_URL_A is false then ¬
		set the acceptable_characters to ¬
			the acceptable_characters & the URL_A_chars
	if encode_URL_B is false then ¬
		set the acceptable_characters to ¬
			the acceptable_characters & the URL_B_chars
	set the encoded_text to ""
	repeat with this_char in this_text
		if this_char is in the acceptable_characters then
			set the encoded_text to ¬
				(the encoded_text & this_char)
		else
			set the encoded_text to ¬
				(the encoded_text & encode_char(this_char)) as string
		end if
	end repeat
	return the encoded_text
end encode_text
[/size]
 
Last edited:

WoodenBrain

New member
Joined
Jun 8, 2005
Messages
245
Points
0
Website
beam.to
hey what happened to the normal post rely button, and to the edit buttons? sorry for 3 posts in a row.

indeed, the following works. i wish saving as a pict first could be avoided though. It takes about 10 seconds. I also have to think of a way around that AWS problem. What a pain to make people have to sign up. Either I have to use my own in distribution, or have to make a pref file or something.

if item 3 of ourURLs is not "" then
set theLargeImage to my Download_the_image(item 3 of ourURLs, "largeImage")
tell application "Image Events"
launch
set the_image to open theLargeImage
save the_image as PICT in ((path to desktop) & "largeImage" & ".pict" as string)
close the_image
end tell
tell application "iTunes"
set the_artwork to read alias ((path to desktop) & "largeImage" & ".pict" as string) from 513 as picture
set data of artwork 1 of current track to the_artwork
end tell
set theImage to read theLargeImage from 513 as picture
tell application "iTunes"
if (count of artwork of theCurrentTrack) is 0 then
delay 5
tell theCurrentTrack
--set data of artwork 1 to (theImage as picture)
--read (alias this_image) as picture
set data of artwork 1 to theImage
end tell
end if
end tell
end if
 

WoodenBrain

New member
Joined
Jun 8, 2005
Messages
245
Points
0
Website
beam.to
here's #4.

I noticed in the source code for a sloth radio search there are direct URLs to amazon. I wonder if there is a good way to parse any of this? that way the whole AWS problem could be avoided.

<!-- RESULT LIST START -->

<!-- RESULT ITEM START -->
<table class="albumbox2"><tr><td width="305" valign="top">No image found. <a href="http://images.google.com/images?q=Fermin+Muguruza+Brigdistak+Sound+System">Google for "Brigdistak Sound System".</a><img src="" width="1" height="1" /></td><td valign="top"><table><tr><td align="right"><b>Artist:</b></td><td>Fermin Muguruza</td></tr><tr><td align="right"><b>Album:</b></td><td>Brigdistak Sound System</td></tr><tr><td align="right"><b>Year: </b></td><td>2002</td></tr><tr><td align="right"><b>Used: </b></td><td>$9.99</td></tr></table><br /><center><a href="http://www.amazon.com/exec/obidos/ASIN/B00005O84Y/ivorytower-20?dev-t=D2AAJ98P9F66IB%26camp=2025%26link_code=xm2"><img src="buy5.gif" width="120" height="28" border="0" /></a></center></td></tr></table>
<!-- RESULT ITEM END -->

<!-- RESULT ITEM START -->
<table class="albumbox2"><tr><td width="305" valign="top"><img src="http://images.amazon.com/images/P/B000063695.01.LZZZZZZZ.jpg" width="300" height="294" /></td><td valign="top"><table><tr><td align="right"><b>Artist:</b></td><td>FermÃ
 

WoodenBrain

New member
Joined
Jun 8, 2005
Messages
245
Points
0
Website
beam.to
and one more thing...

take a look at the "AlbumArt" tiger widget. inside the package there is a java script file called AlbumArtAmazon. In that there's a nice juicey AWS ID.

Still the better "legal" way would be to parse those Sloth Radio results. The problem I see right off is all the " (quotes). How do you even get that as a string in AppleScript with all the quotes in there?
 

deeg

New member
Joined
Jul 1, 2003
Messages
394
Points
0
Location
Swampland, UK
no worries about the number of posts..

the edit/post buttons got pushed to the right due to my comments in the code, have altered the long one to three lines to bring the forum buttons back on screen..

thanks for the link to the fix for -116 error.. noticed in your code that you appear to add the image twice.. is that intended or an error..

interesting about the sloth radio thing... what's the benefit of going to them and searching compared to AWS itself ? is it to avoid the AWS subscription ? i think you can just include your own subscription id in the App.. it would be pointless of Amazon to require every end user of any web service to get their own personal subscription.
if you do go the slot radio way could you not just read the whole source in, the same way Doug does in his http://www.dougscripts.com/itunes/scripts/scripts11.php?page=1#cddbsafarikit ?

also since we have the pict image on the Mac already, the delay 5 can be removed from the section that add's the artwork to the track..from running it a few times, most of the delay appears to be iTunes adding the artwork rather than Image events converting the file so i think you would have the delay regardless of the format you obtain/download the file in..Do you also get stuttering playback in itunes when adding the image ?
 
Last edited:

WoodenBrain

New member
Joined
Jun 8, 2005
Messages
245
Points
0
Website
beam.to
what do you mean add the image twice? unless you include the lines I was trying that I commented out there.

what's irritating is you have download and save to disk as a jpg, read it back, write it back to a pict, read the pict, and finally write (a portion of the pict) out to the id3 tags.

it should be possible to do all, or almost all, of that in memory, don't you think?

regarding the sloth radio, the benefit is indeed to avoid the subscription. I'm not so sure that it would be within the terms of the AWS license to use their images in this way. Clearly they are aiming to get people to put their content on their web pages for click-through sales. Now whether it is technically illegal or not, or violates their license, or whatever, I don't know without a lawyer. But I did scan the agreement enough to get that "sense". I'm sure a lot of people use it for "other purposes" but I don't know what it would take to get them to notice. My guess is it would depend on how "popular" your application (that you included your AWS tolkein in) became.

Thus, I think if possible to bypass that it would be better. Nothing says you can't access their publicly accessable URLs, no matter how you get them.

ANyway, I don't intend to tear my hair out over it, but I took a couple of brief stabs--so far unsuccessful at parsing the sloth stuff.

reading the source does not seem to work unless you download the URL and then open it in safari first (getting the source from the document window and not the file).

if you use url access scripting to download the file, then I thought you could use read ... using delimiters to get the records, then it would be a simple matter of getting the first record that had "amazon" in it and extracting the URL (don't know how to do that either... just wanted to get the record first...)

but for some reason, this doesn't do anything like what I expect:
(to test, download the results of a slothradio seach and save on desktop with name "sloth.html" (as .html source of course)

set tempdir to (path to desktop) as string
set theFile to open for access (tempdir & "sloth.html") as alias
set therecs to read theFile using delimiter {("<!-- RESULT ITEM START -->" & return), ("<!-- RESULT ITEM END -->" & return)}
--{"RESULT ITEM START", "RESULT ITEM END"}
--{"start", "end"}
close access theFile

return therecs

as for the stuttering playback, yes, if you add art to a file playing, or for that matter even change the ID3 tags, it will tend to do that.

For that reason it might be prudent to get the index of the track, and do the operation of the next track to play.

my idea at this point is to do something like that and save the image in either a) a cache file location compatible with "Clutter" (whose cache files my salling clicker app is already using), or b) the album folder. I'm inclined to do the latter, but I don't know how many people would object to having a script populate their music folders with .jpgs. for me it would be desirable and would save more space in the end than adding artwork.

at some point i need to post my geek tool script that puts track info and a picture on the desktop, but it is sort of a pro-user sorta thing, but works really well for me.

PS: anyone know off hand how to test whether we have an internet connection and how fast it is?

PPS: my console log is showing URL Access Scripting repeatedly crashing when running this script (or my version of it which is almost the same -- I think I commented out the Try..quit..end try part in that routine; why would you want to quit URL Access Scripting each call?)

PPPS:

there's are a couple of threads about artwork going on on macscripter:

http://bbs.applescript.net/viewtopic.php?id=13020
 
Last edited:

deeg

New member
Joined
Jul 1, 2003
Messages
394
Points
0
Location
Swampland, UK
sorry, it was not setting the image but reading it in..

this will get the image url from sloths results, the search URL is constructed as per doug's
http://www.dougscripts.com/itunes/scripts/scripts11.php?page=1#searchslothradio

Code:
[size=1]
to open_location(theURL)
	set theResults to (do shell script "curl \"" & theURL & "\" ") as Unicode text
	my parse_the_results(theResults)
end open_location

on parse_the_results(theResults)
	set theResults to theResults as string
	set AppleScript's text item delimiters to return
	repeat with b from 1 to count of text items of theResults
		set thisLine to text item b of theResults
		if thisLine contains "//images.amazon.com" then
			set theResult to my get_URL(thisLine, "width=\"305\" valign=\"top\"><img src=\"", " width=\"300\" height=\"300\" />")
		end if
	end repeat
	set AppleScript's text item delimiters to {""}
	log (theResult)
	
end parse_the_results

-- Subroutine to strip data from a string

on get_URL(the_string, start_delim, end_delim)
	set offset_start to (offset of start_delim in the_string) + (count of characters of start_delim)
	set offset_end to (offset of end_delim in the_string) - 2
	return text offset_start thru offset_end of the_string
end get_URL
[/size]
 
Last edited:
Top