PDA

View Full Version : iteration of a list in applescripts




fhill2
Jun 2, 2012, 11:27 PM
I have a list full of iTunes data I need to return in Max for Live, using this script:

tell application "iTunes"
set theartist to artist of item 1 of selection
set thealbum to album of item 1 of selection
set thebpm to bpm of item 1 of selection
set thecomment to comment of item 1 of selection
set thelist to theartist & " " & thealbum & " " & thebpm & " " & thecomment as list
end tell


What I need to do is iterate each item in the list and return it into the applescripts window (this is how I send it to Max).
How can I accomplish this?
Thanks.



kryten2
Jun 3, 2012, 02:14 AM
tell application "iTunes"
set theartist to artist of item 1 of selection
set thealbum to album of item 1 of selection
set thebpm to bpm of item 1 of selection
set thecomment to comment of item 1 of selection
set thelist to theartist & " " & thealbum & " " & thebpm & " " & thecomment as list
repeat with i from 1 to number of items in thelist
set this_item to item i of thelist
-- insert actions here
end repeat
end tell

chown33
Jun 3, 2012, 10:41 AM
tell application "iTunes"
set theartist to artist of item 1 of selection
set thealbum to album of item 1 of selection
set thebpm to bpm of item 1 of selection
set thecomment to comment of item 1 of selection
set thelist to theartist & " " & thealbum & " " & thebpm & " " & thecomment as list
repeat with i from 1 to number of items in thelist
set this_item to item i of thelist
-- insert actions here
end repeat
end tell

Or maybe:
repeat with this_item in thelist
-- insert actions on this_item here
end repeat

See:
http://www.devdaily.com/apple/applescript-for-loop-while-loop-examples

It's the third result googling for: applescript loop. The first two results are also worth reading.

fhill2
Jun 4, 2012, 02:02 AM
I've tried the repeat loop with this command: return this_item, and still get a list in MAX. Maybe you cannot iterate a list through the return command?
Full script:

tell application "iTunes"
set theartist to artist of item 1 of selection
set thealbum to album of item 1 of selection
set thebpm to bpm of item 1 of selection
set thecomment to comment of item 1 of selection
set thelist to theartist & " " & thealbum & " " & thebpm & " " & thecomment as list
repeat with i from 1 to number of items in thelist
set this_item to item i of thelist
return this_item
end repeat
end tell


And another quest, how would I enclose each item in the list with quotation marks, so I can accurately process the list when it's in MAX.
I would believe saving each variable as string would work?, i tried, and it didn't :(

Thanks for the help!

Red Menace
Jun 4, 2012, 09:04 AM
Your list item (thelist) only has one item (the string you set it to), so can you clarify exactly what you are wanting to output? If you are wanting to do something with each of the items you are getting from iTunes, you can do something like:

set theList to {}
tell application "iTunes"
set the end of theList to (artist of item 1 of selection)
set the end of theList to (album of item 1 of selection)
set the end of theList to (bpm of item 1 of selection)
set the end of theList to (comment of item 1 of selection)
repeat with anItem in theList
set thisItem to quote & (contents of anItem) & quote
log thisItem -- do something with thisItem, this example just logs it
end repeat
end tell