Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sultanoflondon

macrumors 6502
Original poster
Dec 3, 2013
341
16
Hi all,

I came across this AppleScript which is designed to automatically sync all iOS devices connected to iTunes at the time, including those on Wi-Fi (using iTunes Wi-Fi sync).

tell application “iTunes”
repeat with s in sources
if (kind of s is iPod) then update s
end repeat
end tell

I am a beginner to AppleScript and I understand all of the above script except the second line: "repeat with s in sources". Can someone explain what this line means and does?

Thank you!
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
The repeat and end repeat code in AppleScript is a loop, that repeats a number of times.
In this case, iTunes holds a list or array of sources, and the repeat loop in your code loops through each of those sources.
Here are a few other examples of repeat loops in AppleScript below.

Code:
repeat 6 times
    -- Code to execute 6 times
end repeat

set x to 0 as integer
set isValid to false as boolean
repeat until isValid
    -- Executes 10 times
    set x to (x + 1) as integer
    if x = 10 then
        set isValid to true as boolean
    end if
end repeat
return isValid

set myText to "" as text
repeat with n from 2 to 10 by 2
    -- Code to execute 5 times with a 2 step interval
     set myText to (myText & space & (n as text))
end repeat
return myText

set mylist to {"A", "B", "C", "D", "E"} as list
set myText to "" as text
repeat with i from 1 to (count of myList)
    -- Executes 5 times, because there are five items in the list
    set myText to (myText & space & (item i of myList as text)) as text
end repeat
return myText

-- This next one is similar to the repeat loop in your code example, as it iterates through a list of items supplied to it.

set theDocumentsFolder to (path to documents folder from user domain) as text
tell application id "com.apple.systemevents"
    set theFilesList to (every item in folder theDocumentsFolder whose visible is true) as list
    repeat with theFile in theFilesList
        -- Write some code to do something with each theFile in the theFileList
    end repeat
end tell
return theFilesList

There are many more types of repeat loop in AppleScript, but I hope this helps getting you started.
There are a lot of AppleScript learning resources on the web, just google AppleScript repeat loops.

Regards Mark
 

sultanoflondon

macrumors 6502
Original poster
Dec 3, 2013
341
16
The repeat and end repeat code in AppleScript is a loop, that repeats a number of times.
In this case, iTunes holds a list or array of sources, and the repeat loop in your code loops through each of those sources.
Here are a few other examples of repeat loops in AppleScript below.

Code:
repeat 6 times
    -- Code to execute 6 times
end repeat

set x to 0 as integer
set isValid to false as boolean
repeat until isValid
    -- Executes 10 times
    set x to (x + 1) as integer
    if x = 10 then
        set isValid to true as boolean
    end if
end repeat
return isValid

set myText to "" as text
repeat with n from 2 to 10 by 2
    -- Code to execute 5 times with a 2 step interval
     set myText to (myText & space & (n as text))
end repeat
return myText

set mylist to {"A", "B", "C", "D", "E"} as list
set myText to "" as text
repeat with i from 1 to (count of myList)
    -- Executes 5 times, because there are five items in the list
    set myText to (myText & space & (item i of myList as text)) as text
end repeat
return myText

-- This next one is similar to the repeat loop in your code example, as it iterates through a list of items supplied to it.

set theDocumentsFolder to (path to documents folder from user domain) as text
tell application id "com.apple.systemevents"
    set theFilesList to (every item in folder theDocumentsFolder whose visible is true) as list
    repeat with theFile in theFilesList
        -- Write some code to do something with each theFile in the theFileList
    end repeat
end tell
return theFilesList

There are many more types of repeat loop in AppleScript, but I hope this helps getting you started.
There are a lot of AppleScript learning resources on the web, just google AppleScript repeat loops.

Regards Mark

Thanks so much for this!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.