🤯💡 Boost your automation skills with these UIA tips and patterns! 📈
Channel: AUTOHOTKEY Gurus
Date: 13/07/2022

We had another EPIC call with Descolada and he gave some great pointers when working with UI Automation.  This is a video you'll be coming back to over and over, I guarantee it❗  Learn more about UI Automation by going here: https://the-Automator.com/UIA

TLDR: Using UI Automation Library and Accessibility Insights Inspect, you can create hotkeys to automate programs in the background.

1. 00:00 🤖 Automate programs in the background using UI Automation Library to separate elements, patterns, and text ranges.
 1.1 Learn tips and tricks on how to work with patterns in UI automation and how to automate a program in the background.
 1.2 UI Automation Library separates elements, patterns, and text ranges into their own classes with their own properties and methods.

2. 02:14 🔑 Use Accessibility Insights Inspect and UI automation to test the clickability and toggleability of the "add to library" button in Spotify.
 2.1 We can use AutoHotkey to create a hotkey to toggle the play button and like a song in Spotify, which is a web app based program.
 2.2 Googled it, clicked on an episode, and selected 148 148 to add to the list.
 2.3 The Accessibility Insights Inspect tool is best for testing the clickability and toggleability of the "add to library" button.
 2.4 Using the Accessibility Insights tool, you can check the name and tree of an element to determine if it can be clicked.
 2.5 Using UI automation, if an action cannot be performed on a button, it can be checked on one of its parents to see if it can be done with it.
 2.6 Click the steps recorder to record your actions and it will show what you did.

3. 16:00 🔑 Use the Runtime ID to identify elements in code, and be careful not to reuse the same element as it may change.
 3.1 Export the file to XML to view it, as it is not visible in the HTML.
 3.2 Steps Recorder may not launch due to a known bug or antivirus software, and changing the registry item in Windows 10 may help.
 3.3 We can target the image or the button by copying the name and using it in code.
 3.4 The runtime ID of a button will stay the same even if the name of the button changes, allowing you to identify the same button without restarting the program.
 3.5 Load library and add a new hotkey, being careful not to reuse the same element as it may change when navigating to another song.
 3.6 Verify if runtime ID is correct by inspecting and running it again.

4. 23:54 🔑 Create a hotkey to toggle the Spotify element using UI Automation and the HK Executable function.
 4.1 The button gets destroyed and recreated when you come back, so it can only be used once.
 4.2 Create a hotkey to toggle the Spotify element by using the UI Automation and the HK Executable function.
 4.3 If not button, then not saved equals true and try to get the element to toggle the button using the toggle method.
 4.4 Get the current pattern from the element using the getCurrentPattern() method.
 4.5 Microsoft created two methods with similar names, GetCurrentPattern and UsePattern, to get the highest version number of a pattern and check what version it is.
 4.6 If the button is found, toggle it; otherwise, return.

5. 35:59 🔑 Set up a hotkey to toggle the like/dislike button and the play/pause button using invoke and switch statements.
 5.1 Using the same script, a hotkey can be set up to toggle the like/dislike button and the play/pause button.
 5.2 Invoke is a generalized way to use the default action for a particular element.
 5.3 The default action for a button is to click, while for a toggle it is to toggle, and for UI automation the invoke is limited to elements with singular actions.
 5.4 To use a toggle pattern, you must have an invoke pattern, so check the document for the invoke pattern and add buttons for previous and next.
 5.5 Using a switch statement, I can assign different actions to different hotkeys and have a default action if no hotkey is found.
 5.6 Press F3 for "previous" and F4 for "next".

6. 45:36 🔎 Searching in a specific area of Spotify, you can limit your search and create hotkeys for buttons using patterns and positioning.
 6.1 Search can be limited to a specific element by searching for buttons by type and description.

7. 58:23 🤖 Use GetCurrentPropertyValue with the Property ID from the UIA Enumerations to check if an element supports a certain pattern.
 7.1 Using UIAutomation, you can automate programs that cannot be controlled with a mouse click, and you can list the available patterns with the library.
 7.2 Use GetCurrentPropertyValue with the Property ID from the UIA Enumerations to check if an element supports a certain pattern.

8. 01:12:16 🤔 When automating with AutoHotkey, make sure to restore the window, perform the action, and minimize it again to avoid bringing it to the front.
 8.1 Activate the window, move it outside of the view, perform the action, and restore it to its original state to avoid bringing it to the front.