使用Applescript备份文件夹

使用Applescript备份文件夹,applescript,directory,Applescript,Directory,我是Applescript的新手,正在尝试编写一个简单的脚本,以便在运行时备份一些文件夹。我的脚本如下: 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 int

我是Applescript的新手,正在尝试编写一个简单的脚本,以便在运行时备份一些文件夹。我的脚本如下:

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
但是,当我运行脚本时,我收到一个错误,指出:

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

这似乎是一个很小的问题,但我不知道如何解决它。有人能告诉我我做错了什么吗?

更换您的副本。。。符合下列规定:

duplicate folder POSIX file "~/Test" to backupFolder

AppleScript在大多数情况下不理解
“~/Test”
(甚至不理解
“/Users/username/Test/”

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
可以替换为
系统属性“HOME”
。您还可以直接使用HFS路径(如“Macintosh HD:Users:username:Test”)

不过,使用shell脚本会更容易:

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

提示:%F是%Y-%m-%d的快捷方式。所以:d=~/Backup/$(日期+%F)/