Checkboxes GUI Automation

I have managed to integrating checkbox ticking in my scripts. But for the life of me, I cannot figure out the syntax for ticking a checkbox if is not already ticked. I’ve tried boolean but it always comes back with a message saying “missing value”.

Is there a preset value for a ticked or unticked checkbox that AppleScript understands?

It is easy enough to automate a click on as it, as a graphical object on a window, but get the script to intelligent understand the current state of the checkbox still eludes me.

If anyone know anything please share!

Thanks!

A

Hi,

the value of the UI element checkbox is either 1 or 0

Many thanks for the reply back Stefan.

I must still be missing something. Ok I put this hypothetical in a test script:

set theImportCheckbox to checkbox “Import After Bounce” of window “Bounce”
if the value of the theImportCheckbox is 0 then
click theImportCheckbox

The reply I am getting is:

→ missing value

Here is a handler extracted from a script I posted elsewhere.

on ruleTitleAndLegend(titleMode, legendMode)
	# xMode = 1 --> check the box
	# xMode = 0 --> uncheck the box
	tell application "System Events" to tell process "Numbers"
		set frontmost to true
		# Don't ask why it's scroll area 3 in Numbers when it's scroll area 1 in Keynote
		-- name of checkbox of scroll area 3 of window 1 # instruction useful to get infos, useless in real life
		--> {"Titre", "Légende", "Données masquées"} (with pie chart)
		--> {"Titre", "Légende", "Bordure", "Données masquées"} (with columns chart)
		try
			if value of checkbox 1 of scroll area 3 of window 1 is not titleMode then
				click checkbox 1 of scroll area 3 of window 1 # rule Title
			end if
		end try
		try
			if value of checkbox 2 of scroll area 3 of window 1 is not legendMode then
				click checkbox 2 of scroll area 3 of window 1 # rule Legend
			end if
		end try
	end tell # application "System Events" to tell process "Numbers"
end ruleTitleAndLegend

I ran it so often that I may guarantee that it works.

I would not be surprised to learn that your checkbox is not an UI element of the window “Bounce” but belongs to a (container) UI element of this window.

Don’t forget that Apple give us a powerful tool named Accessibility Inspector available as :

“Xcode.app:Contents:Applications:Accessibility Inspector.app:”

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mercredi 7 décembre 2016 14:39:20

How can I tell the difference?

According to the Accessibility Inspector this checkbox has these attributes:

AXRole: AXCheckBox
AXRoleDescription: check box

By the way, does anyone know what AX stands for?

Thanks

It’s just a prefix for a name, it doesn’t matter here.

Is it a way to get the application which you try to drive ?

If I run it while the print dialog is open in TextEdit it will display :

AXApplication
AXWindow:AXStandardWindow
AXSheet
AXCheckBox

If I run it while Numbers is running as it was written in the handler posted in my late message, the Inspector would display :
AXApplication
AXWindow:AXStandardWindow
AXScrollArea
AXCheckBox

If the checkbox really belongs to the main window as you assume in your message, the Inspector would display:
AXApplication
AXWindow:AXStandardWindow
AXCheckBox

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mercredi 7 décembre 2016 17:47:29

I guess it’s a phonetic two letter Cocoa prefix of Accessibility

This is exactly what it says

Many thanks for that. While trying to convert this script for my case it would be great if you could explain a couple of things for me. I have been phenomenally under-slept so please bare with me if the questions are very pedestrian!

You create the variable ruleTitleAndLegend(titleMode, legendMode). In this particular case, what are the variables titleMode and legendMode refer to? Should I assume these are program-specific relating to Numbers?

What I do not understand is if checkbox values are 0 for not checked and 1 for checked (or vice versa - it doesn’t matter) why a simple IF rule is not enough?

I don’t define a variable ruleTitleAndLegend, this name is the name of a handler which would be called by :

my ruleTitleAndLegend(theTitleMode, theLegendMode)

I assumed that the two lines of comments :
[format] # xMode = 1 → check the box
# xMode = 0 → uncheck the box[/format]
were clear enough.
If you use :

my ruleTitleAndLegend(1, 0)

the script will check the title checkbox and uncheck the legend checkbox
If you use :

my ruleTitleAndLegend(0, 1)

the script will uncheck the title checkbox and check the legend checkbox
If you use :

my ruleTitleAndLegend(1, 1)

the script will check both checkboxes
If you use :

my ruleTitleAndLegend(0, 0)

the script will uncheck both checkboxes

If you don’t want to use a handler (wich would be a bad idea) you must code:

# the beginning of your script
set titleMode to 1 # will check the title checkbox
set legendMode to 0 # will uncheck the legend checkbox
 # xMode = 1 --> check the box
       # xMode = 0 --> uncheck the box
       tell application "System Events" to tell process "Numbers"
           set frontmost to true
           # Don't ask why it's scroll area 3 in Numbers when it's scroll area 1 in Keynote
           -- name of checkbox of scroll area 3 of window 1 # instruction useful to get infos, useless in real life
           --> {"Titre", "Légende", "Données masquées"} (with pie chart)
           --> {"Titre", "Légende", "Bordure", "Données masquées"} (with columns chart)
           try
               if value of checkbox 1 of scroll area 3 of window 1 is not titleMode then
                   click checkbox 1 of scroll area 3 of window 1 # rule Title
               end if
           end try
           try
               if value of checkbox 2 of scroll area 3 of window 1 is not legendMode then
                   click checkbox 2 of scroll area 3 of window 1 # rule Legend
               end if
           end try
       end tell # application "System Events" to tell process "Numbers"
# end of your script

Of course, you will have to change the name of the process according to your needs
and, if, as you wrote, the checkboxes are really in the main window you will have to drop the “of scroll area 3” part of the instructions

It will become :

# the beginning of your script
set titleMode to 1 # will check the title checkbox
set legendMode to 0 # will uncheck the legend checkbox
 # xMode = 1 --> check the box
       # xMode = 0 --> uncheck the box
       tell application "System Events" to tell process "yourProcess"
           set frontmost to true
         name of checkbox of window 1 # instruction useful to get infos, useless in real life
           --> {"name1", "name2", "name3"}
           try
               if value of checkbox 1 of window 1 is not titleMode then
                   click checkbox 1 of window 1 # rule Title
               end if
           end try
           try
               if value of checkbox 2 of window 1 is not legendMode then
                   click checkbox 2 of window 1 # rule Legend
               end if
           end try
       end tell # application "System Events" to tell process "yourProcess"
# end of your script

If these explanations aren’t sufficient, it would be useful to tell - as I already asked - the way to get the used application so that me or an other helper try to see what is the wrongdoer.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) jeudi 8 décembre 2016 16:26:39