applescript-objc - 如何使用 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
解决方案
虽然这不是 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进行文件系统操作。这听起来违反直觉,但它并不适合它。使用System Events,它具有处理 posix 路径的能力以及许多其他好处。
该脚本现在将文件夹及其包含的项目压缩到位于 的存档中/tmp/MyFolder.zip
,然后将该存档移动到桌面。
推荐阅读
- android - OneSignal 推送通知未打开所需的链接
- python - 在公共键中合并具有相同值的多个字典列表
- python - 如何遍历使用 Python 和 Selenium 找到的元素的子元素?
- c++ - 二进制文件两次写入数据
- javascript - 如何使用 spreadjs 库快速有效地加载大量展开的 Excel 表格?
- javascript - 如何使用 ejs 和 express 进行图像选择?
- django - 如何使用 django-mdeditor 在前端渲染 markdown 内容?
- python - Django:对象没有属性“注释”
- oracle - Oracle SGA_TARGET 大小
- android - Android 在主题为暗时更改品牌启动背景颜色