使用Applescript备份文件夹

时间:2013-01-09 09:18:55

标签: 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 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".

这似乎是一个微不足道的问题,但我无法弄清楚如何解决它。有人能让我知道我做错了什么吗?

2 个答案:

答案 0 :(得分:1)

将以下内容替换为重复...行:

duplicate folder POSIX file "~/Test" to backupFolder

答案 1 :(得分:1)

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可以替换为system attribute "HOME"。您也可以直接使用HFS路径(如"Macintosh HD:Users:username:Test")。

使用shell脚本会更容易:

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