gpt4 book ai didi

macos - 使用苹果脚本压缩文件夹

转载 作者:行者123 更新时间:2023-12-01 03:50:27 25 4
gpt4 key购买 nike

我正在尝试使用 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

最佳答案

应用程序是目录,因此您需要 -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

关于macos - 使用苹果脚本压缩文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23389118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com