Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Parse iTunes Library.xml into a readable text file Apps
This is a quick shell script to parse the location of music files out of an iTunes Library.xml file, sort, and write to Library.txt. I am sure there are ways to do the same thing better, or faster, but in the few minutes this took me to write, it works great, and made short work of the 2MB file I pointed it at.
#!/bin/sh

files=`grep 'Location' /Users/admin/Desktop/Library.xml | \
sed 's/<key>Location<\/key><string>//g'| sed 's/<\/string>//g' \
| sed 's/%20/ /g' | sort`;

old_IFS=$IFS

# IFS by default is space tab return, and stands for 
# Internal Field Separator.
IFS='
'

for i in $files
do
  `echo $i >> /Users/admin/Desktop/Library.txt`;
done

IFS=$old_IFS
[robg adds: I tested this, and it worked by simply copying and pasting the above code. I'm not positive this handled the "space tab return" bit properly, but I get one line per entry in my library, so it worked well enough for me. The end result is a text file showing the full path to every file in your library. To make the above script work, you'll need to change the admin entry to your admin user's name. You'll also have to copy and rename your iTunes Library file to the desktop and rename it to Library.xml (or the script could be changed to read the original file).]
    •    
  • Currently 3.40 / 5
  You rated: 5 / 5 (5 votes cast)
 
[9,548 views]  

Parse iTunes Library.xml into a readable text file | 9 comments | Create New Account
Click here to return to the 'Parse iTunes Library.xml into a readable text file' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Simpler Version
Authored by: escowles on Oct 06, '04 12:35:07PM
A simpler version:

#!/bin/sh

grep "<key>Location</key>" "$HOME/Music/iTunes/iTunes Music Library.xml" \
| sed 's/<key>Location<\/key><string>//g' \
| sed 's/<\/string>//g' | sed 's/%20/ /g' | sort > $HOME/Desktop/iTunes.txt

This also has a couple of side benefits:

  • uses $HOME to figure out where the files are (so the script doesn't need to be modified for each user)
  • doesn't match songs that have "Location" in the title

-Esme



[ Reply to This | # ]
Simpler Version
Authored by: FunkyOdor on Oct 06, '04 12:51:13PM

Instead of piping "sed" results one into another like this, use sed's "-e" option to stack them. For example:

grep 'goo' filename | sed -e 's/blah//g' -e 's/goo/howdy/g'

is equivalent to:

grep 'goo' filename | sed 's/blah//g' | sed 's/goo/howdy/g'

but more efficient and easier to read from a code maintanence point of view.



[ Reply to This | # ]
Simpler Version
Authored by: tamás on Oct 06, '04 03:33:58PM

To follow up on the recommended use of -e and to remove the tabs before the protocol (file, http, rtsp) in the resulting text file, this works:


#!/bin/sh

grep "<key>Location</key>" "$HOME/Music/iTunes/iTunes Music Library.xml" \
| sed -e 's/<key>Location<\/key><string>//g' -e 's/<\/string>//g' \
-e 's/%20/ /g' -e s/^[^fhr]*//g | sort > $HOME/Desktop/iTunes.txt


[ Reply to This | # ]
Parse iTunes Library.xml into a readable text file*censored*AppleSCript
Authored by: DougAdams on Oct 06, '04 04:20:44PM
AppleScript version:
set the_command to "grep \"<key>Location</key>\" " & ¬
	"\"$HOME/Music/iTunes/iTunes Music Library.xml\"" & ¬
	" | sed -e 's/<key>Location<\\/key><string>//g' -e 's/<\\/string>//g' " & ¬
	"-e 's/%20/ /g' -e s/^[^fhr]*//g | sort > $HOME/Desktop/iTunes.txt"

do shell script the_command

Save as a compiled script or just run it from Script Editor.

[ Reply to This | # ]
Non-ASCII Characters
Authored by: powerbookg3user0 on Oct 06, '04 07:49:47PM
This won't work with non-ASCII characters (in this case Japanese):

file://localhost/Users/tmurayama/Music/iTunes/
iTunes Music/Unknown Artist/Unknown Album/
%E9%95%B7%E9%87%8E%E7%9C%8C%E3%81%AE%E6%AD%8C.wav

This was all one line, I just broke it up to save trouble for people with small screens.

---
Takumi Murayama

[ Reply to This | # ]

Non-ASCII Characters
Authored by: taxi on Oct 07, '04 05:19:48AM

The problem you are describing is due to the handling of extended characters for URL (also called URL Encoding).

If you were going to use the list for other purposes, such as acting on each file with a shell script, or something else, then you might need to convert the extended characters from Hexadecimal (%20 is space, for instance) to octal.

To see what I mean, examine Beyoncé. This appears as 'Beyonce%CC%81' in URL encoding, but bash requires it be something more like 'Beyonce\314\201'.

Using python it is easy to see that 0xCC (hex CC) is equal to 204 (decimal), as is 0314 (octal 314).

What I do find interesting is that typing in Beyonc (alt-e e) gives the result 'Beyonc\303\251' (note the missing second e). Apparently there are two ways to describe an extended character - using the ASCII base character, and not.

Reminds me of the OT quote:

"There are two types of people in the world. Those who divide other people into two groups, and those who do not."

GRIN.



[ Reply to This | # ]
Parse iTunes Library.xml into a readable text file
Authored by: laurence.wilks on Oct 07, '04 03:08:33AM
I store my iTunes files on a separate hard disk so I've used a symbloic link to allow iTunes to 'find' the files it needs. This allows the
$HOME/Music/iTunes/iTunes Music Library.xml
part of this script to resolve correctly if you move your music files.

[ Reply to This | # ]
Parse iTunes Library.xml into a readable text file
Authored by: laurence.wilks on Oct 07, '04 03:09:05AM
I store my iTunes files on a separate hard disk so I've used a symbloic link to allow iTunes to 'find' the files it needs. This allows the
$HOME/Music/iTunes/iTunes Music Library.xml
part of this script to resolve correctly if you move your music files.

[ Reply to This | # ]
Parse iTunes Library.xml into a readable text file
Authored by: arru on Oct 18, '04 02:23:32PM

you are right, there are ways to do this better and faster ;)
Choose File -> Export song list… and you may choose several formats including plain text.



[ Reply to This | # ]