Using AppleScript to click the plus (+) button in a preferences window - applescript

Using AppleScript I would like to automate the creation of a new user accounts on multiple machines. I'm trying to use UI Scripting and cannot seem to be able to do a click command on the "Add Account" (+) button in the iChat Accounts Preference window. I've tried to use the Accessibility Inspector.app to infer the name of the button but I still cannot seem to access it via AppleScript.
According to Accessibility Inspector.app the button has the following attributes:
AXRole: AXButton
AXRoleDescription: button
AXHelp: <nil>
AXEnabled: YES
AXFocused (W): NO
AXParent: <AXGroup>
AXWindow: <AXWindow:AXStandardWindow>
AXTopLevelUIElement: <AXWindow:AXStandardWindow>
AXPosition: x=225.00 y=462.00
AXSize: w=23.00 h=22.00
AXTitle: <empty string>
AXIdentifier: _NS:27
AXDescription: Add Account
Here is a sample starter AppleScript that will get you to the Accounts Preference window:
tell application "iChat"
activate
tell application "System Events"
tell process "iChat"
tell menu bar 1
click menu item "Preferences…" of menu "iChat"
end tell
click button "Accounts" of tool bar 1 of window 1
click button "Add Account" of window "Accounts"
end tell
end tell
end tell
Please note that line 9 is what I'm trying to get working. Below you will find a link to a screenshot of the iChat Accounts Preference window with an arrow pointing to the button that I'm trying to access. Also note that I know that I can just use "keystroke tab" to get to the (+) button, but this will not always work as the number of times you must press tab changes depending on the type of accounts that you already created in iChat.
iChat Account Preferences Window

Not every Ui object has a title, so in those cases you can just use the item number - note that you need to include the entire hierarchy shown in the Inspector window.
tell application "iChat"
activate
tell application "System Events"
tell process "iChat"
tell menu bar 1 to click menu item "Preferences…" of menu "iChat"
delay 0.5
tell window 1
click button "Accounts" of tool bar 1
delay 0.5
click button 1 of group 1 of group 1
end tell
end tell
end tell
end tell

Related

How to programatically switch between touchbar layouts on MacOS?

