iteration of a list in applescripts

Discussion in 'Mac Programming' started by fhill2, Jun 2, 2012.

  1. macrumors newbie

    Joined:
    May 9, 2012
    Messages:
    19
    #1
    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.
     
  2. macrumors 6502a

    Joined:
    Mar 17, 2012
    Messages:
    819
    Location:
    Belgium
    #2
    Code:
    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
    
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    Messages:
    6,404
    #3
    Or maybe:
    Code:
    	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.
     
  4. macrumors newbie

    Joined:
    May 9, 2012
    Messages:
    19
    #4
    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!
     
  5. macrumors 6502

    Joined:
    May 29, 2011
    Messages:
    276
    Location:
    Littleton, Colorado, USA
    #5
    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:

    Code:
    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
     

Share This Page