Searchin Apple Music Store

Here’s a script for searching the Apple Music Store.

Sal

property allowed_chars : {"$", "-", "_", ".", "+", "!", "*", "'", "(", ")", ",", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

set this_URL to my create_searchURL("Dr. Feelgood", "Aretha Franklin", "", "", "")
tell application "iTunes"
	activate
	open location this_URL
end tell

on create_searchURL(track_name, artist_name, album_name, composer_name, genre_name)
	set target_URL to "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?"
	if the track_name is not "" then
		set the track_name to my encode_item(track_name)
		set the target_URL to target_URL & "songTerm=" & track_name
	end if
	if the artist_name is not "" then
		set the artist_name to my encode_item(artist_name)
		if the target_URL ends with "?" then
			set the target_URL to target_URL & "artistTerm=" & artist_name
		else
			set the target_URL to target_URL & "&artistTerm=" & artist_name
		end if
	end if
	if the album_name is not "" then
		set the album_name to my encode_item(album_name)
		if the target_URL ends with "?" then
			set the target_URL to target_URL & "albumTerm=" & album_name
		else
			set the target_URL to target_URL & "&albumTerm=" & album_name
		end if
	end if
	if the composer_name is not "" then
		set the composer_name to my encode_item(composer_name)
		if the target_URL ends with "?" then
			set the target_URL to target_URL & "composerTerm=" & composer_name
		else
			set the target_URL to target_URL & "&composerTerm=" & composer_name
		end if
	end if
	if the genre_name is not "" then
		set the genre_name to my encode_item(genre_name)
		if the target_URL ends with "?" then
			set the target_URL to target_URL & "genreTerm=" & genre_name
		else
			set the target_URL to target_URL & "&genreTerm=" & genre_name
		end if
	end if
	return the target_URL
end create_searchURL


-- this sub-routine is used to encode a string 
on encode_item(this_item)
	set the character_list to (every character of this_item)
	repeat with i from 1 to number of items in character_list
		set this_char to item i of the character_list
		if this_char is not in the allowed_chars then
			set item i of the character_list to my encode_char(this_char)
		end if
	end repeat
	return the character_list as string
end encode_item

-- this sub-routine is used to encode a character 
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

Thanks Sal, it Rocks