Making Focus and Click Action AppleScript more Efficient

Hi,

I have some AppleScript that runs fine but is most likely taking longer than it needs to because I’m stating the same info twice - referencing the same button twice. When I try to simplify the script, I can’t get it to work correctly. I’m trying to set a button “keyboard focused” value to True and then Click the button.

Any help is much appreciated!

Here’s the script that works:

tell application "System Events"
	tell process "Logic Pro"
		set value of attribute "AXFocused" of button 1 of UI element 1 of (groups whose value of attribute "AXDescription" is "Tracks header") of scroll area 1 of splitter group 2 of splitter group 1 of group 2 of (groups whose value of attribute "AXDescription" contains "Tracks") of group 2 of (first window whose value of attribute "AXTitle" contains " - Tracks") to true
		click button 1 of UI element 1 of (groups whose value of attribute "AXDescription" is "Tracks header") of scroll area 1 of splitter group 2 of splitter group 1 of group 2 of (groups whose value of attribute "AXDescription" contains "Tracks") of group 2 of (first window whose value of attribute "AXTitle" contains " - Tracks")
	end tell
end tell

Here’s the variation that I can’t get to work:

The error is “Can’t get attribute “AXFocused” of {button 1 of UI element…”

tell application "System Events"
	tell process "Logic Pro"
		set trackFinal to button 1 of UI element 1 of (groups whose value of attribute "AXDescription" is "Tracks header") of scroll area 1 of splitter group 2 of splitter group 1 of group 2 of (groups whose value of attribute "AXDescription" contains "Tracks") of group 2 of (first window whose value of attribute "AXTitle" contains " - Tracks")
		tell trackFinal
			set value of attribute "AXFocused" to true
			perform action "AXPress"
		end tell
	end tell
end tell

I have also tried:

This error is “Can’t set «class focu» of {«class butT» 1 of «class uiel» 1 of «class sgrp» 1 of «class scra» 1 of «class splg» 2 of …”

tell application "System Events"
	tell process "Logic Pro"
		set trackFinal to button 1 of UI element 1 of (groups whose value of attribute "AXDescription" is "Tracks header") of scroll area 1 of splitter group 2 of splitter group 1 of group 2 of (groups whose value of attribute "AXDescription" contains "Tracks") of group 2 of (first window whose value of attribute "AXTitle" contains " - Tracks")
		tell trackFinal
			set focused to true
			perform action "AXPress"
		end tell
	end tell
end tell

Hey @unknownco,

Unfortunately I don’t have Logic Pro, so I can’t test

Here’s how I inspect that sort of thing:

tell application "System Events"
   tell process "Logic Pro"
      set trackFinal to button 1 of UI element 1 of (groups whose value of attribute "AXDescription" is "Tracks header") of scroll area 1 of splitter group 2 of splitter group 1 of group 2 of (groups whose value of attribute "AXDescription" contains "Tracks") of group 2 of (first window whose value of attribute "AXTitle" contains " - Tracks")

      tell trackFinal
         
         set diagnosticsList to {¬
            "----- PROPERTIES -----", ¬
            properties, ¬
            "----- UI ELEMENTS -----", ¬
            UI elements, ¬
            "----- ATTRIBUTES -----", ¬
            attributes, ¬
            "----- ACTIONS -----", ¬
            actions, ¬
            "----- END -----"}
         
      end tell

   end tell
end tell

I would always try to set the focused property before fooling with the attribute.

UI-Scripting can get very tricky and is not without its bugs…

Sometimes you can get around a problem by using “a reference to” an object.

In other words:

tell application "Google Chrome"
   set win1 to a reference to window 1
end tell

Frequently that resolves the same as window 1, but sometimes you can get lucky.

-Chris

1 Like

Hey @ccstone,

Thanks for the ideas, I found something that works using your “a reference to” trick.

Yes, UI scripting is ridiculous. I’m only having to modify this script because for ‘whatever reason’ in the latest version of Logic Pro a simple “click button” does not work unless you first set focused to true. Is that typical? In previous versions I never had to set this and I actually only have to set this with specific buttons in this version of Logic Pro. Other buttons work fine without first setting focused to true.

Thanks again!

tell application "System Events"
	tell process "Logic Pro"
		set trackFinal to a reference to button 1 of UI element 1 of (groups whose value of attribute "AXDescription" is "Tracks header") of scroll area 1 of splitter group 2 of splitter group 1 of group 2 of (groups whose value of attribute "AXDescription" contains "Tracks") of group 2 of (first window whose value of attribute "AXTitle" contains " - Tracks")
		tell trackFinal
			set focused to true
			click
		end tell
	end tell
end tell

Glad you got things working!

Nyet. I haven’t seen that one before.

But I’ve seen cases where click worked and AXPress didn’t and vice versa.

I’ve also seen cases where I had to click the button more than once.