2011-02-05 49 views
3

我有這個applescript需要選擇的項目和拉鍊文件/文件夾,並使用該名稱作爲zip名稱。我遇到的問題是,當我解壓縮zip文件時,它使得文件夾結構中的所有用戶的路徑都起作用。 像這樣:zip文件夾使用applescript

Users: 
    username: 
     folder: 
      folder: 

我只是想它是:

folder: 

下面是代碼:

tell application "Finder" 
    set theItem to selection 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 

回答

3

添加-j切換到您的zip命令。換句話說,改變你的腳本的最後一行前的「末告訴」到:

do shell script "zip -jr " & zipFile & " " & itemPath 

這應該告訴zip命令,以「垃圾」的路徑,無論你想,當它使目錄壓縮結構爲zip文件。

+0

如果您使用-j,當有兩個具有相同名稱的文件時,您將看到一個錯誤。 – Peter

2
tell application "Finder" 
    set theItems to selection 
    repeat with _a from 1 to (count of theItems) 
     set theItem to (item _a of theItems) 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 repeat 
end tell