caleb531 Posted March 16, 2013 Share Posted March 16, 2013 (edited) 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 Any help would be greatly appreciated. -Caleb Edited March 16, 2013 by caleb531 Link to comment
Kudusch Posted March 16, 2013 Share Posted March 16, 2013 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
caleb531 Posted March 16, 2013 Author Share Posted March 16, 2013 (edited) 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 March 16, 2013 by caleb531 Link to comment
Kudusch Posted March 16, 2013 Share Posted March 16, 2013 I think the <icon> should have the attribute type="filetype". Link to comment
caleb531 Posted March 16, 2013 Author Share Posted March 16, 2013 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
Florian Posted March 16, 2013 Share Posted March 16, 2013 I'm not sure but try without specifying anything for <icon>. I think the default one will do the trick. Link to comment
caleb531 Posted March 17, 2013 Author Share Posted March 17, 2013 I'm not sure but try without specifying anything for <icon>. I think the default one will do the trick. Excellent, that worked! Thanks to both Kudusch and Florian for the help! -Caleb Link to comment
Florian Posted March 17, 2013 Share Posted March 17, 2013 Now I'd be really interested if you found a way to do this without creating a file. Link to comment
caleb531 Posted March 17, 2013 Author Share Posted March 17, 2013 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
James Posted March 17, 2013 Share Posted March 17, 2013 I'd also like to try it if you don't mind Link to comment
caleb531 Posted March 17, 2013 Author Share Posted March 17, 2013 (edited) Okay, I've finished polishing the workflow, and I think it's ready to be unleashed into the world! Download "Play Song" Workflow Details about the workflow's usage are in the README, although there really isn't much to it. Enjoy! -Caleb Edited March 17, 2013 by caleb531 Link to comment
Florian Posted March 17, 2013 Share Posted March 17, 2013 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
caleb531 Posted March 17, 2013 Author Share Posted March 17, 2013 (edited) 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 March 17, 2013 by caleb531 DJay 1 Link to comment
Carlos-Sz Posted March 17, 2013 Share Posted March 17, 2013 Very cool! Thank you for sharing it. Link to comment
DJay Posted March 17, 2013 Share Posted March 17, 2013 thanks for sharing. sounds nice. but my library has 12300 song. seems like didn't work for me. the status is always "searching". Link to comment
caleb531 Posted March 17, 2013 Author Share Posted March 17, 2013 (edited) thanks for sharing. sounds nice. but my library has 12300 song. seems like didn't work for me. the status is always "searching". 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 March 17, 2013 by caleb531 Link to comment
DJay Posted March 17, 2013 Share Posted March 17, 2013 changed the line. no difference. sorry Link to comment
Carlos-Sz Posted March 17, 2013 Share Posted March 17, 2013 (edited) 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". 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 March 17, 2013 by Carlos-Sz Link to comment
caleb531 Posted March 17, 2013 Author Share Posted March 17, 2013 (edited) Thanks, Carlos-Sz, for the help. This kind of issue is exactly why I wish Alfred used JSON for result output instead of XML. Anyway, I have posted a link to the updated workflow below. DJay, please test this and let me know how it works: Download "Play Song" Workflow (updated on 3-17-2013) Edited March 17, 2013 by caleb531 Link to comment
DJay Posted March 17, 2013 Share Posted March 17, 2013 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.. Link to comment
Carlos-Sz Posted March 17, 2013 Share Posted March 17, 2013 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: open the workflow double click the Script Filter module select all (command+a) and copy open AppleScript Editor paste it find the line with: set query to "{query}" replace "{query}" for something to search for, e.g. “fire" now run it and see the error it shows Link to comment
caleb531 Posted March 17, 2013 Author Share Posted March 17, 2013 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: open the workflow double click the Script Filter module select all (command+a) and copy open AppleScript Editor paste it find the line with: set query to "{query}" replace "{query}" for something to search for, e.g. “fire" 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
Carlos-Sz Posted March 17, 2013 Share Posted March 17, 2013 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
DJay Posted March 17, 2013 Share Posted March 17, 2013 (edited) so i changed the "music" playlist to "Zuletzt hinzugefügt" playlist. "Last added". About 100 songs. And it works so far. Edited March 17, 2013 by DJay Link to comment
caleb531 Posted March 17, 2013 Author Share Posted March 17, 2013 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now