Jump to content

Display iTunes Album Artwork in Alfred Results


Recommended Posts

Introduction

 

I recently created a workflow that would use the keyword play along with the given query to search my iTunes library for songs matching that query, which would then be displayed as results in Alfred. I attached an image below to demonstrate how it currently works.

 

Problem

 

Ideally, I would like to have the song's album artwork as the icon for each result. However, linking the icon to the actual song file just displays the typical M4A document icon. Therefore, I would like to know how I can display each song's album artwork in Alfred's results list.

 

Screenshot

 

alfredplay.png

 

Any help would be greatly appreciated.

-Caleb

Edited by caleb531
Link to comment

Hey caleb531,

 

you could use AppleScript to solve this problem:

tell application "iTunes"
set vCover to data of artwork 1 of current track
set vTarget to open for access ":path:to:the:workflow:folder:cover.jpg" with write permission
write vCover to vTarget
close access vTarget 
end tell
 

 

In the feedback XML you set the icon element to the path you specified before.

 

I hope this helps.

Link to comment

Hey caleb531,

 

you could use AppleScript to solve this problem:

tell application "iTunes"
set vCover to data of artwork 1 of current track
set vTarget to open for access ":path:to:the:workflow:folder:cover.jpg" with write permission
write vCover to vTarget
close access vTarget 
end tell
 

 

In the feedback XML you set the icon element to the path you specified before.

 

I hope this helps.

 

I tried that, and I integrated this into my script, but the icons show up as the usual JPEG document icons, rather than the images themselves.

 

For the record, I made sure that the <icon> element (in the resulting XML) has the attribute type="fileicon".

Edited by caleb531
Link to comment

I think the <icon> should have the attribute type="filetype".

 

According to the Generating Feedback in Workflows thread:

 

icon ( optional - Defaults to the script filter extension. If a script filter icon is not available, defaults to workflow icon )

The icon element allows you to specify the icon to use for your result item. This can be a file located in your workflow directory, an icon of a file type on your local machine, or the icon of a specific file on your system. To use the icons of a specific file type or another folder/file, you must provide a type attribute to the icon item. 

Example: <icon type="fileicon">/Applications</icon> - Use the icon associated to /Applications

Example: <icon type="filetype">public.folder</icon> - Use the public.folder (default folder) icon

 

Based on that, "filetype" signifies a specific file type, as the name implies, whereas "fileicon" would imply the icon of the specific file given. Therefore, I would think that I should be using "fileicon" in this case.

Link to comment

Now I'd be really interested if you found a way to do this without creating a file.

 

Same here, although with this solution, it is better to keep the file you create and store it somewhere so you don't have to re-create it with every search.

 

After I add the finishing touches, I'll post my workflow if you'd like to try it.

Link to comment

You should have a default image for the tracks without artwork. 

Also, do you clean up previous images? If this workflow ends up creating an entire duplicate of the library's artworks, it might result in a really really heavy folder...

Nice piece though

Link to comment

You should have a default image for the tracks without artwork. 

Also, do you clean up previous images? If this workflow ends up creating an entire duplicate of the library's artworks, it might result in a really really heavy folder...

Nice piece though

 

I just updated the script to handle tracks without artwork. Here is the new download link.

 

The script is designed to be efficient by storing whatever artwork it finds in a folder called Alfred Album Artwork (in your iTunes folder). This folder of images essentially acts as a cache to improve the performance of future queries. I could empty the folder upon the next query, but I think that introduces the potential for the workflow to be horribly inefficient.

 

However, in order to balance efficiency while trying not to bloat your file system, the generated artwork files only differ by artist and album. In other words, each album is tied to a single artwork file, which is shared by all songs within that album.

Edited by caleb531
Link to comment

thanks for sharing. sounds nice.

but my library has 12300 song. seems like didn't work for me. the status is always "searching".  :unsure:

 

I imagine that is because iTunes has to search through 12,300 songs.

 

Internally, the script is just querying your iTunes library using the search command:

 

tell application "iTunes"
   set allSongs to (search playlist "Music" for query)
end tell

 

Now, I am curious to know how that compares (in terms of performance) with:

tell application "iTunes"
   set allSongs to (get every track in playlist "Music" whose name contains query)
end tell

 

