I am new with Applescript, knowing nothing about it, but learning it by looking at all the examples that are given here in this forum …
I want to write an AppleScript for the following task that I perform every fortnight:
“Perform a Backup of my data to an external drive”
My main work is done on a MacBook Air. When I perform the BackUp from there onto an external drive I can also use this external drive to keep my iMAC up to date … (almost the same way around)
The steps are done in “Finder” (manually) in the following way:
1.) I open two different “Finder” windows
1.1.) the source folder (Macbook) where I store my data
1.2.) the target folder on external drive where I want my data to be copied
2.) on source folder I mark the main folders (e.g. “FINA”, “SPOR”, “MUSI” and “PRIV”)
3.) Then I move the marked folder with the touchpad into the window of the “finder” where
the main target folder is selected.
3.1.) the computer now works to
a) check all folders and subfolder and their subfolders
b) at the end, all files that where updated at this time will be copied to the target drive
3.2.) the user will be asked (for every main-folder) if data should
a) overwritten
b) the ‘newly’ updated data should be integrated
I always opt for (b) …
Is this too ambitious or can this be realised?
I have already problems to program a complete check of subfolders …
And my question: is this really necessary?
I use appleScript all the time and while this is theoretically possible, AppleScript is not the best tool for this. You would be AppleScripting Finder to move lots of data, and neither AppleScript nor Finder are really good at doing that.
I would suggest CarbonCopy Cloner, which is designed for doing exactly that and can be controlled by appleScript.
I would take a look at the command-line tool rsync. Without looking, I believe that it would support all of those requirements (e.g. updated files are synched, others are ignored), and it could be combined with applescript which might make it easier to implement.
I believe that an old version is built in to the mac but you never know anymore with Apple’s recent OS releases. Try launching the terminal and entering the following:
[format]rsync --version[/format]
Obviously, if it isn’t installed, you’ll get an error. If it’s v2 then it’s old although it may still be sufficient for your needs. Alternatively, you could use a package manager such as macports to install v3. Finally, for you can read the documentation by entering man rsync in the terminal (or online in myriad places like ss64.com).
I thought I would provide a simple example of an rsync script that does what the OP wants. It should be noted that:
This was tested on Monterey and utilizes the rsync utility included with Monterey.
The path to rsync is not specified but should be added if an error is encountered.
Folders at the target are created and older but not newer versions of files are overwritten.
set sourceFolders to (choose folder with prompt "Select source folders" with multiple selections allowed)
set targetFolder to quoted form of (POSIX path of (choose folder with prompt "Select target folder"))
repeat with aFolder in sourceFolders
set contents of aFolder to (quoted form of (text 1 thru -2 of POSIX path of aFolder)) & space
end repeat
try
do shell script "rsync --archive --update --exclude=" & quoted form of ".*" & space & sourceFolders & targetFolder
on error errorMessage
display alert "The rsync utility reported an error" message errorMessage as critical
error number -128
end try