2013-01-09 68 views
1

我是Applescript的新手,我試圖將一個簡單的腳本放在一起來備份運行時的某些文件夾。我的腳本如下:使用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". 

這似乎是這樣一個很重要的問題,但我不知道如何要解決這個問題。任何人都可以讓我知道我做錯了什麼?

回答

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 
1

更換您的重複......符合如下:

duplicate folder POSIX file "~/Test" to backupFolder 
+0

提示:%F是快捷鍵%Y-%間%d。所以:d =〜/ Backup/$(日期+%F)/ –

相關問題