0

我正在尝试使用 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 文件中压缩整个文件夹结构,

用户:我的用户:文档:我的文件夹:

我只想在没有此文件夹结构的情况下压缩文件夹内的整个文件。我的意思是连MyFolder. 只是压缩文件里面的文件。

?

4

1 回答 1

1

这是一个快速修改,它将创建 zip 内部的所有文件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 内容的 Documents 中创建 MyFolder.zip。

这里的技巧是脚本首先 cd-ing 到文件夹中,然后递归调用 zip on *,它代表当前目录中的所有文件(这是要压缩的目标)。

于 2013-07-07T17:40:40.187 回答