Lab rats wanted
The only way to get a user's Music folder is by parsing the iTunes Music Library.xml file. The following script does that and opens the Music folder, no matter where it is.
Hopefully.
I'm curious if it doesn't work. It's part of a larger (freeware!) project I'm working on. If anyone is interested, just copy and paste the code below into Script Editor, and Run. Code should wrap correctly in the clipboard. You can save it as a compiled script in iTunes Scripts folder if you like. Post your results and/or coding suggestions in this thread. Thanks!
tell application "Finder"
set xmlPath to (file "iTunes Music Library.xml" of ?
folder "iTunes" of folder "Music" of home) as alias
end tell
copy (open for access xmlPath) to fR
copy (read fR for 600) to x
close access fR
-- parse what we got from the xml file
repeat with thisL in (every paragraph of x)
if thisL contains ">Music Folder<" then exit repeat
end repeat
-- follow the progress of thisL
(*
set thisL to last item of my TextToList(thisL, "<string>file://localhost/Volumes/")
set thisL to first item of my TextToList(thisL, "</string>")
set thisL to POSIX path of thisL
set myMF to (characters 2 thru -1 of thisL)
*)
set myMF to (characters 2 thru -1 of ?
(POSIX path of (first item of my TextToList((last item of my ?
TextToList(thisL, "<string>file://localhost/Volumes/")), "</string>")))) as string
tell application "Finder"
open folder myMF
activate
end tell
--
on TextToList(theText, theDelimiter)
set saveDelim to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to {theDelimiter}
set theList to every text item of theText
on error errStr number errNum
set AppleScript's text item delimiters to saveDelim
error errStr number errNum
end try
set AppleScript's text item delimiters to saveDelim
return (theList)
end TextToList
[2099 byte] By [
Doug Adams] at [2007-11-9 12:35:37]

