John Harris John Harris - 11 days ago 6
AppleScript Question

Applescript to not quit when erroring

I have an applescript that is used to copy a file (using rsync) from a source to a multitude of locations:

set Servers to {"afp://service.username:password@MACSERVER1.domain.com/Public/", "afp://service.username:password@MACSERVER2.domain.com/Public/", "afp://service.username:password@MACSERVER3.domain.com/Public/", "afp://service.username:password@MACSERVER4.domain.com/Public/", "afp://service.username:password@MACSERVER5.domain.com/Public/"}
--these are constant so no need to change
set Source to "/Shared Items/Public/DeployStudio/Masters/HFS/Sierra.hfs.dmg"
set Destination to "/Volumes/Public/DeployStudio/Masters/HFS"
--the scripts that goes recursively through the list of servers
repeat with i from 1 to count of Servers
set thisServer to item i of Servers
--this will mount each server destination in turn creating a /Volumes/Public folder
mount volume thisServer
--these are two rsync scripts which creates logfiles
do shell script "/usr/bin/rsync -rlptD --log- file=/Users/username/Documents/logs/Sierra-rsync.log " & (quoted form of Source) & " " & (quoted form of Destination)
--not sure this is needed but added this 5 sec delay to stop ejecting disk errors
delay 5
--eject the mounted volume so we don't have multiple Public folders
tell application "Finder"
eject disk "Public"
end tell
end repeat


This works great and produces a log of what it does. Unless there is a problem. Then it will just quit. I would like to change it so it ignores errors and keeps going. Any ideas? Thanks

Answer

To ignore errors in the repeat loop wrap the affected code in a try block

repeat with i from 1 to count of Servers
    set thisServer to item i of Servers
    try

      ...

    end try
end repeat
Comments