木川 炎星
木川 炎星

Reputation: 4093

Backup a folder using Applescript

I'm new to Applescript, and am trying to put together a simple script to backup some folders when it is run. My script is as follows:

on run
    tell application "Finder"
        set backupFolder to make new folder at "BACKUP" with properties {name:(year of (current date) as string) & "-" & (month of (current date) as integer as string) & "-" & (day of (current date) as string)}
        duplicate folder "~/Test" to backupFolder
    end tell
end run

However, when I run the script I recieve an error stating that:

Finder got an error: Can’t set folder "2013-1-9" of disk "BACKUP" to folder "~/Test".

This seems like such a trivial problem, but I can't work out how to fix it. Would anyone be able to let me know what am I doing wrong?

Upvotes: 1

Views: 1483

Answers (2)

Lri
Lri

Reputation: 27613

AppleScript doesn't understand "~/Test" (or even "/Users/username/Test/") most of the time.

set d to (year of (current date) as text) & "-" & (month of (current date) as integer as text) & "-" & (day of (current date) as text)
tell application "Finder"
    set f to make new folder at POSIX file "/Users/username/Backups/" with properties {name:d}
    duplicate POSIX file "/Users/username/Test/" to f
end tell

/Users/username can be replaced with system attribute "HOME". You can also use HFS paths (like "Macintosh HD:Users:username:Test") directly.

It would be easier to do with a shell script though:

d=~/Backup/$(date +%Y-%m-%d)/
mkdir -p $d
cp -R ~/Test $d

Upvotes: 1

Michele Percich
Michele Percich

Reputation: 1892

Replace your duplicate... line with the following:

duplicate folder POSIX file "~/Test" to backupFolder

Upvotes: 1

Related Questions