Luca Reggiannini
Luca Reggiannini

Reputation: 73

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

I want to enable option "Open in Low Resolution" for all ".app" files in the system.

This is my Applescript code:

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 run it with:

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

This work very good: it looks for all ".app" files, pass the file to the script, the script will open the "information window" of the file, clicks the "Open in Low Resolution" checkbox and finally it closes the window.

Problem: i need to click on the Checkbox ONLY if is not already selected.

I tried with this solution (and other similar): Tick a checkbox only if it's not selected

Nothing happens. No errors.

System Events, Applescripts ecc... are allowed to run on Security And Privacy Settings

This does not works:

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

It simply does nothing. The following sample (with an app Info Window opened):

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

Will produce the following error:

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

What can i do next?

Best regards.

Upvotes: 0

Views: 538

Answers (1)

Ted Wrigley
Ted Wrigley

Reputation: 3184

I suspect the problem is that you've put everything inside a Finder tell block. The Finder is notoriously finicky about scripting; best to use it as little as possible.

Rearranging things to use System Events seems to fix the problem (though admittedly my old MacBook Pro doesn't have a retina screen, so I had to test this using a different checkbox). Notice that you don't need to activate the Finder or the information window; you can do this all in the background.

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

Upvotes: 1

Related Questions