Get UI Elements of application opened by Finder using AppleScript - applescript

set classNames to {}
tell application "Finder"
tell front window of (first application process whose frontmost is true)
set uiElems to entire contents as list
end tell
end tell
When I run this in Script Editor, it returns:
{group 1 of window "test" of application process "Script Editor" of application "Finder", pop up button 1 of group 1 of window "test" of application process "Script Editor" of application "Finder", pop up button 2 of group 1 of window "test" of application process "Script Editor" of application "Finder", checkbox 1 of window "test" of application process "Script Editor" of application "Finder", checkbox 2 of window "test" of application process "Script Editor" of application "Finder", checkbox 3 of window "test" of application process "Script Editor" of application "Finder", busy indicator 1 of window "test" of application process "Script Editor" of application "Finder", static text "Running…" of window "test" of application process "Script Editor" of application "Finder", splitter group 1 of window "test" of application process "Script Editor" of application "Finder", splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", scroll area 1 of splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", text area 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", scroll bar 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", splitter 1 of splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", group 1 of splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", static text "Result" of group 1 of splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", text area 1 of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", scroll bar 1 of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", splitter 1 of splitter group 1 of window "test" of application process "Script Editor" of application "Finder", toolbar 1 of window "test" of application process "Script Editor" of application "Finder", button 1 of toolbar 1 of window "test" of application process "Script Editor" of application "Finder", button 2 of toolbar 1 of window "test" of application process "Script Editor" of application "Finder", button 3 of toolbar 1 of window "test" of application process "Script Editor" of application "Finder", button 4 of toolbar 1 of window "test" of application process "Script Editor" of application "Finder", group 1 of toolbar 1 of window "test" of application process "Script Editor" of application "Finder", group 1 of group 1 of toolbar 1 of window "test" of application process "Script Editor" of application "Finder", checkbox 1 of group 1 of group 1 of toolbar 1 of window "test" of application process "Script Editor" of application "Finder", checkbox 2 of group 1 of group 1 of toolbar 1 of window "test" of application process "Script Editor" of application "Finder", static text "—" of window "test" of application process "Script Editor" of application "Finder", button 1 of window "test" of application process "Script Editor" of application "Finder", button 2 of window "test" of application process "Script Editor" of application "Finder", button 3 of window "test" of application process "Script Editor" of application "Finder", menu button "Edited" of window "test" of application process "Script Editor" of application "Finder", image "test" of window "test" of application process "Script Editor" of application "Finder", static text "test" of window "test" of application process "Script Editor" of application "Finder"}
But when I try to run the script as an app and try to open an application through Finder the error is:
Can’t get window 1 of application process 1 whose frontmost = true. Invalid index. (-1719)
Can someone help with this? I am new to AppleScript and don't have a clue.

Related

How to inspect elements and click on Apple Script

I'm pretty new with apple scripts, and I'm trying to change the focus mode in the system preferences via Apple Script (the shortcut way is not an option). And I found one script that enables the Don't Disturb mode. But it doesn't change the mode.
How can I inspect the window and click select the focus mode?
Here is the script:
tell application "System Preferences"
set current pane to pane "com.apple.preference.notifications"
end tell
delay 0.5
tell application "System Events"
tell application process "System Preferences"
click radio button "Focus" of tab group 1 of window "Notifications & Focus"
click checkbox 1 of group 1 of tab group 1 of window "Notifications & Focus"
set theCheckbox to checkbox 1 of group 1 of tab group 1 of window "Notifications & Focus"
tell theCheckbox
set theCheckboxStatus to value of theCheckbox as boolean
if theCheckboxStatus is true then
display notification "Do not disturb is on"
else
delay 0.2
display notification "Do not disturb is off"
end if
end tell
end tell
end tell
And this is the window:

How to click on iCloud button in System Preferences using applescript

I am trying to get the script to click the iCloud button, but I am getting the syntax/logic incorrect.
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preferences.internetaccounts"
delay 1
end tell
tell application "System Events"
tell process "System Preferences"
click button "iCloud" of window "Internet Accounts"
end tell
end tell
Help is much appreciated. I am on Monterey (12,4) iMac.
Try this.
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preferences.internetaccounts"
end tell
tell application "System Events" to tell process "System Preferences"
repeat until exists of UI element "iCloud" of UI element 1 of row 1 of ¬
table 1 of scroll area 1 of group 1 of window "Internet Accounts"
delay 0.1
end repeat
click button "iCloud" of UI element 1 of row 1 of table 1 of scroll area 1 of ¬
group 1 of window "Internet Accounts"
end tell

Enable Voice Control. AppleScript

I try to create an Apple Script to Enable Voice Control in a Mac.
This is in: System Preferences > Accessibility > Voice Control > Enable Voice Control
I think I get close. But I do not know how to call the left menu "Voice control"
This is what I have tried and do not work:
tell application "System Events"
click checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility" of application process "System Preferences" of application "System Events"
end tell
You asked in the comments if there was another way to get the solution. Here's a way to revise your code. First, I will explain the solution, step by step.
Update: This if statement, as pointed out in user3439894's very helpful comments below, is actually necessary, for several reasons. It should always be included.
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
This is done so the script will not fail if it is running. Note that the user also pointed out that killall should be used to ensure that the process completely terminates.
It allows for predictability in the behavior of the application.
user3439894 also pointed out that I should add this code block, in case there is a timing issue, resulting in the application trying to reopen while it is in the process of closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
Next, in this snippet, you click the Accessibility button in System Preferences
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2 -- while loads
Next, you select the Voice Control row in Settings.
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
And finally, click the checkbox. As I explained in the comments, groups are used to organize elements.
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
Full (updated) code:
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
activate application "System Preferences"
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2.0 -- delay while page loads
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
Corrections I made to your code:
You cannot reference elements from windows that are not already opened in an app (like you did with the checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility"), so first I opened the Accessibility window (click a button) -> Voice Control Window (select the row), and then found/clicked the checkbox.
Then, I looked at the hierarchy of the elements to figure out where the checkbox is in that Voice Control Window. I found out that it is inside group 1 of window 1 (Voice Control Window), so I clicked it.
I also added delays (the app needs time to load after every click/select).
Also, here is how I figured out where the elements are.
If you don't already use it, there is this built-in app called Accessibility Inspector to help you locate elements.
You can also use get in applescript. For example,
tell application "System Events"
get buttons of scroll area 1 of window 1 of process "System Preferences"
end tell
would return:
... button "Accessibility" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", button "Screen Time" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events",...
Another example:
tell application "System Events"
get UI elements of window 1 of process "System Preferences"
end tell
and here's some of the output
...scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", toolbar 1 of window "System Preferences" of application process "System Preferences" of application "System Events..."
A very helpful tool.

How to close Safari preference window using AppleScript?

I am using the below script to enable developer menu.
tell application "Safari"
tell application "System Events"
tell process "Safari"
click menu item "Preferences…" of menu 1 of menu bar item "Safari" of menu bar 1
click button "Advanced" of toolbar 1 of window 1
click checkbox "Show Develop menu in menu bar" of group 1 of group 1 of window 1
-- delay 2
keystroke "w" using {command down} -- > not working
end tell
end tell
end tell
How to close the preference window? With keystroke "w" using {command down}, I am getting cannot The document cannot be closed while the script is running. error.
Also how to enable checkbox only if it is not enabled? Currently, if I run the script twice, it toggles.
Just click the first button of the first window and check the value of the checkbox
tell application "Safari"
tell application "System Events"
tell process "Safari"
click menu item "Preferences…" of menu 1 of menu bar item "Safari" of menu bar 1
click button "Advanced" of toolbar 1 of window 1
tell checkbox "Show Develop menu in menu bar" of group 1 of group 1 of window 1
if value is 0 then click it
end tell
click button 1 of window 1
end tell
end tell
end tell
This AppleScript code works for me using the latest version of macOS Mojave.
tell application "System Events"
tell application process "Safari"
set frontmost to true
end tell
keystroke "." using {command down}
end tell

XCode 4 Debugger: Attach to process from command line

I would like to launch an application from the command line and immediately attach the graphical debugger in XCode 4 to it, without having to click in the Xcode menu Product->Attach To Progress. Is it possible to script this using a bash or Apple script?
This seems to work in the Xcode-Beta Version 6.2 (6C121). For Xcode, just change "Xcode-Beta" to Xcode. Also, you'll want to change the process name "Staging" to the process you'd like to attach.
tell application "Xcode-Beta"
activate
end tell
tell application "System Events"
tell application process "Xcode"
click (menu item "By Process Identifier (PID) or Name…" of menu 1 of menu item "Attach to Process" of menu 1 of menu bar item "Debug" of menu bar 1)
end tell
tell application process "Xcode"
set value of text field 1 of sheet 1 of window 1 to "Staging"
end tell
tell application process "Xcode"
click button "Attach" of sheet 1 of window 1
end tell
end tell
Worked for Xcode 12
tell application "Xcode"
activate
end tell
tell application "System Events"
tell application process "Xcode"
click (menu item "Attach to Process by PID or Name…" of menu 1 of menu bar item "Debug" of menu bar 1)
end tell
tell application process "Xcode"
set value of text field 1 of sheet 1 of window 1 to "Your App/Process Name"
end tell
tell application process "Xcode"
click button "Attach" of sheet 1 of window 1
end tell
end tell
tell application "our App/Process Name"
activate
end tell

Resources