If someone with a large iTunes library would be so kind to test these two snippets of AppleScript (of course, substituting query for something else), I would be very curious to know the performance difference (if any). Thanks in return!

-Caleb

Edited by caleb531
Link to comment

I imagine that is because iTunes has to search through 12,300 songs.

 

Internally, the script is just querying your iTunes library using the search command:

...

 

thanks for sharing. sounds nice.

but my library has 12300 song. seems like didn't work for me. the status is always "searching".  :unsure:

 

 

I think I know why is stuck on Searching.

 

It seems the workflow is not parsing the returned XML data for Alfred. If a song has “&” for example Alfred will not process it because it is not parsed.

 

Here is a simple code to parse XML I did:

 

property disallowedChars : "&'\"<>"
on parseXML(sXML)
	set theNew to ""
	repeat with i from 1 to length of sXML
		if (character i of sXML) is in disallowedChars then
			if (character i of sXML) is in "&" then
				set theNew to theNew & "&"
			else if (character i of sXML) is in "'" then
				set theNew to theNew & "'"
			else if (character i of sXML) is in "\"" then
				set theNew to theNew & """
			else if (character i of sXML) is in "<" then
				set theNew to theNew & "<"
			else if (character i of sXML) is in ">" then
				set theNew to theNew & ">"
			end if
		else
			set theNew to theNew & character i of sXML
		end if
	end repeat
	return theNew
end parseXML


 

Just an update about my post:

 

I added the procedure above and added two lines (8 and 9 below) in your createitem procedure and things worked a lot better (even with more results):

 

on createItem(uid, arg, title, subtitle, icon)
	
	-- recognize file paths for the icon
	if icon begins with homePath then
		set icon to POSIX path of icon
	end if
	
	set title to parseXML(title)
	set subtitle to parseXML(subtitle)
	
	return "<item uid='" & uid & "' arg='" & arg & "'>
	<title>" & title & "</title>
	<subtitle>" & subtitle & "</subtitle>
	<icon>" & icon & "</icon>
	</item>"
	
end createItem
Edited by Carlos-Sz
Link to comment

thanks caleb531.

but unfortunately no. doesn't work for me.

seach for an song titel, search for artist. nothing in alfred to see. always "searching". sorry..

 

It seems you know a bit of scripting considering you have edited the workflow once.

 

So, you can easily see what is going wrong:

 

  1. open the workflow
  2. double click the Script Filter module
  3. select all (command+a) and copy
  4. open AppleScript Editor
  5. paste it
  6. find the line with: set query to "{query}"
  7. replace "{query}" for something to search for, e.g. “fire"
  8. now run it and see the error it shows
Link to comment

thanks caleb531.

but unfortunately no. doesn't work for me.

seach for an song titel, search for artist. nothing in alfred to see. always "searching". sorry..

 

Well, for what it's worth, I included a new variable in the script called chosenPlaylist, which determines from which playlist the script will search. Setting the variable's value to a smaller playlist (as opposed to "Music", which is the default) may result in faster searching.

 

It seems you know a bit of scripting considering you have edited the workflow once.

 

So, you can easily see what is going wrong:

 

  1. open the workflow
  2. double click the Script Filter module
  3. select all (command+a) and copy
  4. open AppleScript Editor
  5. paste it
  6. find the line with: set query to "{query}"
  7. replace "{query}" for something to search for, e.g. “fire"
  8. now run it and see the error it shows

 

Good suggestion. I would have recommended the same to DJay (if I had thought of it sooner).

Link to comment

Well, for what it's worth, I included a new variable in the script called chosenPlaylist, which determines from which playlist the script will search. Setting the variable's value to a smaller playlist (as opposed to "Music", which is the default) may result in faster searching.

 

 

Good suggestion. I would have recommended the same to DJay (if I had thought of it sooner).

 

As soon as you fine tune the workflow post it in the Share your Workflows forum. A clean one, without our discussion here. :)

 

Again, nice workflow!

Link to comment

so i changed the "music" playlist to "Zuletzt hinzugefügt" play list. Last added. About 100 songs. 

And it works so far. Strange not with all songs, but it shows a few.  ;)

 

Well, I suppose it's a workaround for those (like yourself) with huge iTunes libraries.

 

However, I still recommend you test what Carlos-Sz is suggesting, just for the sake of knowing. ;)

-Caleb

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...