System
M1 MacBook Pro
MacOS Big Sur
Problem
I like the default Mac touchbar layout for everyday use, but I prefer the F1-F12 keys at my fingertips when programming. I also don't like holding down the fn key. That's why I wrote two AppleScripts to switch the layouts (included below).
The scripts work, but they are buggy. This is because they rely on opening the System Preferences app and navigating through the menus. I made a couple "apps" with Automator
that simply run the scripts and then assigned them to keyboard shortcuts.
This is an ok solution, but I'd like to do something more elegant. Ideally, my script should run behind the scenes and instantly change the touchbar layout instead of opening System Preferences, selecting items from drop-down-lists, and then finally closing System Preferences.
I messed around with the shell for quite awhile with no success before resorting to using Automator. Any suggestions from those who are more savvy with sort of thing?
Code
This one makes the F1-F12 keys the default touchbar layout:
tell application "System Preferences"
set the current pane to pane id "com.apple.preference.keyboard"
delay 0.25
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 2 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 2 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 4 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "Show App Controls" of menu 1 of pop up button 4 of tab group 1 of window "Keyboard"
end tell
end tell
quit application "System Preferences"
And this one does the reverse (makes app controls the default touchbar layout):
tell application "System Preferences"
set the current pane to pane id "com.apple.preference.keyboard"
delay 0.25
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 2 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "App Controls" of menu 1 of pop up button 2 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 4 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "Show F1, F2, etc. Keys" of menu 1 of pop up button 4 of tab group 1 of window "Keyboard"
end tell
end tell
quit application "System Preferences"
On macOS Catalina when toggling the target setting it changes the value of the PresentationModeGlobal key in ~/Library/Preferences/com.apple.touchbar.agent.plist from appWithControlStrip to functionKeys or vise versa for those two choices. However, toggling it programmatically using the defaults command while it changes it in the UI it does not change it on the Touch Bar without also restarting the ControlStrip process.
The following example shell script code is what I use with a single keyboard shortcut to toggle between between Show App Controls and F1, F2, etc. Keys as that is what they are set to respectively in System Preferences > Keyboard > Keyboard on my system.
Example shell script code:
#!/bin/zsh
pmg="$(defaults read com.apple.touchbar.agent 'PresentationModeGlobal')"
if [[ $pmg == "appWithControlStrip" ]]; then
defaults write com.apple.touchbar.agent 'PresentationModeGlobal' 'functionKeys'
killall "ControlStrip"
else
defaults write com.apple.touchbar.agent 'PresentationModeGlobal' 'appWithControlStrip'
killall 'ControlStrip'
fi
Notes:
Other versions of macOS may require additional settings to be changed and or additional processes to be restarted, e.g., pkill 'Touch Bar agent' if applicable.
In Terminal you can use the read verb of the defaults command to examine changes to com.apple.touchbar.agent.plist as you make them in the UI to see if the value for additional keys needs to be changed too.
Side Note
As to your AppleScript code, here is how I'd do it based on your code as a single script to toggle between the two choices.
You didn't say what version of macOS are you running and since I do not have a pop up button 4 I cannot test the example AppleScript code shown below, however, this should eliminate having two separate scripts and it just toggles between the two with a single keyboard shortcut.
Example AppleScript code:
tell application "System Preferences"
set the current pane to pane id "com.apple.preference.keyboard"
delay 0.25
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell tab group 1 of window 1 of process "System Preferences"
if value of pop up button 2 is "Show App Controls" then
click pop up button 2
click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 2
click pop up button 4
click menu item "Show App Controls" of menu 1 of pop up button 4
else
click pop up button 2
click menu item "Show App Controls" of menu 1 of pop up button 2
click pop up button 4
click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 4
end if
end tell
end tell
quit application "System Preferences"
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

OSX tick menu bar checkbox

For my application MuteSpotifyAds I wan't to add a feature that automatically enables the Private Session mode.
To do so, I wan't to toggle the menu bar item Private Session in the menu Spotify of the OSX menu bar. I use the following applescript, which works:
Source: https://stackoverflow.com/a/16497564/6286431
tell application "System Events" to tell process "Spotify"
tell menu bar item 2 of menu bar 0
click
click menu item "Private Session" of menu 1
end tell
end tell
The problem is that I have to check if the checkbox is already ticked, because else I would disable the Private Session.
Is there a way to check if the menu checkbox is already ticked?
I've already tried this, but seems to only work for checkboxes in actual windows.
If you have a solution that does not use applescript, that's also good!
The information if the menu item is checked is in the attribute "AXMenuItemMarkChar" of the menu item
tell application "System Events" to tell process "Spotify"
tell menu bar item 2 of menu bar 1 -- AppleScript indexes are 1-based
tell menu item "Private Session" of menu 1
set isChecked to value of attribute "AXMenuItemMarkChar" is "✓"
if not isChecked then click it
end tell
end tell
end tell

AppleScript: Menu bar item click (but no AXDescription)

I'd like to AppleScript to click a menu bar icon (not a system process) and then click on one of the menu items. I'm using OS X 10.10.
I've read that I can use the Accessibility Inspector to find the AXDescription of the item, however, I can't find it anywhere in the inspector (in fact none of the items I inspected seemed to have this property).
I also tried the method described here but get an error on line 10:
ignoring application responses
tell application "System Events" to tell process "Webcam Settings"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Webcam Settings"
tell menu bar item 1 of menu bar 2
click menu item "Show Webcam Settings Panel" of menu 1
end tell
end tell
System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of process "Webcam Settings". Invalid index.
There are some menu bar apps which you can click and show menu with applescript regardless of if they appear in dock.
But I am afraid you can’t access the menubar of “Webcam Settings” with applescript.
Because there isn’t key named “isAccessbiliyFocused” in the attributes.
Menu bar apps you can show their menu with applescript have that key and its value is “Yes”.
I am not quite sure about my answer but as far as I investigate this issue, my answer is probably right.

