使用Applescript在预览中调整颜色

时间:2015-07-31 15:40:34

标签: xcode applescript

我对Applescripts很新。

我的目标:使用"预览"自动打开(在一个窗口/实例中)目录中的所有图片。应用程序,访问"调整颜色"工具箱[工具>调整颜色...],单击"自动等级"按钮表示工具箱,继续下一个图像,然后单击"自动等级"等,然后全部保存。

我一直在使用Xcode"辅助功能检查器"实用程序试图获取这些按钮的名称和类等,但我也是使用辅助功能检查器的新手,所以我现在主要是在玩这个。

到目前为止,这是我对Applescript的大杂烩;做了一些其他人的片段的原始组装' Applescripts,我已经能够打开图片,只有部分可重复的成功:

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. 我不知道如何在一个窗口中打开所有图像。 [open pics]行似乎分别打开每个文件,即使它们都已被选中。
  2. Applescripts似乎并不喜欢我告诉应用程序单击菜单项的事实。但我不认为预览构成了一个过程" (即改变"告诉应用程序" ...到"告诉进程" ..允许代码编译,但过程"预览"不存在...... oi!
  3. 期待我可以开始告诉它点击" Auto Level"工具箱中的按钮,可能是工具箱的类(?)?我看到"浮动窗口"在辅助功能检查器中,但我的尝试中出现了错误......
  4. 感谢您提供任何见解,资源或热议......

2 个答案:

答案 0 :(得分:0)

GUI脚本,特别是Preview.app真的很痛苦。

请尝试此操作,它会逐个打开图像,点击Auto Level按钮保存图像并关闭它。

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

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

答案 1 :(得分:0)

强烈建立 vadian 的答案,我会删除activate application "Preview"行。对我来说,它在首次启动预览和打开图片后会干扰。确保预览是打开图片文件的标准应用程序,它可以正常工作。另外对于其他感兴趣的人,我研究了如何操纵滑块。处理具有大文件大小的图片时,预览似乎变得不稳定,因此使用延迟可能会有所帮助。以下是桌面上包含图片的文件夹中 vadian 的更改vode。只需更改*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"