# Scripting

Using DropSync with Applescript
Applescript is a powerful way to integrate DropSync into more complex workflows, or perform automated tasks in ways that are not possible with DropSync alone. If you're just getting started with Applescript itself check out some of the many excellent tutorials online and the comprehensive documentation from Apple.
Automating Syncs with an Automator Calendar action
By combining the Calendar application with DropSync it is possible to automate syncing at specific times of day or month

#####

1. Write the Script

   A template script for running a single sync from left to right on a folder pair called mudflats is shown below. The script code itself is relatively self explanatory. The first line opens the DropSync application if it isn't open already. Then we set a variable called currentStore to the folder pair we want to sync. A Store is equivalent to a Folder Pair. In this case the store is called "mudflats" and is one we have previously created using the GUI (It's also possible to create stores using AppleScript). The script assumes that a folder pair called "mudflats" exists in DropSync so you will need to change "mudflats" in the script to the name of a folder pair you have already configured. Subsequent lines initiate a sync and then wait a specified time, or until the sync is completed before quitting.

   Note that this script launches DropSync at the start and quits DropSync when it is finished. If you want to leave DropSync running you should remove the line that says "launch" as well as the final line that tells DropSync to quit

       (* Running a sync using DropSync and Applescript

       Demonstrates how to launch DropSync, select a folder pair for syncing, and then run a sync. DropSync supports more applescript functionality than is demonstrated here. Consult the DropSync.sdef file for complete details.

       To use this script you will need to modify it to reflect your own setup. The Applescript Editor utility can be used to run, edit and debug your scripts.

       The script can then be run from terminal using the osascript command, or can be automatically launched by linking it to a recurring event in the Calendar app.
       *)

       tell application "DropSync 3"
       	launch

       	(* Select the folder pair (store) the you want to sync by using its name *)
       	set currentStore to store named "mudflats"

       	(* Initiate the sync in the desired direction *)
       	sync currentStore direction DestinationRight

       	(* All the steps below are only required if you want to wait for the sync to complete and quit DropSync when the sync is finished *)
       	set timeused to 0
       	set timeoutsecs to 7200 -- 2 hours

       	(* Poll every 10 seconds to see if we are complete *)
       	repeat until syncing of currentStore is equal to false or timeused > timeoutsecs
       		delay 10
       		timeused = timeused + 10
       	end repeat

       	(* Tell Dropsync to quit if it is finished the sync *)
       	if syncing of currentStore is equal to false then quit

       end tell

2. Open the Automator application from Apple and create a new Calendar Alarm
3. Select Utilities from the library pane. Within Utilities you should find an action called "Run AppleScript". Select the "Run AppleScript" action and drag it into the workflow area.
4. Delete the default script text for your Run Applescript action and replace it with the script you created earlier.
5. Save your Calendar Alarm. The act of saving will create a new calendar item in your Automator calendar.
6. Edit the new calendar item to the desired schedule