Hovered UI AX value as a Variable?

I have been slightly losing my mind trying to figure this out so I would REALLY appreciate any help :pray:

As the title suggests, I want to grab a Hover UI Element value, specifically the "AXTitle" value, so I can use that app name in a subsequent script. I have been able to set variable before but the window was focused, however in this case I want to grab a value from an ICON not a window which has been making my life a bit more complicated.

Hi @frankmak,

Using the below AppleScript, it will output the value of AXTitle of the app icon you're currently hovering over in the Dock.

:information_source: The script starts with delay 4 to give you four seconds to hover over an icon in the Dock before the script runs and outputs the name of the hovered app.

delay 4 -- wait until you hover over an icon in the Dock

-- Ensure System Events (UI scripting) is allowed in Accessibility preferences.
tell application "System Events"
	-- Access the Dock application’s UI elements
	tell process "Dock"
		tell list 1 -- the list of Dock items (apps, folders, Trash, etc.)
			try
				-- Find the first dock item that is not a separator and is 'selected' (hovered)
				set hoveredItem to first UI element whose subrole is not "AXSeparatorDockItem" and selected is true
				-- Retrieve the AXTitle attribute (app name) of the hovered Dock item
				set hoveredName to value of attribute "AXTitle" of hoveredItem
				-- Alternative: AppleScript exposes AXTitle as the 'title' property
				-- e.g., set hoveredName to title of hoveredItem
				display dialog hoveredName with title "Dock Hovered App"

			on error
				-- If no Dock icon is hovered or an error occurs, return a message
				return "No Dock icon is currently hovered or accessible."
			end try
		end tell
	end tell
end tell

Fun fact, ChatGPT Deep Research was able to produce this AppleScript in one shot!

thank you so much!!

1 Like