Small UI Scripting Question

Hi again folks,

The goal of this script is to toggle the Safari->Preferences->Advanced->Never Use Font Sizes Less Than-> combo box value between “12” and “18”.


activate application "Safari"
tell application "System Events"
	tell process "Safari"
		perform action "AXPress" of menu item "Preferences." of menu 1 of menu bar item "Safari" of menu bar 1
		perform action "AXPress" of button "Advanced" of tool bar 1 of window 1
		tell window "Advanced"
			tell combo box 1 of group 1 of group 1
				if value is "12" then
					set value to "18"
				else
					set value to "12"
				end if
			end tell
		end tell
		delay 2
		keystroke tab
	end tell
end tell
tell application "Safari" to set index of window 2 to 1 -- move prefs window out of foreground

After the toggle is run a couple of times, it is pretty reliable. But on initial opening, the first couple of times the toggle is run, it changes the value in the combo box but doesn’t actually do anything.

I tried a simpler method a while back of just using the “defaults” command-line utility to change the value but that doesn’t seem to trigger the actual action either.

My question is: is there a better way to select or click an element in a combo box? I think actually clicking it would work reliably but I don’t know what UI element to apply the “click” verb to.

Also if anyone has a more elegant method of doing a “send to back” of the Preferences window I would love to hear it. The reason I do not just close it is that the misbehavior occurs on the first couple of toggles after opening the preferences window.

Take care and thanks…

Johnny

I just tried it. Looks like sometimes the value changes in the field but it hasn’t taken effect. I would add a keystroke of Enter/Return or just close the preference window instead of making it inactive. Or do both to make sure

activate application "Safari"
tell application "System Events"
   tell process "Safari"
       perform action "AXPress" of menu item "Preferences." of menu 1 of menu bar item "Safari" of menu bar 1
       perform action "AXPress" of button "Advanced" of tool bar 1 of window 1
       tell window "Advanced"
           tell combo box 1 of group 1 of group 1
               if value is "12" then
                   set value to "18"
               else
                   set value to "12"
               end if
           end tell
       end tell
       delay 2
       key code 76 --Enter
       delay 0.5
       keystroke "w" using {command down}
   end tell
end tell

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Sorry, just tested it and that didn’t work consistently either…

Thy this instead. THis seems to force it to take the new value:

activate application "Safari"
tell application "System Events"
	tell process "Safari"
		perform action "AXPress" of menu item "Preferences." of menu 1 of menu bar item "Safari" of menu bar 1
		perform action "AXPress" of button "Advanced" of tool bar 1 of window 1
		tell window "Advanced"
			tell combo box 1 of group 1 of group 1
				if value is "12" then
					set value to "18"
				else
					set value to "12"
				end if
			end tell
		end tell
		delay 0.5
		keystroke tab using {shift down}
		delay 0.5
		keystroke tab
		delay 0.5
		keystroke space
		delay 0.5
		keystroke space
		delay 0.5
		keystroke "w" using {command down}
	end tell
end tell

Delay is your friend. If you have a decently fast machine, I’ve had luck with delays as short as 0.3. You might also be able to experiment with figuring out which delays you actually need and which you don’t.

OK this works. Had to toggle the checkbox to make the combo box “value” change be consistently accepted. What a kludge, LOL.

I’d still like to know the proper syntax for selecting an item from a combo box.

Telling the window to close itself didn’t work either. Had to tell Safari to close it.

I tested with decreasing delays and found that I did not need any at all for checking and unchecking the box.

activate application "Safari"
tell application "System Events"
	tell process "Safari"
		perform action "AXPress" of menu item "Preferences." of menu 1 of menu bar item "Safari" of menu bar 1
		perform action "AXPress" of button "Advanced" of tool bar 1 of window 1
		tell window "Advanced"
			tell combo box 1 of group 1 of group 1
				if value is "12" then
					set value to "18"
				else
					set value to "12"
				end if
			end tell
			
			repeat 2 times
				perform action "AXPress" of checkbox "Never use font sizes smaller than" of group 1 of group 1
			end repeat
		end tell
	end tell
end tell

tell application "Safari" to close front window

Thank you everyone for the assistance!

Thanks a bunch, Jacques! I’m the type of guy that likes to find the most elegant solution and I am never satisfied with just getting something that “works.” This is perfect.

And I think I learned something about using UI Browser. When you select the combo box, UI Browser shows the Actions of that element as being “Confirm” and “Show Menu.” So now I know that “AXConfirm” corresponds to the “Confirm” Action listed. Pretty slick. PreFab should publish a list of actions and what they do.

GUI scripting is never an elegant solution, Johnny :wink: