PDA

View Full Version : Another AppleScript question - Reading a file




mduser63
May 4, 2005, 03:58 PM
I'm trying to figure out how to read a file in using AppleScript. I'd like to read each line into a different list elements so that I can then copy the contents of each list element to the name field of tracks in iTunes. I've got it to read in as a list OK I think, but I don't know how to access individual lines in the list (or file). This is the code I've got so far:

tell application "Finder"
set Names to (read (choose file with prompt "Pick text file containing track names")) as list
display dialog Names
end tell

Now what I need to know how to do is reference just one line of "Names" at a time. Any help would be appreciated.



HexMonkey
May 4, 2005, 10:07 PM
Your current code doesn't actually read the file as a list, because it doesn't know how to split the file into list items. You can use "paragraphs of" to convert it to a list of lines.

Then, to read the list, you can get an item at a particular index (eg "item 1 of Names"), and find out how many items there are with "count" (eg "count Names"). Alternatively, you can use a repeat loop to iterate through the list as follows (this example also filters out blank lines):

tell application "Finder"
set Names to paragraphs of (read (choose file with prompt "Pick text file containing track names"))
repeat with nextLine in Names
if length of nextLine is greater than 0 then
--Do something with the next name, which is stored in "nextLine"
end if
end repeat
end tell

mduser63
May 4, 2005, 11:14 PM
Thanks a lot for the reply. The people here on MacRumors are great. In case anybody is interested, I've written a little script (with the help of HexMonkey) which takes track names from a text file (one track name per line) and uses it to name the currently selected tracks in iTunes. I'm not expert at programming, and I've never even attempted to start to write an AppleScript before today, so I'm sure it's not perfect, but it works well enough for what I want to do. Here's the code:

tell application "iTunes"
if selection is not {} then
copy selection to theTracks
copy theTracks's length to theTrackCount
else
display dialog "No tracks selected." with icon caution
return
end if
end tell

tell application "Finder"
set Names to paragraphs of (read (choose file with prompt "Pick text file containing track names"))
if (the length of Names) is not equal to theTrackCount then
display dialog "Number of tracks selected does not match the number of lines in the track info file." with icon caution
return
end if
end tell

repeat with i from 1 to (theTrackCount)
tell application "iTunes"
copy (a reference to item i of theTracks) to theTrack
copy (item i of Names) to the name of theTrack
end tell
end repeat

This is something I had hoped to be able to do with Automator, but couldn't. It was Automator's shortcomings and limitations that motivated me to start learning some AppleScript. The next step is to see about writing some Automator actions with AppleScript.