AppleScript for automatically synchronising iOS devices

Discussion in 'Mac Programming' started by sultanoflondon, Feb 19, 2017.

  1. sultanoflondon macrumors 6502

    Joined:
    Dec 3, 2013
    #1
    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!
     
  2. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #2
    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
     
  3. sultanoflondon thread starter macrumors 6502

    Joined:
    Dec 3, 2013
    #3
    Thanks so much for this!
     

Share This Page