Dismiss MacOS Big Sur notifications with keyboard - macos

I have long used an Applescript triggered from a keyboard shortcut to dismiss notifications on MacOS. It worked by simulating a click of the "close" button of every open Notification Center window.
Since upgrading to Big Sur (currently 11.0.1), notifications no longer have a close button. Instead, when you hover over them with the mouse, an "X" button will appear. For a person who has difficulty clicking precisely (I have a slight hand tremor), using the mouse/trackpad is difficult.
Anyone have suggestions on how to restore that functionality so I can just keep my hands on the keyboard?

Found this https://itectec.com/askdifferent/macos-dismiss-macos-big-sur-notifications-with-keyboard/.
I tested it worked. The minor error is process name is 'NotificationCenter' without space. Original code may cause process not found error.
Credit belongs to the original author from the link.
activate application "NotificationCenter"
tell application "System Events"
tell process "NotificationCenter"
set theWindow to group 1 of UI element 1 of scroll area 1 of window "Notification Center"
# click theWindow
set theActions to actions of theWindow
repeat with theAction in theActions
if description of theAction is "Close" then
tell theWindow
perform theAction
end tell
exit repeat
end if
end repeat
end tell
end tell

Related

AppleScript for AirPlay Button in the new TV app

Can anybody figure out how to click the AirPlay Display pop up button in the new TV app (found in MacOS Catalina) using AppleScript? I've tried a zillion different ways, and still get the result "missing value."
As far as I know this code should work:
tell application "System Events"
tell application process "TV"
tell window 1
click ((button whose description is "airplay") of pop up button 1)
end tell
end tell
end tell
Here is a picture of the Accessibility Inspector for that pop up button:
UPDATE: Apparently the new TV app in Catalina is not fully scriptable. AppleScript support for AirPlay Displays does not exist (neither directly nor through GUI scripting). Hopefully, Apple will add support in the future...

How to click on a button in an app from the status bar with Apple Script

I have see how to click on menu items but this application does not have those. I was wondering how to click on the button Connect
I tried click at {818, 320} but it did not work
Here is the scrip, i was able to open the app with this
tell application "System Events"
tell UI element "Hotspot Shield"
tell menu bar item 1 of menu bar 2
click
delay 2
get the actions of button "Connect"
end tell
end tell
end tell
FWIW One would not normally use tell UI element "Hotspot Shield" rather use tell application process "Hotspot Shield", however, looking at this through Accessibility Inspector, I do not see a way to click the "Connect" button. Even using a Watch Me Do action in Automator, while it will record the process, it won't play it back successfully.
That said, here is a workaround solution that works for me on macOS Mojave using AppleScript and the third-party command line utility Cliclick.
Looking at the cropped screen shot in your OP and using the approximate center of the Connect button to be at 320 for the y axis and using AppleScript to get the position of the menu bar item for use as the approximate x axis, the following example AppleScript code should work for you too.
tell application "System Events" to ¬
tell application process "Hotspot Shield" to ¬
tell menu bar item 1 of menu bar 2
set xPos to first item of (get its position)
click
end tell
delay 0.5
do shell script "/Applications/cliclick c:" & xPos & ",320"
Note that the value of the delay command may need to be adjusted to ensure the Connect button is visible before the click event takes place using cliclick.
Adjust the path to cliclick as needed. I placed it in /Applications just for testing purposes.
The usual caveats, regarding System Preferences > Security & Privacy > Privacy, apply.
I am not affiliated with the developer of Cliclick, just a satisfied user of the product.
Note: The example AppleScript code is just that and does not contain any 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.

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 click Done when dictating (Mac)

I have a script that opens "Dictation & Speech", activates it (turns Dictation: On) and runs a shortcut to start dictating (System Events keystrokes Command D). All this works fine, but what I want to do is for it to click done (or the return key) after speech is not heard. I tried someone's answer from StackOverflow.
Here is the link: Applescript to automatically close dialog box after input?
But when it's done editing and the return key is pressed, there is no text entered. Maybe the return key is not pressed, and it just quits the dictation?
EDIT: After some research, I found out that the Dictation Window (or tiny Microphone Popup) will close if clicked anywhere except Done. So I am guessing when the Keystroke Return happens, it is on a different window, so I was wondering how would I get the current window/pop-up then click the Done key in it?
The Done button is button 1 of process "DictationIM":
tell application "System Events" to tell process "DictationIM"
click button 1
end tell
I don't understand why you are running a script or why you are not happy with it.
I have Dictation and Speech turned on in System Preferences with the shortcut "Press Left Command Key Twice". (You can set your own custom shortcut.) When I press the command key twice then the dictation service activates. I can terminate the dictation service by pressing the return key and the text of whatever I was dictating is inserted wherever the cursor is. This sounds like exactly what you want, so why do you need the script?
Try to "click" on the button with applescript like this:
tell application "System Events"
set theProcess to first process whose frontmost is true
tell theProcess
click button "Done" of window 1
end tell
end tell
You only have to repeat the command-D keystroke.
thats all. you are able to start dictation with command-D so you are able to exit dictation with command-D

How do you programmatically press a toolbar button with AppleScript?

I hope this isn't too obvious, but I'd like to press one of the toolbar buttons within an application by means of AppleScript.
Background: The button doesn't have any menu item or keyboard shortcut. Thus, I can't activate it by any of those methods; I need to find an AppleScript way of actually 'pressing' the button.
I am going to expand a little on what #hced said.
I agree that click button 3 of tool bar 1 of window 1 works in many cases. However from OS to OS thic can be a moving target. My example is iTunes. In iTunes 10.5.8 the airplay button is 17 where in 10.7.5 it is 25. However, in both instances it is called "AirPlay"!
So I suggest using the developer kit, and getting this code from Apple:
http://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/Intro.html
Run the application and whatever you mouse over will get you all the info you need to make this work in your AppleScripts:
click (every button whose description is "AirPlay") of window "iTunes" of application process "iTunes"
In this case there should only be one button with this description, but you can use the tool to discover whether this is the case. If so, limit the scope!
Hope this helps!!
Bo
Ah, figured out that one myself, too. Seems I'm on a self-commenting spree here.
This example worked for me, referencing a specific toolbar button by means of its description attribute:
tell application "System Events"
tell process "OmniFocus"
click (every button whose description is "Contexts") of tool bar 1 of window 1
end tell
end tell
I'll leave it up for any commenters with an opinion about referencing every button whose descripton is "Contexts" (as per my example), to express those down below. But I presume there can't be multiple buttons with the same description, right?
Ok, that was simpler than I thought, once I found out how. (Googling it didn't leave much help at all, but an application called Prefab UI Browser did the trick finding out how this is done).
Example:
tell application "System Events"
tell process "OmniFocus"
click button 3 of tool bar 1 of window 1
end tell
end tell
I've got a follow-up question, though:
How do you reference a toolbar button by name/description? I know for a fact that the button has a description attribute with the value: "Contexts". But again, how do I reference it in the click statement?

Resources