Xcode 使用Applescript在预览中调整颜色

Xcode 使用Applescript在预览中调整颜色,xcode,applescript,Xcode,Applescript,我对Applescripts很陌生 我的目标:使用“预览”应用程序自动打开(在一个窗口/实例中)目录中的所有图片,访问“调整颜色”工具箱[Tools>Adjust Color…],单击工具箱中的“Auto Level”按钮,继续下一个图像并单击“Auto Level”等,然后保存所有图片 我一直在使用Xcode“Accessibility Inspector”实用程序,试图获取这些按钮的名称和类,但我对使用Accessibility Inspector也很陌生,所以我主要是在这一点上玩 到目前为

我对Applescripts很陌生

我的目标:使用“预览”应用程序自动打开(在一个窗口/实例中)目录中的所有图片,访问“调整颜色”工具箱[Tools>Adjust Color…],单击工具箱中的“Auto Level”按钮,继续下一个图像并单击“Auto Level”等,然后保存所有图片

我一直在使用Xcode“Accessibility Inspector”实用程序,试图获取这些按钮的名称和类,但我对使用Accessibility Inspector也很陌生,所以我主要是在这一点上玩

到目前为止,这是我的一本大杂烩苹果书;在对其他人的一些应用程序描述片段进行了一些粗略的组装之后,我能够打开这些图片,但只获得了部分可重复的成功:

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
我知道有太多的事情会让你的祖母对这个代码感到畏缩,但我希望能把它打破

  • 我不知道如何在一个窗口中打开所有图像。[open pics]行似乎分别打开每个文件,即使它们都已被选中
  • Applescripts似乎不喜欢我告诉应用程序单击菜单项这一事实。但我不认为预览构成了一个“过程”(即将“告诉应用程序”…更改为“告诉过程”)。允许代码进行编译,但过程“预览”不存在…oi
  • 期待着当我开始告诉它单击工具箱中的“自动级别”按钮时,工具箱的类可能是什么?我在可访问性检查器中看到“浮动窗口”,但在我的尝试中出现了错误

  • 感谢您提供的任何见解、资源或pep演讲…

    GUI脚本编写,尤其是Preview.app,真是一件痛苦的事

    请尝试此操作,它会逐个打开图像,单击
    自动分级
    按钮保存图像并将其关闭

    运行脚本之前,请确保未打开浮动窗口“调整颜色”。如果脚本卡住,请关闭浮动窗口,退出预览,然后再次运行脚本

    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
    

    GUI脚本编写,尤其是Preview.app,是一个真正的难题

    请尝试此操作,它会逐个打开图像,单击
    自动分级
    按钮保存图像并将其关闭

    运行脚本之前,请确保未打开浮动窗口“调整颜色”。如果脚本卡住,请关闭浮动窗口,退出预览,然后再次运行脚本

    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
    

    基于瓦迪安的答案,我将删除激活应用程序“预览”line。对我来说,在第一次启动预览和打开一张又一张图片时,它会产生干扰。请确保,预览是打开图片文件的标准应用程序,它应该可以正常工作。另外,对于其他感兴趣的ppl,我也研究了如何操作滑块。在处理文件大小较大的图片时,请参阅预览ms变得不稳定,因此处理延迟会有所帮助。以下是从vadian更改的vode,用于桌面上的文件夹,其中包含图片。只需更改
    *用户名*
    ,然后在桌面上创建一个文件夹(此处名为
    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"
    

    基于瓦迪安的答案,我将删除激活应用程序“预览”line。对我来说,在第一次启动预览和打开一张又一张图片时,它会产生干扰。请确保,预览是打开图片文件的标准应用程序,它应该可以正常工作。另外,对于其他感兴趣的ppl,我也研究了如何操作滑块。在处理文件大小较大的图片时,请参阅预览ms变得不稳定,因此处理延迟会有所帮助。以下是从vadian更改的vode,用于桌面上的文件夹,其中包含图片。只需更改
    *用户名*
    ,然后在桌面上创建一个文件夹(此处名为
    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"