2011-02-05 153 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