试图让 AppleScript 创建一个合适的 zip 文件

Trying to get AppleScript to create a proper zip file

脚本需要执行与在 Finder 中右键单击 >“压缩”相同的操作。

tell application "Finder"
    set myDir to choose folder
    set myParentDir to the parent of myDir as alias
    set myParentPath to POSIX path of myParentDir
    set myDirName to the name of myDir
    set myZipName to "'" & myDirName & ".zip'"
    set myDirName to the quoted form of myDirName
    set myParentPath to the quoted form of myParentPath
    set myShellScript to "cd " & myParentPath & " | zip -r " & myZipName & " " & myDirName
    tell current application
        do shell script myShellScript
    end tell
end tell

如果我复制保存在 myShellScript 中的字符串并在终端中 运行 它可以正常工作,但是当从脚本编辑器 运行 时它会失败并显示以下内容:

error " zip warning: name not matched: IDA template Folder
zip error: Nothing to do! (try: zip -r IDA template Folder.zip . -i IDA template Folder)" number 12

有什么建议吗?

您需要将 myShellScript 中的竖线 | 更改为换行符 \n,因此: set myShellScript to "cd " & myParentPath & "\n zip -r " & myZipName & " " & myDirName

注意:另一种方法——以及它在编译时在脚本编辑器中的显示方式——就是简单地键入文字换行符:

set myShellScript to "cd " & myParentPath & "
 zip -r " & myZipName & " " & myDirName

您应该将 | 替换为 && - 这样,如果 cd 命令成功,zip 命令将仅 运行。像这样:

set myShellScript to "cd " & myParentPath & " && zip -r " & myZipName & " " & myDirName