Applescript:仅当尚未选中时如何单击复选框?

Applescript: how to click on checkbox only if is not already selected?

我想为系统中的所有“.app”文件启用选项 "Open in Low Resolution"。

这是我的 Applescript 代码:

on run argv
    tell application "Finder"
        set myFile to (POSIX file (item 1 of argv)) as alias
        open information window of myFile
        activate
        tell application "System Events"
            try
                click checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info") of process "Finder"
            end try
        end tell
        close information window of myFile
    end tell
end run

i 运行 它与:

sudo find / -name *.app -type d -exec osascript myScript.scpt "{}" \;

这项工作非常好:它查找所有“.app”文件,将文件传递给脚本,脚本将打开文件的 "information window",单击 "Open in Low Resolution" 复选框并最后它关闭了 window.

问题:只有当尚未选中时,我才需要单击复选框。

我试过这个解决方案(和其他类似的): Tick a checkbox only if it's not selected

没有任何反应。没有错误。

允许系统事件、Applescripts ecc...运行 安全和隐私设置

这不起作用:

on run argv
    tell application "Finder"
        --set myFile to (POSIX file (item 1 of argv)) as alias
        set myFile to (POSIX file ("/Applications/App Store.app")) as alias
        open information window of myFile
        activate
    end tell
    tell application "System Events" to tell process "Finder"
        try
            set theCheckbox to checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info") of process "Finder"
            tell theCheckbox
                set checkboxStatus to value of theCheckbox as boolean
                if checkboxStatus is false then click theCheckbox
            end tell
        end try
    end tell
    tell application "Finder"
        delay 1
        close information window of myFile
    end tell
end run

它什么都不做。 以下示例(打开应用信息 Window):

tell application "System Events" to tell process "Finder"
    set theCheckbox to checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info")
    tell theCheckbox
        if not (its value as boolean) then click theCheckbox
    end tell
end tell

会产生如下错误:

Can’t make «class valL» of {«class chbx» "Open in Low Resolution" of «class scra» 1 of window "App Store.app Info" of «class pcap» "Finder" of application "System Events"} into type boolean

接下来我能做什么?

此致。

我怀疑问题是您将所有内容都放在了 Finder 的 tell 块中。众所周知,Finder 对脚本非常挑剔。最好尽量少用。

重新安排使用系统事件似乎可以解决问题(虽然我的旧 MacBook Pro 没有视网膜屏幕,所以我不得不使用不同的复选框进行测试)。请注意,您不需要激活 Finder 或信息 window;你可以在后台完成这一切。

on run argv
    set myAppPath to item 1 of argv -- should be a POSIX path

    tell application "Finder"
        open information window of file (POSIX file myAppPath as text)
    end tell
    tell application "System Events"
        set appName to displayed name of disk item myAppPath
        tell process "Finder"
            tell window (appName & " info")
                tell first scroll area
                    if (value of checkbox "Open in Low Resolution" as boolean) is false then
                        click checkbox "Open in Low Resolution"
                    end if
                end tell
                click (first button whose role description is "close button")
            end tell
        end tell
    end tell
end run