I’ve combined Doug Adam’s “Lyrics to TextEdit” iTunes script with Apple’s own “Clipboard to iPod Note” script to make a script that takes the currently playing or selected song[s] in iTunes and copies the Lyrics of the song[s] to a note on my iPod (sure wish Apple would include the nano’s lyrics-viewing feature in an update to non-nano iPods).
There is a “Lyrics” folder within the “Notes” folder of my iPod where I’m trying to save the notes to. If I leave the script set up to simply save the note[s] into the “Lyrics” folder, it works like a champ, but that’s not good enough for me–I want the script to check for a subfolder of the “Lyrics” folder named for the artist of the track being operated on, and if it exists, save the track’s lyrics to that folder (e.g. :My iPod:Notes:Lyrics:ArtistName:). If a folder named for the artist doesn’t exist, I want the script to create one, and then save the file there.
The problem I’m having is this: If multiple tracks by the same artist are selected, it will run through to completion once, successfully creating the artist folder, and saving the lyrics note. But on the iteration for the second track, the if (exists (folder whose name is …)) generates an error saying that the folder already exists. In other words, I have an if statement checking to see if the artist folder exists–if so, move on to saving the note in that folder; if not, create the folder, then move on to saving the note in that folder. But for some reason, wether or not the folder exists, the else statement that creates the new folder is being executed even if the folder already exists.
Here’s my script, with the broken part commented:
–Open this script in a new Script Editor window.
tell application “iTunes”
if player state is playing then
set sel to current track as list
else if selection is not {} then
set sel to selection
end if
[b][color=blue]repeat[/color][/b] [b][color=blue]with[/color][/b] [color=green]this_track[/color] [b][color=blue]in[/color][/b] [color=green]sel[/color]
[b][color=blue]set[/color][/b] {[color=green]art[/color], [color=green]nom[/color]} [b][color=blue]to[/color][/b] {[color=green]this_track[/color]'s [color=blue]artist[/color], [color=green]this_track[/color]'s [color=blue]name[/color]}
[b][color=blue]set[/color][/b] [color=green]the_string1[/color] [b][color=blue]to[/color][/b] "<a href=\"ipod:music?song=" & [color=green]nom[/color] & "&artist=" & [color=green]art[/color] & "\">Play Song</a><br>"
[b][color=blue]set[/color][/b] [color=green]the_lyrics[/color] [b][color=blue]to[/color][/b] [color=green]this_track[/color]'s [color=blue]lyrics[/color]
[b][color=blue]set[/color][/b] [color=green]file_name[/color] [b][color=blue]to[/color][/b] ([color=green]art[/color] & "-" & [color=green]nom[/color])
--[i][color=olive] check for iPods[/color][/i]
[b][color=blue]set[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color] [b][color=blue]to[/color][/b] [b][color=blue]my[/color][/b] [color=green]locate_iPods[/color]()
--[i][color=olive] check for iPod count[/color][/i]
[b][color=blue]if[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color] [b][color=blue]is[/color][/b] {} [b][color=blue]then[/color][/b]
[b][color=blue]error[/color][/b] "No iPod is connected to this computer."
[b][color=blue]else[/color][/b] [b][color=blue]if[/color][/b] [b][color=blue]the[/color][/b] ([color=blue]count[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]) [b][color=blue]is[/color][/b] [b][color=blue]greater than[/color][/b] 1 [b][color=blue]then[/color][/b]
--[i][color=olive] choose iPod[/color][/i]
[b][color=blue]set[/color][/b] [b][color=blue]the[/color][/b] [color=green]ipod_names[/color] [b][color=blue]to[/color][/b] {}
[b][color=blue]repeat[/color][/b] [b][color=blue]with[/color][/b] [color=green]i[/color] [b][color=blue]from[/color][/b] 1 [b][color=blue]to[/color][/b] [b][color=blue]the[/color][/b] [color=blue]count[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]
[b][color=blue]set[/color][/b] [color=green]this_iPod[/color] [b][color=blue]to[/color][/b] [color=blue]item[/color] [color=green]i[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]
[b][color=blue]tell[/color][/b] [color=blue]application[/color] "Finder"
[b][color=blue]set[/color][/b] [b][color=blue]the[/color][/b] [b][color=blue]end[/color][/b] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]ipod_names[/color] [b][color=blue]to[/color][/b] [b][color=blue]the[/color][/b] [color=blue]name[/color] [b][color=blue]of[/color][/b] [color=green]this_iPod[/color]
[b][color=blue]end[/color][/b] [b][color=blue]tell[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]repeat[/color][/b]
[b][color=blue]set[/color][/b] [color=green]this_name[/color] [b][color=blue]to[/color][/b] ([color=blue]choose from list[/color] [color=green]ipod_names[/color] [color=blue]with prompt[/color] "Pick the iPod to use:") [b][color=blue]as[/color][/b] [color=blue]string[/color]
[b][color=blue]if[/color][/b] [color=green]this_name[/color] [b][color=blue]is[/color][/b] "false" [b][color=blue]then[/color][/b] [b][color=blue]error[/color][/b] [color=blue]number[/color] -128
[b][color=blue]repeat[/color][/b] [b][color=blue]with[/color][/b] [color=green]i[/color] [b][color=blue]from[/color][/b] 1 [b][color=blue]to[/color][/b] [b][color=blue]the[/color][/b] [color=blue]count[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]ipod_names[/color]
[b][color=blue]if[/color][/b] [color=blue]item[/color] [color=green]i[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]ipod_names[/color] [b][color=blue]is[/color][/b] [color=green]this_name[/color] [b][color=blue]then[/color][/b]
[b][color=blue]set[/color][/b] [color=green]this_iPod[/color] [b][color=blue]to[/color][/b] [color=blue]item[/color] [color=green]i[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]
[b][color=blue]exit[/color][/b] [b][color=blue]repeat[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]repeat[/color][/b]
[b][color=blue]else[/color][/b]
[b][color=blue]set[/color][/b] [color=green]this_iPod[/color] [b][color=blue]to[/color][/b] [color=blue]item[/color] 1 [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
(*[i][color=olive] This section is supposed to check for a folder named for the artist, create one if it doesn't exist, and set the_path to that folder. STATUS: NOT WORKING [/color][/i]*)
[b][color=blue]tell[/color][/b] [color=blue]application[/color] "Finder"
[b][color=blue]set[/color][/b] [color=green]artist_folder[/color] [b][color=blue]to[/color][/b] [color=green]art[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]
[b][color=blue]if[/color][/b] ([color=blue]exists[/color] ([color=blue]folder[/color] [b][color=blue]whose[/color][/b] [color=blue]name[/color] [b][color=blue]is[/color][/b] [color=green]artist_folder[/color]) [b][color=blue]in[/color][/b] [color=blue]folder[/color] "Lyrics" [b][color=blue]of[/color][/b] [color=blue]folder[/color] "Notes" [b][color=blue]of[/color][/b] [color=green]this_iPod[/color]) [b][color=blue]then[/color][/b]
[b][color=blue]set[/color][/b] [b][color=blue]the[/color][/b] [color=green]the_path[/color] [b][color=blue]to[/color][/b] [color=blue]folder[/color] [color=green]artist_folder[/color] [b][color=blue]of[/color][/b] [color=blue]folder[/color] "Lyrics" [b][color=blue]of[/color][/b] [color=blue]folder[/color] "Notes" [b][color=blue]of[/color][/b] [color=green]this_iPod[/color]
[b][color=blue]else[/color][/b]
[color=blue]make[/color] [color=blue]new[/color] [color=blue]folder[/color] [b][color=blue]in[/color][/b] [color=blue]folder[/color] "Lyrics" [b][color=blue]of[/color][/b] [color=blue]folder[/color] "Notes" [b][color=blue]of[/color][/b] [color=green]this_iPod[/color] [color=blue]with properties[/color] {[color=blue]name[/color]:[color=green]artist_folder[/color]}
[b][color=blue]set[/color][/b] [b][color=blue]the[/color][/b] [color=green]the_path[/color] [b][color=blue]to[/color][/b] [color=blue]folder[/color] [color=green]artist_folder[/color] [b][color=blue]of[/color][/b] [color=blue]folder[/color] "Lyrics" [b][color=blue]of[/color][/b] [color=blue]folder[/color] "Notes" [b][color=blue]of[/color][/b] [color=green]this_iPod[/color]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]tell[/color][/b]
(*[i][color=olive] End artist-folder-checking/creating section [/color][/i]*)
[b][color=blue]if[/color][/b] [color=green]the_lyrics[/color] [b][color=blue]is[/color][/b] [b][color=blue]not[/color][/b] "" [b][color=blue]then[/color][/b]
[b][color=blue]set[/color][/b] [color=green]this_data[/color] [b][color=blue]to[/color][/b] {[color=green]the_string1[/color] & [color=green]nom[/color] & [color=green]the_lyrics[/color]} [b][color=blue]as[/color][/b] [color=blue]string[/color]
[b][color=blue]set[/color][/b] [color=green]target_file[/color] [b][color=blue]to[/color][/b] ([color=green]the_path[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) & [color=green]file_name[/color]
[b][color=blue]set[/color][/b] [color=green]openTargetFile[/color] [b][color=blue]to[/color][/b] [color=blue]open for access[/color] [color=blue]file[/color] [color=green]target_file[/color] [b][color=blue]with[/color][/b] [color=blue]write permission[/color]
[color=blue]write[/color] [color=green]this_data[/color] [color=blue]to[/color] [color=green]openTargetFile[/color]
[color=blue]close access[/color] [color=green]openTargetFile[/color]
--[i][color=olive] end tell[/color][/i]
[b][color=blue]else[/color][/b]
[color=blue]beep[/color]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]repeat[/color][/b]
end tell
on [color=green]locate_iPods/color
set the volumes_directory to “/Volumes/” as POSIX file as alias
set the volume_names to list folder volumes_directory without invisibles
set mounted_iPods to {}
repeat with i from 1 to the count of volume_names
try
set this_name to item i of volume_names
set this_disk to (“/Volumes/” & this_name & “/”) as POSIX file as alias
set these_items to list folder this_disk
if “iPod_Control” is in these_items then
set the end of the mounted_iPods to this_disk
end if
end try
end repeat
return mounted_iPods
end locate_iPods
Can anyone tell me how to fix the Finder tell statement here so that it works the way it should? I have a feeling it has something to do with how my entities and classes are being identified, but I’m strictly an AS amateur, so I don’t know enough to fix this.
TIA!
Browser: Firefox 1.0.7
Operating System: Mac OS X (10.4)
My guess is that whose is the problem?
Works…
if exists folder “Users” of disk “Macintosh HD” then
Doesn’t work…
if exists (folder whose name is “Users”) of disk “Macintosh HD” then
Someone smarter than me will have to explain why.
or…
set theArtistFolderPath2Test to (this_iPod as string) & “ Lyrics:” & artist_folder
if exists theArtistFolderPath2Test then
p.s. … You’re well past amature status.
Thanks for the reply. I tried what you said, but it didn’t work. Still get the same error. It’s still executing the “else” statement which tries to create the new folder, even when it already exists.
FWIW, here’s the output of the event log when I run it in Script Editor:
tell application "Finder"
exists "My iPod::Notes:Lyrics:Metallica"
false
make new folder with properties {name:"Metallica"} at folder "Lyrics" of folder "Notes" of alias "My iPod:"
"Finder got an error: The operation could not be completed because there is already an item with that name."
For some reason, it’s not seeing the existing folder. You can see in the Event Log that the “if exists” statement is returning false, which clearly, in my case, isn’t right after the first iteration. Very odd.
Here are some more suggestions to try:
Try building the paths before you get to the if/then/else section to cut down on translation work. For instance, instead of a run of folder of, folder of, etc., build the folder name and assign it to a variable:
set l_folder to this_pod & "Notes:"&"Lyrics:"& artist_folder as unicode text
Now, you have the exact name and location of the folder you want to test for as a variable.
I have had similar issues with these folder tests before, and sometimes I will test for the folder NOT existing:
set mainpath to path to desktop as Unicode text
set need_fold to "WhateverYouWant"
tell application "Finder"
if not (folder (mainpath & need_fold) exists) then
make new folder at mainpath with properties {name:need_fold}
end if
end tell
This is a script snippet I use in my stuff a lot.
Good luck, keep at it; it will work eventually.
casdvm
casdvm,
Thanks, that worked like a champ!
I just replaced the explicit folder references with the reference variable, and all is well!
Thanks to all for your help!
In case anyone is interestd, here’s my working script that takes the lyrics of the playing/selected track[s] in iTunes, and transfers them to a folder in my iPod named for the artist of the track. The artist folders are in a subfolder of the Notes folder called Lyrics. The script checks to see if the artist folder exists, and if not, it creates it. If no iPod is connected, it returns a warning.
–Open this script in a new Script Editor window.
tell application “iTunes”
if player state is playing then
set sel to current track as list
else if selection is not {} then
set sel to selection
end if
[b][color=blue]repeat[/color][/b] [b][color=blue]with[/color][/b] [color=green]this_track[/color] [b][color=blue]in[/color][/b] [color=green]sel[/color]
[b][color=blue]set[/color][/b] {[color=green]art[/color], [color=green]nom[/color]} [b][color=blue]to[/color][/b] {[color=green]this_track[/color]'s [color=blue]artist[/color], [color=green]this_track[/color]'s [color=blue]name[/color]}
[b][color=blue]set[/color][/b] [color=green]the_string1[/color] [b][color=blue]to[/color][/b] "<a href=\"ipod:music?song=" & [color=green]nom[/color] & "&artist=" & [color=green]art[/color] & "\">Play Song</a><br>"
[b][color=blue]set[/color][/b] [color=green]the_lyrics[/color] [b][color=blue]to[/color][/b] [color=green]this_track[/color]'s [color=blue]lyrics[/color]
[b][color=blue]set[/color][/b] [color=green]file_name[/color] [b][color=blue]to[/color][/b] ([color=green]art[/color] & "-" & [color=green]nom[/color])
--[i][color=olive] check for iPods[/color][/i]
[b][color=blue]set[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color] [b][color=blue]to[/color][/b] [b][color=blue]my[/color][/b] [color=green]locate_iPods[/color]()
--[i][color=olive] check for iPod count[/color][/i]
[b][color=blue]if[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color] [b][color=blue]is[/color][/b] {} [b][color=blue]then[/color][/b]
[b][color=blue]error[/color][/b] "No iPod is connected to this computer."
[b][color=blue]else[/color][/b] [b][color=blue]if[/color][/b] [b][color=blue]the[/color][/b] ([color=blue]count[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]) [b][color=blue]is[/color][/b] [b][color=blue]greater than[/color][/b] 1 [b][color=blue]then[/color][/b]
--[i][color=olive] choose iPod[/color][/i]
[b][color=blue]set[/color][/b] [b][color=blue]the[/color][/b] [color=green]ipod_names[/color] [b][color=blue]to[/color][/b] {}
[b][color=blue]repeat[/color][/b] [b][color=blue]with[/color][/b] [color=green]i[/color] [b][color=blue]from[/color][/b] 1 [b][color=blue]to[/color][/b] [b][color=blue]the[/color][/b] [color=blue]count[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]
[b][color=blue]set[/color][/b] [color=green]this_iPod[/color] [b][color=blue]to[/color][/b] [color=blue]item[/color] [color=green]i[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]
[b][color=blue]tell[/color][/b] [color=blue]application[/color] "Finder"
[b][color=blue]set[/color][/b] [b][color=blue]the[/color][/b] [b][color=blue]end[/color][/b] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]ipod_names[/color] [b][color=blue]to[/color][/b] [b][color=blue]the[/color][/b] [color=blue]name[/color] [b][color=blue]of[/color][/b] [color=green]this_iPod[/color]
[b][color=blue]end[/color][/b] [b][color=blue]tell[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]repeat[/color][/b]
[b][color=blue]set[/color][/b] [color=green]this_name[/color] [b][color=blue]to[/color][/b] ([color=blue]choose from list[/color] [color=green]ipod_names[/color] [color=blue]with prompt[/color] "Pick the iPod to use:") [b][color=blue]as[/color][/b] [color=blue]string[/color]
[b][color=blue]if[/color][/b] [color=green]this_name[/color] [b][color=blue]is[/color][/b] "false" [b][color=blue]then[/color][/b] [b][color=blue]error[/color][/b] [color=blue]number[/color] -128
[b][color=blue]repeat[/color][/b] [b][color=blue]with[/color][/b] [color=green]i[/color] [b][color=blue]from[/color][/b] 1 [b][color=blue]to[/color][/b] [b][color=blue]the[/color][/b] [color=blue]count[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]ipod_names[/color]
[b][color=blue]if[/color][/b] [color=blue]item[/color] [color=green]i[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]ipod_names[/color] [b][color=blue]is[/color][/b] [color=green]this_name[/color] [b][color=blue]then[/color][/b]
[b][color=blue]set[/color][/b] [color=green]this_iPod[/color] [b][color=blue]to[/color][/b] [color=blue]item[/color] [color=green]i[/color] [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]
[b][color=blue]exit[/color][/b] [b][color=blue]repeat[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]repeat[/color][/b]
[b][color=blue]else[/color][/b]
[b][color=blue]set[/color][/b] [color=green]this_iPod[/color] [b][color=blue]to[/color][/b] [color=blue]item[/color] 1 [b][color=blue]of[/color][/b] [b][color=blue]the[/color][/b] [color=green]mounted_iPods[/color]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]set[/color][/b] [color=green]artist_folder[/color] [b][color=blue]to[/color][/b] [color=green]art[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]
[b][color=blue]set[/color][/b] [color=green]l_folder[/color] [b][color=blue]to[/color][/b] [color=green]this_iPod[/color] & "Notes:" & "Lyrics:" [b][color=blue]as[/color][/b] [color=blue]Unicode text[/color]
[b][color=blue]tell[/color][/b] [color=blue]application[/color] "Finder"
[b][color=blue]if[/color][/b] [b][color=blue]not[/color][/b] ([color=blue]folder[/color] ([color=green]l_folder[/color] & [color=green]artist_folder[/color]) [color=blue]exists[/color]) [b][color=blue]then[/color][/b]
[color=blue]make[/color] [color=blue]new[/color] [color=blue]folder[/color] [color=blue]at[/color] [color=green]l_folder[/color] [color=blue]with properties[/color] {[color=blue]name[/color]:[color=green]artist_folder[/color]}
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]set[/color][/b] [b][color=blue]the[/color][/b] [color=green]the_path[/color] [b][color=blue]to[/color][/b] [color=blue]folder[/color] ([color=green]l_folder[/color] & [color=green]artist_folder[/color])
[b][color=blue]end[/color][/b] [b][color=blue]tell[/color][/b]
[b][color=blue]if[/color][/b] [color=green]the_lyrics[/color] [b][color=blue]is[/color][/b] [b][color=blue]not[/color][/b] "" [b][color=blue]then[/color][/b]
[b][color=blue]set[/color][/b] [color=green]this_data[/color] [b][color=blue]to[/color][/b] {[color=green]the_string1[/color] & [color=green]nom[/color] & [color=green]the_lyrics[/color]} [b][color=blue]as[/color][/b] [color=blue]string[/color]
[b][color=blue]set[/color][/b] [color=green]target_file[/color] [b][color=blue]to[/color][/b] ([color=green]the_path[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) & [color=green]file_name[/color]
[b][color=blue]set[/color][/b] [color=green]openTargetFile[/color] [b][color=blue]to[/color][/b] [color=blue]open for access[/color] [color=blue]file[/color] [color=green]target_file[/color] [b][color=blue]with[/color][/b] [color=blue]write permission[/color]
[color=blue]write[/color] [color=green]this_data[/color] [color=blue]to[/color] [color=green]openTargetFile[/color]
[color=blue]close access[/color] [color=green]openTargetFile[/color]
--[i][color=olive] end tell[/color][/i]
[b][color=blue]else[/color][/b]
[color=blue]beep[/color]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]repeat[/color][/b]
end tell
on [color=green]locate_iPods/color
set the volumes_directory to “/Volumes/” as POSIX file as alias
set the volume_names to list folder volumes_directory without invisibles
set mounted_iPods to {}
repeat with i from 1 to the count of volume_names
try
set this_name to item i of volume_names
set this_disk to (“/Volumes/” & this_name & “/”) as POSIX file as alias
set these_items to list folder this_disk
if “iPod_Control” is in these_items then
set the end of the mounted_iPods to this_disk
end if
end try
end repeat
return mounted_iPods
end locate_iPods
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]