【问题标题】:Zip a folder using apple script使用苹果脚本压缩文件夹
【发布时间】:2014-04-30 13:14:05
【问题描述】:

我正在尝试使用 applescript 将文件夹复制到我的桌面,对其进行压缩,然后将 .zip 文件移动到其他位置,但我无法让压缩部分工作。

我到处寻找在 applescript 中压缩文件/文件夹的方法,但我不知道自己做错了什么,但没有一个对我有用。

我也宁愿不必选择复制后的文件夹和压缩后的文件夹,但我想我会留下它们直到压缩修复。

任何帮助都将不胜感激。

这是我的代码:(在 djbazziewazzie 的帮助下更新)

set workspace to (path to desktop as text) --"Users:produser:Desktop"

tell application "Finder"

display dialog "Select a folder to be zipped"
set inputFolder to choose folder
set inputFolderProperties to properties of inputFolder
set inputFolderName to name of inputFolder

duplicate inputFolder to workspace with properties --copy input folder to workspace
{name:inputFolderName} --keep the same name

--set copiedFile to (workspace & inputFolderName) 
display dialog "Select the folders desktop copy"
set copiedFile to choose folder --select the file copy thats on the workspace

tell current application
    set qpp to quoted form of POSIX path of copiedFile
    do shell script "zip -r " & qpp & ".zip " & qpp -- zips the file (or not...)
end tell

display dialog "Select the .zip file" --select the new .zip file
set zipFile to choose file
display dialog "Select the output folder"
set outputFolder to choose folder --moves zipped file
move zipFile to outputFolder

end tell   

【问题讨论】:

    标签: macos shell zip applescript


    【解决方案1】:

    应用程序是目录,因此您需要 -r 选项和 zip 才能将文件夹的所有文件添加到 zip 文件中。在 Mac OS X 中,以 .app 结尾的目录显示为文件而不是文件夹。

    tell 应用程序“Finder” 中使用 do shell 脚本也违反了脚本添加安全策略。仅当目标设置为常量current application 时才应使用 do shell 脚本。默认情况下,不是针对应用程序的每个代码都针对当前应用程序

    tell current application
     do shell script "zip -r " & qpp & ".zip " & qpp -- zips the file (or not...)
    end tell
    

    编辑 1: 显示工作代码

    编辑 2: 更新了 do shell 脚本以使用相对路径

    set workspace to (path to desktop as text)
    
    tell application "Finder"
        set inputFolder to choose folder with prompt "Select a folder to be zipped"
    
        set copiedFile to (duplicate inputFolder to workspace) as string
        set copiedFile to text 1 thru -2 of copiedFile --remove the trailing ":" 
    
        tell current application
            set qpp to quoted form of POSIX path of copiedFile
            do shell script "cd $(dirname " & qpp & ")
        zip -r  \"$(basename " & qpp & ").zip\" \"$(basename " & qpp & ")\""
            set zipFile to copiedFile & ".zip"
        end tell
    
        set outputFolder to choose folder with prompt "Select the output folder"
        move zipFile to outputFolder
    end tell
    

    【讨论】:

    • 感谢您的回复,根据您的建议,我不再收到任何错误消息,但不幸的是它仍然无法压缩所选文件
    • 我已经测试了你的代码并更新了它并编辑了我上面的帖子。您遇到的错误是因为选择文件夹返回一个带有尾随“:”的文件夹。 zip 文件类似于“/Users//Desktop/foldertozip/.zip”。这会产生一个名为“.zip”的不可见文件,因为以句点开头的文件在 Finder 中是不可见的。因此文件已创建,但在一个意想不到的地方不可见
    • 非常感谢你让这个工作,只有一个小问题:当我打开新的 .zip 时,它包含一个用户文件夹、一个用户名文件夹,最后是一个桌面文件夹,直到它进入我选择压缩的文件夹。你知道如何解决这个问题吗?
    • Zip 使用工作目录的相对路径,但是默认情况下,do shell 脚本中的当前工作目录是根文件夹。此外,该脚本不适用于相对路径,而是使用绝对路径,因此从根文件夹到所选文件夹到 zip 的完整路径存储在 zip 存档中。我已更新脚本以使用相对路径。
    • 万岁 :D 非常感谢你美丽的人 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多