Toggle messages preview in notification center with applescript/quicksilver - macos

Haven't ever scripted with apple script before was wondering if it was possible to create an applescript that could toggle the settings for messages in the notification that displays the message preview. I was then gonna use quicksilver to call that script, is this possible with applescript and would quicksilver be able to make the appropriate calls to initiate the script? I'm running mavericks OS.

This toggles the "Show message preview" checkbox:
tell application "System Preferences"
reveal pane id "com.apple.preference.notifications"
end tell
tell application "System Events" to tell window 1 of process "System Preferences"
repeat with r in rows of table 1 of scroll area 1
if name of UI element 1 of r is "Messages" then
set selected of r to true
exit repeat
end if
end repeat
click checkbox "Show message preview" of group 1
end tell

Related

Change MacBook screen scaling via AppleScript

I am trying to change my MacBook Pro 14"s scale setting via AppleScript.
The setting should toggle two resolution settings.
I found the following script here: https://stackoverflow.com/a/62664159/15705553
on run {input, parameters}
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
set lowResolutionSettingIndex to 4
set highResolutionSettingIndex to 5
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
click radio button "Display" of tab group 1
click radio button "Scaled" of tab group 1
tell radio group 1 of group 1 of tab group 1
set isHighResolutionSet to get value of radio button highResolutionSettingIndex
end tell
if isHighResolutionSet then
-- Toggle native resolution
click radio button lowResolutionSettingIndex of radio group 1 of group 1 of tab group 1
else
-- Toggle Default setting - "Retina optimized"
click radio button highResolutionSettingIndex of radio group 1 of group 1 of tab group 1
end if
end tell
quit application "System Preferences"
return input
end run
I changed "Built-in Retina display" to "Built-in Liquid Retina XDR Display" as shown in my System Preferences, but two errors occur:
If I execute this script through Automator, I get the following error:
Syntax Error: System Events got an error: Can’t get window "Built-in Liquid Retina XDR Display" of process "System Preferences".
If I execute it through shortcuts.app, I get the following error, even though I granted access to accessibility features for Shortcuts in System Preferences
System Events got an error: Shortcuts is not allowed assistive access.
Here's how I select the first monitor in the list (Macbook Pro Built-in Retina Display) to gain access to the settings such as screen scaling:
tell application "System Preferences"
activate
reveal anchor "universalControlTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
tell window "Displays"
select row 1 of outline 1 of scroll area 1 of sheet 1
end tell
end tell
end tell
And to select the second monitor in the list if you want to gain access to those settings:
tell application "System Preferences"
activate
reveal anchor "universalControlTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
tell window "Displays"
select row 2 of outline 1 of scroll area 1 of sheet 1
end tell
end tell
end tell
Cheers.

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.

How to to select specific checkboxes in System Preferences?

I am trying to select the third checkbox of the Keyboard menu item and tab to change the basic fn key function with one run of the script. The rest of the code appears to work fine, but I just recently started trying to code at all so I have no idea.
Here is my current code:
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.keyboard"
delay 1
tell application "System Events"
tell (click checkbox 3 of tab group 1)
delay 2
end tell
end tell
end tell
tell application "System Preferences" to quit
And here is the error message:
error "System Events got an error: Can’t get tab group 1." number -1728 from tab group 1
It looks like I'm just not defining it correctly, but I can't find out how to. Any help is appreciated!
Also, this is not needed but would it be possible to run the script without visibly opening the System Preferences application?
The following example AppleScript code was tested under macOS Catalina and clicks the Turn keyboard backlight off after checkbox at: System Preferences > Keyboard > Keyboard
As coded, it does the following:
Checks to see if System Preferences is running and if it is, it closes it so as to not have to see the UI flashing thru the different panes.
If System Preferences is not running it opens to the target anchor/pane without showing the UI.
Clicks the target checkbox.
Closes System Preferences
Example AppleScript 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
tell application "System Preferences" to ¬
reveal anchor "keyboardTab" of pane id ¬
"com.apple.preference.keyboard"
tell application "System Events"
tell front window of application process "System Preferences"
repeat until (exists checkbox 3 of tab group 1)
delay 0.01
end repeat
click checkbox 3 of tab group 1
delay 0.1
end tell
end tell
tell application "System Preferences" to quit
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.

Apple Script Error: Can't continue click

I'm trying to open a messaging application (it does not have an Apple Script Dictionary (command + shift + o)), click on text, and type into the text box, and hit send.
Pop up: Script Error - Telegram got an error: Can't continue click after the application becomes active.
Result Tab: error "Telegram got an error: Can’t continue click." number -1708
P.S., The messaging application is Telegram.
Apple Script:
tell application "Telegram"
activate
delay 1
click on text "chat name"
keystroke "some text"
//assuming this works because text box is the first responder when the chat opens.
click on text "Send"
end tell
If an application lacks an AppleScript dictionary, any command except the standard commands launch, activate, open, reopen and quit will throw an error.
The solution is GUI scripting: The built-in application System Events is the bridge to send mouse clicks and keyboard events to the target application.
I don't know the application Telegram at all, so this code might fail, but it might also be a starting point
activate application "Telegram"
tell application "System Events"
tell process "Telegram"
tell window 1
keystroke "some text"
click button "Send"
end tell
end tell
end tell
You have two choices for a 3rd party app that lacks an AppleScript dictionary.
Option 1:
Use System Events as described above to perform an action on an element, e.g. click a button, keystroke text into a field, etc. The trick is to identify the element in syntax that is recognized by Applescript. Besides UIElementInspector mentioned above, which can be confusing and occasionally wrong/incomplete, you can also run the following commands in a separate Applescript Editor. For example, to get all UI elements for the active window (window 1) in Telegram:
tell application "System Events" to tell application process "Telegram" to tell window 1
UI elements
end tell
To get all UI elements for the main menu bar in Telegram:
tell application "System Events" to tell application process "Telegram" to tell menu bar 1
UI elements
end tell
In each case the Result pane will display a comma delimited list of all available UI elements in that window or menu bar. Moreover, the syntax as listed is guaranteed to be recognizable by Applescript. Just identify the correct element and tell System Events to tell it what to do.
For example if you want to click the Menu item "Format" In TextEdit first run the following:
tell application "System Events" to tell application process "TextEdit" to tell menu bar 1
UI elements
end tell
Among the results in the Result pane will be the following:
menu bar item "Format" of menu bar 1 of application process "TextEdit" of application "System Events"
Convert that to Applescript, run the script and it will click the "Format" Menu:
tell application "TextEdit" to activate --you need TexEdit up and running to click its menu bar
tell application "System Events" to click menu bar item "Format" of menu bar 1 of application process "TextEdit"
For submenus, etc. you just iterate the process asking for UI elements for the submenu. GUI scripting is iterative and empirical.
Option 2:
Download the free Terminal/Command Line app cliclick which allows you to click on any point in the screen. The screen coordinates you want to click can be manually identified with your cursor by holding down command + shift + 4.

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