AppleScript压缩没有文件夹结构的文件夹?

时间:2013-07-07 16:54:05

标签: macos file compression zip directory

我正在尝试使用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 个答案:

答案 0 :(得分:1)

这是一个快速修改,将创建itemPath内的所有文件的zip并输出到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。

这里的诀窍是脚本首先进入文件夹,然后递归调用*上的zip,代表当前目录中的所有文件(这是要压缩的目标) )。

相关问题