Applescript to click on a specific icon in the Mac Menu Bar

Sometimes I use PdaNet to tether using my iPhone. The desktop client for OSX is not as rich as the one for windows. One of the chief differences is, that the OSX does not allow to automatically connect to iPhone as soon as the latter is plugged in.
Would you know of a way using Applescript to click on the PdaNet icon on the Menu Bar and then select and click the 'Connect' option on it ?
Here is what the 'PdaNetMac' application's menu bar icon looks like:
I have looked at the following questions but am an applescript newbie and am not sure how to search for PdaNet's icon on the menu bar:
Click menu item on Mac OSX Lion using AppleScript
Applescript: on clicking Menu Bar item via gui script
Accessing dock icon right-click menu items with AppleScript
I have confirmed that 'Enable Access for assistive devices' is enabled.
Based on the second question above, Here is my current attempt at doing this:
ignoring application responses
tell application "System Events" to tell process "PdaNet"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "PdaNet"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Interestingly, the above script works for me fine when I change PdaNet to Flux.
Thanks!!
You were very close !!
I just downloaded the PdaNet application to test this, and the only edit I had to make to your script was change PdaNet to 'PdaNetMac` ( I was thinking that this is the Process Name and so used the process name displayed in Activity Monitor).
So this works for me:
ignoring application responses
tell application "System Events" to tell process "PdaNetMac"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "PdaNetMac"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Hope this works for you too !!
(Very useful script, btw. Cheers !)

AppleScript to set up VPN on Mac 10.8 is loosing focus and types text in wrong window, what is wrong?

I wanted to share a very useful AppleScript with other Mac admins but it is losing focus. When it's run in AppleScript editor it runs to a certain stage and then instead of copying text to required windows it replaces text in AppleScript editor. Can anyone suggest why?
set vpnname to "VPN (Primary)"
set vpnserver to "vpn.website.com"
set vpnsecret to "abcdefghij"
set vpnusername to system attribute "USER"
set groupname to "GROUP"
activate application "System Preferences"
tell application "System Events"
-- Checks whether Universal Access is enabled - this is required for all scripts to work
if not (UI elements enabled) then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "This script requires access for assistive devices be enabled." & return & return & "To continue, click the OK button and enter an administrative password in the security dialog." with icon note
end tell
set UI elements enabled to true
if UI elements enabled is false then return "User Cancelled"
delay 1
end if
tell process "System Preferences"
click button "Network" of scroll area 1 of window "System Preferences"
--Creating VPN (CU Primary) interface
tell window "Network"
click button "Add Service"
delay 1
end tell
tell sheet 1 of window "Network"
click pop up button 1
click menu item "VPN" of menu 1 of pop up button 1
delay 1
click pop up button 2
click menu item "Cisco IPSec" of menu 1 of pop up button 2
set focused of text field 1 to true
keystroke "a" using command down
keystroke vpnname
click button "Create"
delay 1
end tell
--Entering server / account details
tell group 1 of window "Network"
click checkbox "Show VPN status in menu bar"
set focused of text field 3 to true
keystroke vpnserver
set focused of text field 1 to true
keystroke vpnusername
click button 2 --pressing "Advanced…" button
delay 1
end tell
--Entering "Advanced…" details
tell sheet 1 of window "Network"
activate
set focused of text field 2 to true
keystroke vpnsecret
set focused of text field 1 to true
keystroke groupname
click button "OK"
delay 1
end tell
-- Apply all changes
tell window "Network"
click button "Apply"
delay 1
end tell
tell application "System Preferences"
quit saving yes
end tell
end tell
end tell
I'm not sure if it will work but if any of the key strokes are going into applescript just run the
tell application "application name here"
activate
end tell
before the keystrokes that are being put into apple script then it will focus on the window you have told it to
Sorry for the lack of code snippet I am new to this forum and I'm not sure how to use it properly

Resources