2013-07-07 107 views
0

我想壓縮使用AppleScript的文件夾內的整個文件。我使用這個代碼,AppleScript壓縮沒有文件夾結構的文件夾?

tell application "Finder" 
    set x1 to (path to home folder as text) 
    set theItem to x1 & "Documents:MyFolder" as alias 
    set itemPath to quoted form of POSIX path of theItem 
    set fileName to name of theItem 
    set theFolder to POSIX path of (container of theItem as alias) 
    set zipFile to quoted form of (theFolder & fileName & ".zip") 
    do shell script "zip -r " & zipFile & " " & itemPath 
end tell 

這工作正常。但它呼嘯而過的整個文件夾結構是這樣的zip文件中,

用戶:MYUSER:文件:MyFolder文件:

我想只是壓縮的文件夾內的所有文件,而這個文件夾結構。我的意思是甚至沒有MyFolder。只需將文件放入一個zip文件即可。

回答

1

這裏有一個快速的修改,將創建壓縮所有文件的itemPath並輸出裏面zipFile

tell application "Finder" 
    set x1 to (path to home folder as text) 
    set theItem to x1 & "Documents:MyFolder" as alias 
    set itemPath to quoted form of POSIX path of theItem 
    set fileName to name of theItem 
    set theFolder to POSIX path of (container of theItem as alias) 
    set zipFile to quoted form of (theFolder & fileName & ".zip") 
    do shell script "cd " & itemPath & ";zip -r " & zipFile & " *" 
end tell 

如果你運行該原樣,它將包含內容的文檔創建MyFolder.zip MyFolder的。

這裏的技巧是腳本首先進入文件夾,然後遞歸調用*上的zip,它表示當前目錄中的所有文件(這是要壓縮的目標)。

+0

燦爛的答案。奇蹟般有效。 –