使用 Applescript 在预览中调整颜色

Using Applescript To Adjust Colors In Preview

我对 Applescripts 很陌生。

我的目标:使用"Preview"应用程序自动打开(在一个window/instance中)目录中的所有图片,访问"Adjust Colors" 工具箱 [工具 > 调整颜色...],单击工具箱中的 "Auto Level" 按钮,继续下一张图像并单击 "Auto Level",依此类推,然后全部保存。

我一直在使用 Xcode "Accessibility Inspector" 实用程序来尝试获取这些按钮等的名称和 classes,但我对使用Accessibility Inspector,所以我现在主要是在玩。

到目前为止,这是我的 Applescript 大杂烩;在对其他人的 Applescript 的一些片段进行了一些粗略的组装后,我已经能够打开图片,但只有部分可重复的成功:

tell application "Finder"
    activate
    open folder "Import" of folder "Shared" of folder "Users" of startup disk
    set pics to select every item of folder "Import" of folder "Shared" of folder "Users" of startup disk
    open pics
end tell
tell application "System Events" to tell Application "Preview"
    click menu item "Adjust Color..." of menu bar item "Tools" of window 1
end tell

我知道有那么多......会让你祖母对这段代码感到畏缩的事情太多了,但我希望能把它分解......

  1. 我不确定如何打开一个图像中的所有图像 window。 [打开图片] 行似乎单独打开每个文件,即使它们都已被选中。
  2. Applescripts 似乎不喜欢我告诉应用程序单击菜单项这一事实。但我不认为预览构成 "Process"(即将 "tell application"... 更改为 "tell process".. 允许代码编译,但过程 "Preview" 不会存在... oi!
  3. 期待我可以开始告诉它点击工具箱中的"Auto Level"按钮,工具箱的class(?)可能是什么?我在辅助功能检查器中看到 "floating window",但在我尝试时出现了错误...

感谢您提供任何见解、资源或鼓舞人心的谈话...

GUI 脚本,尤其是 Preview.app 真的很痛苦。

请试试这个,它会一张一张打开图片,单击 Auto Level 按钮保存图片并关闭它。

确保在 运行 脚本之前未打开浮动 window Adjust Color。如果脚本卡住,关闭浮动 window,退出预览并再次 运行 脚本。

set sharedFolder to path to shared documents folder
tell application "Finder" to set allImages to every item of folder "Import" of sharedFolder

set adjustColorWindow to missing value
repeat with anImage in allImages
    tell application "Finder" to open anImage
    activate application "Preview"
    tell application "System Events"
        tell process "Preview"
            repeat until exists (1st window whose value of attribute "AXSubRole" is "AXStandardWindow")
                delay 0.2
            end repeat
            set documentWindow to (name of 1st window whose value of attribute "AXSubRole" is "AXStandardWindow")
            if adjustColorWindow is missing value then
                click menu item "Adjust Color…" of menu 1 of menu bar item "Tools" of menu bar 1
                repeat until exists (1st window whose title starts with "Adjust Color")
                    delay 0.2
                end repeat
            end if
            set adjustColorWindow to (1st window whose title starts with "Adjust Color")
            tell adjustColorWindow
                click button "Auto Levels"
            end tell
            click menu item "Save" of menu 1 of menu bar item "File" of menu bar 1
            click button 1 of window documentWindow
        end tell
    end tell
end repeat

基于 vadian 的回答,我删除了 activate application "Preview" 行。对我来说,它会在第一次启动预览时和一张接一张地打开图片时产生干扰。确保预览是您打开图片文件的标准应用程序,它应该可以正常工作。同样对于其他感兴趣的人,我研究了如何操作滑块。处理文件较大的图片时,预览似乎变得不稳定,因此处理延迟会有所帮助。以下是 vadian 为您桌面上的文件夹更改后的图片,其中包含图片。只需更改 *Username* 并在您的桌面上创建一个文件夹(此处命名为 APPLESCRIPT):

tell application "Finder"
    set desktopFolder to folder "Macintosh HD:Users:*Username*:Desktop"
    set allImages to every item of folder "APPLESCRIPT" of desktopFolder
end tell

set adjustColorWindow to missing value
repeat with anImage in allImages
    delay 5.0
    tell application "Finder" to open anImage
    -- activate application "Preview"
    tell application "System Events"
        tell process "Preview"
            repeat until exists (1st window whose value of attribute "AXSubRole" is "AXStandardWindow")
                delay 1.0
            end repeat
            set documentWindow to (name of 1st window whose value of attribute "AXSubRole" is "AXStandardWindow")
            if adjustColorWindow is missing value then
                click menu item "Adjust Color ..." of menu 1 of menu bar item "Tools" of menu bar 1
                repeat until exists (1st window whose title starts with "Adjust Color")
                    delay 1.0
                end repeat
            end if
            set adjustColorWindow to (1st window whose title starts with "Adjust Color")
            tell adjustColorWindow

                -- click button "Auto Levels"
                -- get value of slider x
                -- slider 1: exposure (-2.0; 2.0)
                -- slider 2: contrast (-1.0; 1.0)
                -- slider 3: highlights (-1.0; -0.3)
                -- slider 4: shadows (0.0; 1.0)
                -- slider 5: saturation (0.0; 2.0)
                -- slider 6: temperature (3500.0; 6500.0)
                -- slider 7: hue (-150.0; 150.0)
                -- slider 8: eepia (0.0; 1.0)
                -- slider 9: sharpness (-1.0; 1.0)

                set value of slider 1 to 2.0
                delay 3.0

                set value of slider 6 to 3500.0
                delay 3.0

                set value of slider 9 to 1.0
                delay 3.0

                set value of slider 2 to -0.95
                delay 3.0

            end tell
        end tell

        tell application "Preview" to close every window

    end tell
end repeat
tell application "System Events" to quit application "Preview"