Applescript automation flipping checkbox not always working - user-interface

I have the following applescript that is acting strangely. If the file sharing checkbox is unchecked it will check it everytime. If the file sharing checkbox is already checked it will sometimes uncheck it. then when it comes to the if statement that is to determine if the current state of the file sharing checkbox(this is to take place after the checking action) it never reads it as checked.
here is the thought proccess:
open system prefs (works)
show sharing pane (works)
click the file sharing check box, row 3. (sort of works. checks if unchecked. does not uncheck)
get current state of file sharing box, generate appropriate message(does not work)
quit system prefs(works)
Display message about what action was taken(works)
Apple Script
tell application "System Preferences"
activate
reveal (pane id "com.apple.preferences.sharing")
end tell
tell application "System Events"
tell process "System Preferences"
try
click checkbox of row 3 of table 1 of scroll area of group 1 of window "Sharing"
delay 2
if checkbox of row 3 of table 1 of scroll area of group 1 of window "Sharing" is equal to 1 then
set response to "File Sharing turned on"
else
set response to "File Sharing turned off"
end if
tell application "System Preferences" to quit
activate (display dialog "Flipped")
on error
activate
display dialog "something went wrong in automation but you are in the right menu..."
return false
end try
end tell
end tell

Most of the time, the problem is that the window is not yet fully shown, so you should ask the UI element whether it is already available:
== I´ve edited the script below to reflect changes based on the comments.
tell application "System Preferences"
activate
reveal (pane id "com.apple.preferences.sharing")
end tell
tell application "System Events" to tell table 1 of scroll area of group 1 of window 1 of process "System Preferences"
tell (1st row whose value of static text 1 is "File Sharing")
set sharingStatus to value of checkbox 1 as boolean
if sharingStatus is true then
click checkbox 1
my notify("File Sharing is now turned off")
else
click checkbox 1
my notify("File Sharing is now turned on")
end if
end tell
end tell
on notify(notification)
display dialog notification
end notify

Related

Need accessibility help on mac os sound output using applescript

set outputB to 2 --change this to the actual 'line number' of your second desired output
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 outputA of table 1 of scroll area 1) then
set selected of row outputB of table 1 of scroll area 1 to true
else
set selected of row outputA of table 1 of scroll area 1 to true
end if
end tell
end tell
end tell
--tell application "System Preferences" to quit
I have this apple script from Arthur Hammer. It works perfect if I'm in script editor. When I export it as an application and try to open it from my desktop it only gives me the error "System Events got an error: Output 1 is not allowed assistive access. (-25211)".
I verified that sys pref > security & privacy > privacy > accessibility > Output 1 and script editor are enabled. I've restarted my computer as well but I still can only get that error.
Any ideas how to fix it?

How to click the checkbox of a System Preferences UI element by name

I've got a piece of Applescript that is currently functional and clicks a checkbox in System Prefs > Security & Privacy > Contacts. However, right now it only works because I am explicitly stating the row of the app that I'm targeting (in this case, row 2). This works fine for now, but if in the future I end up with a different app order in that pane, it will break. Is there a way to loop through all the items of a given window and say "if UI element is Alfred 4.app, then click the checkbox"? I'd like to harden the code so that it will work regardless of which order the apps are listed in this pane.
set appName to "Alfred 4.app"
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.security.Privacy_Contacts"
delay 1
end tell
tell application "System Events"
click checkbox 1 of UI element appName of row 2 of table 1 of scroll area 1 of group 1 of tab group 1 of window "Security & Privacy" of application process "System Preferences"
end tell
Following script is tested on the Catalina:
set appName to "Alfred 4.app"
tell application "System Preferences"
activate
reveal anchor "Privacy_Contacts" of pane id "com.apple.preference.security"
end tell
delay 1
tell application "System Events" to tell process "System Preferences"
repeat with aRow in (rows of table 1 of scroll area 1 of group 1 of tab group 1 of window "Security & Privacy")
if name of UI element 1 of aRow is appName then
click checkbox 1 of UI element 1 of aRow
exit repeat
end if
end repeat
end tell

Applescript - Idein

I'm newbie to Appplescript. I need to automate certain actions on my computer related with my Bluetooth keyboards.
I want to be able to click on the remove or connect button of a keyboard in the following dialog window of the System Preferences Panel.
Dialog window
My code until this moment is as follows:
try
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Set Up Bluetooth Keyboard…" of window "Keyboard"
end tell
end tell
tell application "System Events"
tell group 1 of window 1 of application process "System Preferences"
click button "remove" of "Home Keyboard"
end tell
end tell
end try
My problem is related with the remove button since is unidentified cell of an unidentified table. With unidentified, I mean without description. Maybe there is an easy solution, but I'm not able to find it. Furthermore, It could happen that more than one keyboard exists, so I need to identify the cell from the Keyboard name.
Do you know any hint related with this issue?
Thanks in advance
Here is a sample script I used to reconnect a specific mouse via Bluetooth :
tell application "System Events"
tell application "System Preferences"
activate
reveal anchor "MouseTab" of pane id "com.apple.preference.mouse"
end tell
tell application process "System Preferences"
click button "Configuration of Bluetooth mouse…" of window 1 -- see note 1
delay 1
select (first row of table 1 of scroll area 1 of sheet 1 of front window whose value of item 1 of static text of UI element 1 contains "Mouse") -- see note 1
get value of item 1 of static text of UI element 1 of row 2 of table 1 of scroll area 1 of sheet 1 of front window
click button "Done" of sheet 1 of front window -- see note 1
end tell
tell application "System Preferences" to quit
end tell
Note 1 : Be careful about the 3 lines with comment 'see note 1' : the value of the string may be different for your local language. Please adjust these 3 values.
I think for keyboard, concept should be very similar. Because it is using GUI scripting, if Apple changes the layout of Bluetooth screen preferences, it must be adjusted. This script works from Yosemite to ElCaptain.I can't test it for next systems.

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 issue

I am testing applescripts that I will use later in my OSX app.
I'm getting a 6 sec delay after the click button command below.
After some research it seems that this is a known issue.
What I find interesting is, if i use the commercial app QuicKeys to perform the same
button click there is no delay, so I assume they found a work around.
Anybody have any ideas?
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
click button "Track List pop-up" of window 1
-- 6 seconds delay before next command is sent
key code 36 -- return key stroke
end tell
end tell
Was having the same problem and resolved it by enclosing the click causing delay in the ignoring application responses block. Here is a quick summary:
OLD CODE (Causes 6 sec delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
NEW CODE (No delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
ignoring application responses
click bt
end ignoring
end tell
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
Please check detailed answer in the thread listed below.
Speed up AppleScript UI scripting?
Hope this helps.
It seems click or axpress causes a big delay.
Instead - get position and use a third party shell script to do the clicking. Much Much faster.
using clicclik : https://www.bluem.net/en/mac/cliclick/
put in user library/application support/Click
set clickCommandPath to ((path to application support from user domain) as string) & "Click:cliclick"
set clickCommandPosix to POSIX path of clickCommandPath
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
tell button "Track List pop-up" of window 1
set {xPosition, yPosition} to position
set x to xPosition
set y to yPosition
end tell
do shell script quoted form of clickCommandPosix & " c:" & xPosition & "," & yPosition
key code 36 -- return key stroke
end tell
end tell

Resources