如何使用AppleScript-objc压缩文件夹?

时间:2019-06-18 23:35:43

标签: applescript-objc

我正在系统上生成一些日志,然后将其复制到/ tmp / MyFolder,然后 我将文件夹移动到桌面上,在尝试继续之前将其压缩,但是我不知道如何操作,我尝试了以下操作:

tell application "Finder"
    set theItem to "/tmp/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

1 个答案:

答案 0 :(得分:1)

虽然这不是AppleScript-ObjC脚本,但我要发布您自己的脚本的更正版本,以便其能够按照您的描述进行操作:

tell application "System Events"
    set itemPath to "/tmp/MyFolder"
    set theItem to item itemPath
    set fileName to quoted form of (get name of theItem)
    set theFolder to POSIX path of (container of theItem)
    set zipFile to fileName & ".zip"
end tell

do shell script "cd " & quoted form of theFolder & ¬
    "; zip -r " & zipFile & space & fileName & ¬
    "; mv " & zipFile & space & "~/Desktop/"

尝试避免使用 Finder 进行文件系统操作。这听起来有点违反直觉,但是并不适合。使用系统事件,它具有许多优势,例如,能够处理posix路径。

脚本现在将文件夹及其包含的项目压缩到/tmp/MyFolder.zip的存档中,然后将该存档移至桌面。