tags:

views:

29

answers:

1

I am trying to figure out how to iterate through a file line by line in AppleScript. Similar to a bash script like so:

for LINE in $(cat file)
do
   echo ${LINE}
done

Any tips? Thanks!

A: 

I can't take credit for writing this, I lifted the code from a response to a post in the MacRumors forums.

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

Original code credit to HexMonkey on the MacRumors forum.

kdmurray
There's no need to tell the Finder to do these things. You should take all of that code out of the Finder tell block. Applescript can handle that code itself and the Finder is often busy with other tasks so it may even slow the code down.
regulus6633
Good to know. I really should learn applescript myself one of these days. :P
kdmurray