AppleScript to change Audio MIDI Settings - macos

Quick and simple AppleScript question.
I need to have an applescript that ticks the checkbox in the Audio MIDI Setup application. I am completely new to applescript.
The dialog looks like this, I need to check the 2nd checkbox.
Any hints?
Thank you!

I do not have "Soundflower" in my own system audio devices so I don't know I can get the exact values to create the AppleScript exactly the way you need it. However, this script will select the second checkbox for "Built In Output".
Here is the script that will accomplish this:
tell application "Audio MIDI Setup"
activate
end tell
delay 2
try
tell application "System Events"
tell process "Audio MIDI Setup"
select row 2 of outline 1 of scroll area 1 of splitter group 1 of window "Audio Devices"
-- change the value in the beginning of that line from "select row 2" to "select row 3" This should select the "Soundflower Output Devicee"
click checkbox 1 of UI element 4 of row 3 of outline 1 of scroll area 1 of tab group 1 of splitter group 1 of window "Audio Devices"
end tell
end tell
end try

Related

How to programmatically change audio output to built in Mac speakers ALWAYS [Shell]

So I've seen answers like these but what I'm trying to do is programmatically ALWAYS set the audio output to built-in speakers regardless of which row item the Mac is currently tuned to.
In the below code (cited from the above link), the user selects between items 3 and 4. We know that built in speakers are always item 1, so is there a simpler way to achieve this where you send a shell command to always set the audio output to Built-in speakers?
(*
Applescript to toggle between two sound outputs by Line number, ¬
as they appear in the Sound Control Panel. Based on code by ¬
Arthur Hammer http://apple.stackexchange.com/a/209434/85275
*)
set internal to 1 --internal speakers
set appletv to 3 --Follows internal speakers and separator line for Airplay devices
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
end tell
tell application "System Events"
tell application process "System Preferences"
repeat until exists tab group 1 of window "Sound"
end repeat
tell tab group 1 of window "Sound"
click radio button "Output"
if (selected of row internal of table 1 of scroll area 1) then
set selected of row appletv of table 1 of scroll area 1 to true
else
set selected of row internal of table 1 of scroll area 1 to true
end if
end tell
end tell
end tell
tell application "System Preferences" to quit

how to set safari zoom level to 100% from the below shared image/steps with applescript or python script for macos desktop?

safari->preferences->websites->Page Zoom-> click on "when visiting other websites" and make this to 100% if option ranges in number from 50% to 200%
Need an applscript for this.
click on this to see the image where i am looking for the zoom setting to 100%
AppleScript 1: Set Safari default Page Zoom to 100%
tell application "System Events" to tell application process "Safari"
set frontmost to true
keystroke "," using command down -- Open Safari Preferences window
click button "Websites" of toolbar 1 of window 1
-- Need for seeing, selecting or clicking groups (or panes) under Websites window
tell group 1 of group 1 of window "Websites"
select row 5 of table 1 of scroll area 1 of group 1
-- group 1: left pane. row 5: Page Zoom
click pop up button of group 2
-- group 2: right pane. Revealed from the command "UI element" in AppleScript 2 below
click menu item "100%" of menu 1 of pop up button of group 2
end tell
keystroke "w" using {shift down, command down}
-- Or, keystroke "w" using command down: close Preferences window
end tell
MacOS 11.6.8 Big Sur
Explanation: see comments after -- on each lines.
Add a line "delay 1" between action command (Eg: keystroke, click and select) lines if your Mac needs.
You may script changes of Safari and other apps' Preferences in a similar way.
AppleScript 2: Find out all UI elements after some UI actions
To find out which UI elements (such as window, toolbar, button, group, scroll area, table, row, pop up button, menu, menu item) are available after some UI actions, put the command "UI element" under their parent UI element in Script Editor (NOT Automator). Eg:
tell application "System Events" to tell application process "Safari"
set frontmost to true
keystroke "," using command down
click button "Websites" of toolbar 1 of window 1 -- Need
UI element of group 2 of group 1 of group 1 of window "Websites"
end tell
Result: (below the script in Script Editor)
--> {static text "Control the page zoom level on the websites below:" ...,
group 1 ...,
pop up button 1 of group 2 of group 1 of group 1 of window "Websites" of application process "Safari" of application "System Events",
-- Useful in AppleScript 1 above
static text "When visiting other websites:" ...,
button "Remove" ...}
Attention: the action command 'click button "Websites" ...' is required to display the UI elements after it.
Note: Pls encourage new contributor and vote up if you find this answer useful. Thanks!

Use AppleScript to select eraser in Powerpoint

