使用applescript的zip文件夹

时间:2011-02-05 20:05:43

标签: zip applescript

我有这个AppleScript,它获取所选项目并压缩该文件/文件夹并使用该名称作为zip名称。我遇到的问题是,当我解压缩zip时,它使得它具有文件夹结构,所有路径都来自User on up。 像这样:

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

2 个答案:

答案 0 :(得分:3)

在zip命令中添加-j开关。换句话说,在“结束告诉”之前将脚本的最后一行更改为:

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

这应该告诉zip命令“破解”当你为zip文件制作目录结构时要压缩的任何路径。

答案 1 :(得分: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