【问题标题】:applescript - how to improve this scriptapplescript - 如何改进这个脚本
【发布时间】:2017-06-28 21:20:28
【问题描述】:

首先,这是我第一次尝试使用applescript,所以请耐心等待:)

我想要实现的目标:我使用 Dropbox 作为在线存储一些图片的一种方式。我几乎使用了所有可用空间,但今天我意识到,通过对 jpg 文件进行简单的无损优化,我可以节省多达 35-40% 的空间(我使用应用程序优化了所有内容)。所以我的想法是:因为我的 Mac 上的 Dropbox 每次都在后台运行,如果我添加一些东西(或从手机上传的相机),图片最终会到达我的 Mac 中的一个文件夹,所以,也许,我可以监控那个文件夹,自动优化图片并再次上传,因为就DP而言,图片不同,我最终会得到自动优化的图像。

到目前为止的步骤:

  • 找到一个 CLI 来优化图像✔️

  • 基于文件夹创建服务,该服务在文件夹中发生任何变化时运行✔️

  • 优化图片✔️

  • 完成后显示通知✔️

自动机脚本如下所示(暂时附在测试文件夹中):

on run {input, parameters}
    tell application "Terminal"
        activate
        do script with command "imageoptim --directory /Users/Nick/Desktop/untitled"
    end tell
end run

这实际上是有效的,嗯,或多或少。

我想改进它,因为我对它的工作方式不是 100% 满意。

目前,如果我添加/重命名图片,则会打开一个终端窗口(有时是 2 个终端窗口),执行命令,然后就是这样。它永远不会关闭,并且在终端窗口打开时立即触发通知,而不是在优化完成后触发。

我想如何改进它:

  • 首先,我不想打开终端窗口。理想情况下,什么都不应该打开。它应该只执行优化,就是这样。

  • 通知应该显示一些有用的消息,它应该只在压缩后打开(基本上作为脚本的回调),但我不知道如何等待回调

  • 理想情况下,通知应该显示以前的大小和新的大小。我搜索了一种获取文件夹大小的方法,但是我可以使用 automator 中的任何内容。我的想法是获取以前的文件夹大小并将其添加到变量中,运行所有内容,获取新文件夹大小,另一个变量并将变量打印为通知消息,但正如我所说,我不知道如何获取文件夹大小。

你能帮我一把,或者给我一些有用的资源吗? (甚至更好)类似的东西已经存在,我只是想重新发明轮子,请告诉我:)

提前致谢

编辑

我发现了这个:https://www.alfredforum.com/topic/3703-imageoptim-workflow/,它几乎可以满足我的所有需求。是否可以从 Alfred wordkflow 转换为纯 automator 工作流?

【问题讨论】:

  • 请注意,JPEG 优化实际上永远不会是无损的。此外,如果您不希望终端显示,只需使用do shell script 而不是告诉终端激活。

标签: macos optimization applescript automator lossless-compression


【解决方案1】:

我将此脚本作为应用程序保存在 scripteditor 中。所以现在设置的方式就是我需要做的就是将图像文件拖到触发脚本的应用程序的图标上。您可以更改此设置并将脚本用作文件夹操作,或者您想要的其他任何方式

on open files_list
    (*
scale v : Scale an image
scale specifier : the object for the command
[by factor real] : scale using a scalefactor
[to size integer] : scale using a max width/length
*)
    set max_size to "600" as integer -- use the above commented code to play around with scaling.  My example scaled everything to a width of 600 pixels proportionately
    set output_folder to alias "Macintosh HD:Users:Smokestack:Desktop:untitled folder:" -- create an alias to whatever folder you want
    tell application "System Events" to get the size of output_folder
    set originalSize to the result
    set n2 to "1,000,000" as integer
    set originalSize to originalSize / n2
    repeat with file_ref in files_list
        try
            tell application "Image Events"
                set the_image to open file_ref
                scale the_image to size max_size
                save the_image in file ((output_folder as text) & name of the_image)
                close the_image
            end tell
        on error error_string
            display dialog "Skipping file " & (file_ref as text) & ¬
                " due to error:" & error_string buttons {"OK"} default button 1
        end try
    end repeat
    tell application "System Events" to get the size of output_folder
    set newSize to the result
    set newSize to newSize / n2
    display dialog "The Original Folder Size Is " & originalSize & " Megabytes" & linefeed & "The New Folder Size Is " & newSize & " Megabytes" with title "FOLDER INFORMATION" buttons {"OK"} default button 1 giving up after 10
end open

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    相关资源
    最近更新 更多