First, here is a video explanation of my problem.
I am a teacher searching for a way to easily access the Eraser tool while giving presentations with PowerPoint. Partly following this guide, I have tried using Automator to record my actions when I select the eraser, saving this as a Quick Action, and then assigning a keyboard shortcut to the action.
Unfortunately, I get the error:
The action "Watch Me Do" encountered an error: "Application Powerpoint is not running when it is expected to"
Or, if I first copy the code from the Automator action, paste it into a Run AppleScript file, then set a shortcut to execute the file, I get this:
The action “Run AppleScript” encountered an error: “The operation couldn’t be completed. (com.apple.Automator error -212.)”
Can anyone help please? Thank you!
Alternatively, if anyone knows of a better way to achieve my goal, please do share.
Sometimes you need to go through each process one step at a time to isolate what is generating the errors. First let’s see if this code will select the eraser tool.
Paste this following AppleScript code into a new Script Editor.app document. You can run the code directly from that document. If the code works for you then tried pasting it into your Automator workflow.
tell application "Microsoft PowerPoint"
activate
repeat until frontmost
delay 0.1
end repeat
end tell
tell application "System Events"
repeat while not (exists of radio button 3 of tab group 1 of window 1 of application process "PowerPoint")
delay 0.1
end repeat
if not (exists of menu button "Eraser" of group 1 of scroll area 1 of tab group 1 of window 1 of application process "PowerPoint") then
click radio button 3 of tab group 1 of window 1 of application process "PowerPoint"
delay 0.1
if value of menu button 1 of group 1 of scroll area 1 of tab group 1 of window 1 of application process "PowerPoint" is 0 then
click menu button "Eraser" of group 1 of scroll area 1 of tab group 1 of window 1 of application process "PowerPoint"
end if
else
delay 0.1
if value of menu button 1 of group 1 of scroll area 1 of tab group 1 of window 1 of application process "PowerPoint" is 0 then
click menu button "Eraser" of group 1 of scroll area 1 of tab group 1 of window 1 of application process "PowerPoint"
end if
end if
end tell
Make sure you add Automator.app, Script Editor.app, System Events.app, and PowerPoint.app… as well as your script (if it was saved as an .app) to the System Preferences > Security > Privacy > Accessibility application list. Then add again those same items to the System Preferences > Security > Privacy > Full Disk Access application list.
Also, if you save your script or Automator workflow as an application, anytime you make changes and save that file again, you will need to go back and re add it to those application lists in the security preferences again.

How do I use applescript to get this checkmark clicked

So I would like to use applescript to click the checkmark that says "allow access for all users" in System Preferences then sharing then remote login. As shown in this screenshot:
What it should look like if it works
Anyways so I've gotten really close to being able to get it clicked but i haven't yet succeeded here is the script that I have so far:
tell application "System Preferences"
set current pane to pane "com.apple.preferences.sharing"
end tell
tell application "System Events"
tell process "System Preferences"
tell checkbox 2 of row 4 of table 1 of scroll area 1 of group 1 of window "Sharing" to if value is 0 then click
end tell
end tell
I just cant quite get it to click the right checkbox, if you guys could help me out that would be awesome. thanks!
(SOLVED)
You need to do 2 steps :
1) find the correct row containing the "Remote Login" service. This may not be always a fix number (i.e. row 5 in your example). then select the row and click the check box to activate
2) once the service is activated, click of the "all users" radio button
The script bellow does this with comments :
set SWindow to "Sharing"
set SRow to "Remote Login"
tell application "System Preferences"
set current pane to pane "com.apple.preferences.sharing"
end tell
tell application "System Events"
tell process "System Preferences"
-- get the correct row number of the sharing services
repeat with theRow in every row of table 1 of scroll area 1 of group 1 of window SWindow
if value of UI element 2 of theRow is SRow then -- this is the "Remote Login" row
select theRow -- select the row
if value of checkbox 1 of theRow is 0 then click checkbox of theRow -- click on activate check box if not yet set
end if
end repeat
delay 1 -- time to display the correct pane with the users for sharing
click radio button 1 of radio group 1 of group 1 of window SWindow -- click the button "all users"
end tell
end tell
Note : I defined and assigned 2 variables SWindow and SRow because it is easier for me to test with my system (not English language, then not same string values !).
Smart solution without GUI scripting (requires admin password):
do shell script "launchctl load -w /System/Library/LaunchDaemons/ssh.plist" with administrator privileges
do shell script "/usr/bin/dscl . -delete /Groups/com.apple.access_ssh" with administrator privileges
Caveat: The dscl line deletes the created group in "Only these users" completely.

Applescript delay so long when run click button command

i was write a simple code to click share button on Notes applicaion
set upArrow to ASCII character 30
activate application "Notes"
tell application "System Events"
tell process "Notes"
set frontmost to true
click button 2 of group 2 of splitter group of window 1
keystroke upArrow
keystroke return
end tell
end tell
the problem is
click button 2 of group 2 of splitter group 1 of window 1 of application process "Notes" --> missing value
it need 6s to complete run this and return error missing value
but if i dont add keystroke or any command follow that, this code work perfectly
I also get that delay and it returns missing value. But could you click a menu bar item instead?
activate application "Notes"
tell application "System Events" to tell process "Notes"
click menu item "Email" of menu 1 of menu item "Share" of menu "File" of menu bar 1
end tell
set focused of button 2 of group 2 of splitter group 1 of window 1 to true
keystroke space
or
set value of attribute "AXFocused" of button 2 of group 2 of splitter group 1 of window 1 to true
keystroke space
Source:
https://lists.apple.com/archives/Accessibility-dev/2006/Oct/msg00013.html
There is a thread that seems to describe the same bug that appears to be limited to specific software. It is on MacScripter.net: Script delays on clicking button (controlling Sys Prefs pane)
Note the most recent post that describes a delay in FileMaker. It seems like the only work-around when software exhibits this bug is to do something else to open the new window. If the only way to run the desired function is via a button, one (terrible) option is clicking at coordinates relative to the corner of the front window. Obviously this will easily fail if the content can move relative to the corner or if a new version of the software moves the button desired. Any other ideas?
UPDATE, 2023-02-13:
In case anybody is still dealing with this bug, here's some useful AppleScript to get the coordinates of the object you'd like to click. You can then feed those into some coordinate-clicking utility, like cliclick (binary), Python code, Keyboard Maestro, and so on.
tell application "System Events"
set someObject to <REFERENCE TO YOUR OBJECT>
-- e.g. op's: button 2 of group 2 of splitter group of window 1 of application process "Notes"
set {xCoord, yCoord} to position of someObject
set {xSize, ySize} to size of someObject
end tell
set objOffset to round (minNum({xSize, ySize}) / 2) rounding down
set xClick to xCoord + objOffset
set yClick to yCoord + objOffset

Resources