Search code examples
applescriptautomatordroplet

Folder Backup every 5 minutes (Applescript - Droplet)


i am trying to write a script ;

  1. dragging a folder to droplet
  2. The script sets "dropped folder" as source
  3. List item
  4. Set target (different location)
  5. rsync every 5 minutes.

this is my starting point.

set source to "Dropped_Folder"
set destFolder to "/Users/xxx/Documents"
do shell script "/usr/bin/rsync -a  " & (quoted form of source) & " " & (quoted form of destFolder)

Thanks!


Solution

  • This script will create a stay-open droplet that lets you store up a list of rsync commands and execute them periodically. Copy/paste this code into Script Editor, then save it as an Application, with the 'Stay open after run handler' checkbox clicked on.

    • Drag and drop a folder onto the app icon to set up an rsync
    • Double-click the app icon or click the dock icon to remove rsync commands from the internal list.

    The app will execute all commands every execInterval seconds until you quit it, and resume executions when you restart it (unless you resave or recompile the app, which erases its persistant property storage).

    property rsyncCommandList : {}
    property execInterval : 300 -- 300 seconds is 5 minutes
    
    on run
        if (count of rsyncCommandList) = 0 then
            display alert "No rsync commands set up. Drop a folder on the application icon to set up an rsync command." as informational
        end if
    end run
    
    on reopen
        if (count of rsyncCommandList) > 0 then
            set deletableCommandList to (choose from list rsyncCommandList with prompt "Remove unwanted rsync commands" OK button name "Remove selected" with multiple selections allowed and empty selection allowed)
            set revisedList to {}
            repeat with thisItem in rsyncCommandList
                if (get thisItem) is not in deletableCommandList then
                    copy thisItem to end of revisedList
                end if
            end repeat
            set rsyncCommandList to revisedList
        end if
    end reopen
    
    on open theseItems
        repeat with thisItem in theseItems
            set source to POSIX path of thisItem
            set destination to POSIX path of (choose folder "Choose a destination folder for bacups of disk item '" & thisItem & "'")
            set rsyncString to "/usr/bin/rsync -a " & (quoted form of source) & " " & (quoted form of destination)
            copy rsyncString to end of rsyncCommandList
        end repeat
    end open
    
    on idle
        repeat with thisCommand in rsyncCommandList
            do shell script thisCommand & " &> /dev/null"
        end repeat
        return execInterval
    end idle