Applescript to click Done when dictating (Mac) - macos

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

Related

Dismiss MacOS Big Sur notifications with keyboard

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

AppleScript, why does first "display alert" work but then cause AppleScript icon to bounce, waiting for me to click it until I get another dialog?

NOTE: I do not have Admin privileges, and cannot use things that require assistance or accessibility or whatever it is!
I'm basically trying to write an execution of a script that sets up my macOS Desktop by opening apps, positioning them etc
Part of that is allowing me to click continue on a dialogue after doing certain things I have to do manually as a result of the limitation (positioning a few apps for example).
I can successfully have the first alert pop up, and when I press OK, it executes the task, but then the 2nd alert is hidden behind AppleScript and will not pop up until I click AppleScript. Anyone know why?
Heres what I have, and please keep in mind I do not have Admin privileges, which means I can't use System Events:
tell application "Script Editor"
set miniaturized of window 1 to true
end tell
display alert "App is opening. Make sure you log-out of Apple Connect!"
delay 3
tell application "Safari"
activate
make new document
set the bounds of the front window to {1077, 23, 1581, 464}
tell window 1
set URL of tab 1 to "http://appwebsite.com"
end tell
end tell
display alert "Set up App, sign into Apple then click OK to open all other Safari windows"
delay 1
tell application "Test Task App" to activate
Place an activate command before the display alert command so Script Editor has focus and the display alert command will not be hidden.
Example:
activate
display alert "Set up App, sign into Apple then click OK to open all other Safari windows"
You do not need it before the first one as Script Editor has focus with it runs the first display alert command, but once you activate Safari, the display alert command is behind Safari. So if you use an activate command before the display alert command, it brings it forward in front of, in this case, Safari.

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.

AppleScript to block Safari pop-up windows

I am trying to automate the blocking of pop-up windows in Safari.
I have tried the following defaults write operation
defaults write com.apple.Safari WebKit2JavaScriptCanOpenWindowsAutomatically -boolean false
But this fails to do it. I have also tried setting it as true but even that didn't help. I am currently trying to find a way to do it using AppleScript.
This is what I have written so far -
CMD_ACTIVATE='tell application "Safari" to activate'
CMD_NEWTAB='tell application "System Events" to keystroke "," using {command down}'
osascript -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB"
This opens up the preferences but then I draw a blank. Anyone with any suggestions on how to proceed? Also I don't really need a solution to this only in AppleScript any other way to do it would also be helpful.
Note: I have been using Mac OS only for a week now, and am not that well versed in the nuances of this OS, so please be a bit descriptive when answering.
Thanks.
Blocking pop-up window is done via Safari / preferences / tab Security where you need to set the correct checkbox.
This preference seems to be stored in your library / preferences file com.safari.plist which contains the flag com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptCanOpenWindowsAutomatically with value Yes when pop-up are blocked.
However, it may not be the only place to be changed and in any cases, it is not officially documented, so that place can be changed by Apple any time. This is not recommended to use it.
Going back to Applescript, because Safari is quite poor in terms of applescript handling events, you are forced to go via GUI scripting. That’s what you’ve started, but keep in mind that if Apple changes the layout of Safari preference window, your script must be reviewed.
When going through GUI scripting (which, again, should only be when no other solution found) you must understand structure of GUI objects. Window contains button, check box, tool bar... in a hierarchy model. For instance the preference window in Safari contains bellow the tool bar, an object "group 1" with itself contains many objects depending of the tool bar current selection. Once you understand that concept, the script below, which does what you're looking for, will be easy to understand with many comments:
tell application "Safari" to activate
tell application "System Events"
keystroke "," using {command down}
delay 0.2 -- leave sometime to open window
tell window 1 of process "Safari"
click button 6 of toolbar 1 -- Security button is number 6
delay 0.2
-- check if check box not yet set and set it.
if (value of checkbox 5 of group 1 of group 1) = 0 then click checkbox 5 of group 1 of group 1
end tell
delay 0.2
click button 1 of window 1 of process "Safari" -- click on red / close button
end tell
I am running on Safari 10.0.3. If your version is different, the preference window may be different. Then the script must be adjusted: the Security tab button may not longer be the number 6 in your version,...

OSX Photos video play/pause Applescript shortcut

There is currently no keyboard shortcut to play videos in the new Mac Photos application.https://discussions.apple.com/thread/6996731?start=0&tstart=0
Rather than be frustrated in manually clicking the "play" button hundreds of times, I would like to run an Applescript to play/pause with a keyboard shortcut. I tried with no success:
tell application "photos"
"play" video
end tell
Any help advice on this would be appreciated.
Thanks
Just to update Paul's response, in Photos 2.0 (3150.4.120), you need to hit TAB twice, then spacebar will start playback. Similarly you need to hit TAB twice once you are done playing it, and then (and only then) spacebar will take you back to the grid view of your thumbnails.
If you do not mind a slightly time-consuming workaround, you can enslave Automator to create a Service that will subsequently respond to a keyboard shortcut. You can find the instructions here.
Since your goal is to use the keyboard, there's a better option for you. After selecting a video with cursor keys or mouse, press space to maximize the video, and then press TAB. Then space will start the video playing, left and right arrows will move frame-by-frame. Hitting TAB again will allow the space bar to return the video to its normal size.
Apparently this changes the focus to the pane with the movie so that space can start it, or perhaps it changes focus to the movie controls? IMHO, whatever it does, it's not very intuitive and badly needs to documented and preferably fixed.
More info at:
https://discussions.apple.com/thread/6996731?start=0&
2020 / Photos 6.0:
You can now toggle playback with Option + Space
or do it manually via "Image" -> "Start Playback".
Autoplay Script
repeat while application "Photos" is running
delay 1
try
tell application "System Events"
tell process "Photos"
click menu item "Start Playback" of menu "Image" of menu bar 1
end tell
end tell
end try
end repeat
Save it as autoplay.scpt and run it with osascript autoplay.scpt.
The script exits when you close the Photos application.

Resources