# 1 Re: Lab rats wanted
Doug,
I get this error:
Can't make file "iTunes Music Library.xml" of ?class cfol? "iTunes" of ?class cfol? "Music" of ?class home? of application "Finder" into a alias.
# 2 Re: Lab rats wanted
Grr.
Is that the location of your iTunes Music Library.xml file? Is there another way of positively locating this file in the script, d'you suppose?
I'm actually more concerned that the next section works, so it's a bummer not to get beyond this early part.
# 3 Re: Lab rats wanted
No. That's the problem right there. I use the default settings, and iTunes puts the file in home/Documents/iTunes/, not home/Music/iTunes.
Is there a way to maybe use the Terminal's "find" command to find the file?
find ~/ -name "*iTunes Music Library*"
will return a path to the file - we'd just have to get AppleScript to turn that into an alias.
# 4 Re: Lab rats wanted
Ah, of course! The old put it in the Documents folder trick! That's actually very good to know!
No, I can write a workaround for that. If it's not in Music, it's in Documents, simple enough. In fact, if you want to test that, just change "Music" to "Documents" and I will be surprised if it does not work correctly.
tell application "Finder"
try
set xmlPath to (file "iTunes Music Library.xml" of ?
folder "iTunes" of folder "Music" of home) as alias
on error
set xmlPath to (file "iTunes Music Library.xml" of ?
folder "iTunes" of folder "Documents" of home) as alias
end try
end tell
The Terminal script is also a good idea. I'd really like to start thinking along those lines more (altho', I try to make sure things can work in OS 9 whenever possible--rumors of its demise are greatly exagerated).
# 5 Re: Lab rats wanted
Doug,
That works. The Finder opens my Music Library folder in a new window.
# 6 Re: Lab rats wanted
Thanks e!
# 7 Re: Lab rats wanted
Fails for me too. The XML file is in the iTunes folder, but the iTunes music folder is called NewStuff which is a sister folder to the iTunes folder. -Chuck-
# 8 Re: Lab rats wanted
The error I am getting is:
Finder got an error: Can't get folder " <key>Music Folder<:key><string>file/::localhost:Users:chuck:Music:NewStuff:".
and it highlights the open line in:
tell application "Finder"
open folder myMF
activate
end tell
-Chuck-
# 9 Re: Lab rats wanted
Thanks BC.
Just so I know what's going on: is your hard drive partitioned, or do you have just the one volume (which I suspect)?
# 10 Re: Lab rats wanted
Originally posted by Doug Adams
Thanks BC.
Just so I know what's going on: is your hard drive partitioned, or do you have just the one volume (which I suspect)?
One volume on this computer, but I often have others mounted from other systems. -Chuck-
# 11 Re: Lab rats wanted
Replies appreciated! This should open the designated Music folder on any OS X machine:
tell application "Finder"
try
set xmlPath to (file "iTunes Music Library.xml" of ?
folder "iTunes" of folder "Music" of home) as alias
on error
set xmlPath to (file "iTunes Music Library.xml" of ?
folder "iTunes" of folder "Documents" of home) as alias
end try
end tell
copy (open for access xmlPath) to fR
copy (read fR for 600) to x
close access fR
-- parse what we got from the xml file
repeat with thisL in (every paragraph of x)
if thisL contains ">Music Folder<" then exit repeat
end repeat
set thisL to last item of my TextToList(thisL, "<string>file://localhost/")
if thisL starts with "Volumes/" then set thisL to last item of my TextToList(thisL, "Volumes/")
set thisL to first item of my TextToList(thisL, "</string>")
set thisL to POSIX path of thisL
set myMF to (characters 2 thru -1 of thisL) as text
-- myMF is now a usable file string
-- like so:
tell application "Finder"
open folder myMF
activate
end tell
--
on TextToList(theText, theDelimiter)
set saveDelim to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to {theDelimiter}
set theList to every text item of theText
on error errStr number errNum
set AppleScript's text item delimiters to saveDelim
error errStr number errNum
end try
set AppleScript's text item delimiters to saveDelim
return (theList)
end TextToList
# 12 Re: Lab rats wanted
Originally posted by Doug Adams
Replies appreciated! This should open the designated Music folder on any OS X machine:
That runs for me and does locate and open my music folder. -Chuck-
# 13 Re: Lab rats wanted
Thanks BC!
# 14 Re: Lab rats wanted
Doug,
Just so you know, I've been playing with my idea to use the Terminal to get the Music Library.xml location, and the Terminal finds it, no problem. But when I tell AppleScript to take what the Terminal returns and make an alias of it, it gives an error. Not sure why yet. It's a POSIX path the Terminal returns; this ought to be easy enough to turn into a alias, but no dice.
# 15 Re: Lab rats wanted
e,
That's sounds cool. How are you coercing the result of the shell script? Can you convert somehow as POSIX file? I seem to remember that POSIX path and POSIX file can be used to go back and forth.
(I'm fuzzy on shell-to-AppleScript stuff, but using cp, rm, mv, and similar basic file commands is fantastic. Perl integration likewise.)
# 16 Re: Lab rats wanted
Doug,
I tried
do shell script "find ~/ -name '*iTunes Music Library.xml*'"
set xmlPath to the result
set xmlPath to POSIX path of xmlPath
and
do shell script "find ~/ -name '*iTunes Music Library.xml*'"
set xmlPath to the result
set xmlPath to xmlPath as alias
# 17 Re: Lab rats wanted
e,
Having difficulty here, too.
First, I was getting multiple results from shell thanks to astute backup strategy. Finally figured results are delimited by returns, did a text item delimiters swap, yadda yadda.
OK, have a file path and note the double slashes after home folder, which I convert to single slash (see yadda above), then add startup disk to front, so I build it up to this:
Mac OS X/Users/doug/Music/iTunes/iTunes Music Library.xml
From here I can't get it to do anything. Can't get info for, can't get container, Finder won' see it as file or alias (not found).
Baffled
:confused: <-- This an actual snapshot of me right now. Those question marks are real.
# 18 Re: Lab rats wanted
Got it, but it's ugly:
tell application "Finder"
set myhome to home
end tell
do shell script "find ~/ -name '*iTunes Music Library.xml*'"
set xmlPath to the result
-- eliminate extra finds
copy item 1 of my TextToList(xmlPath, return) to xx
-- get rid of double slash
copy my TextToList(xx, "//") to xx
-- swap slashes with colons
copy my TextToList((item 2 of xx), "/") to xx
copy my ListToText(xx, ":") to xx
set tt to ((myhome as text) & xx)
-- tt is workable
--
on TextToList(theText, theDelimiter)
set saveDelim to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to {theDelimiter}
set theList to every text item of theText
on error errStr number errNum
set AppleScript's text item delimiters to saveDelim
error errStr number errNum
end try
set AppleScript's text item delimiters to saveDelim
return (theList)
end TextToList
on ListToText(theList, theDelimiter)
set saveDelim to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to {theDelimiter}
set theText to theList as text
on error errStr number errNum
set AppleScript's text item delimiters to saveDelim
error errStr number errNum
end try
set AppleScript's text item delimiters to saveDelim
return (theText)
end ListToText
# 19 Re: Lab rats wanted
If the head command is installed by default (I don't know if it is or not) then pipe the results of the find though head -n1 to only return the first line.
You may wish to check if the find command throws an error if it doesn't find anything matching. (grep will throw an error if you search for soemthing that isn't there instead of just returning no lines.)
Interesting URL: Mac OSX Man Pages Online (http://developer.apple.com/documentation/Darwin/Reference/ManPages/)