Dictation settings with AppleScript - macos

I'm trying to create an an AppleScript to turn dictation on or off (not just make it start listening). My goal is to be able to disable dictation when I am on battery power. I am new to AppleScript and don't know how to do this. I need this to work on macOS Sierra.

I found my answer!
Here is my code if somebody wants it:
set quitting to 0
delay 1
repeat until quitting is 1
set PowerSource to (do shell script "pmset -g ps")
repeat until PowerSource does not contain "AC Power" or quitting is 1
set PowerSource to (do shell script "pmset -g ps")
end repeat
--START
tell application "System Events"
set ProcessList to name of every process
if "System Prefrences" is in ProcessList then
set ThePID to unix id of process "System Prefrences"
do shell script "kill -KILL " & ThePID
end if
end tell
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.keyboard"
end tell
tell application "System Events"
--Part 1:
tell process "System Preferences"
--Part 2:
delay 1
--Part 3:
tell application "System Events" to tell radio button "Dictation" of tab group 1 of window "Keyboard" of application process "System Preferences" of application "System Events" to if exists then click
tell application "System Events" to tell radio button "Off" of radio group 1 of tab group 1 of window "Keyboard" of application process "System Preferences" of application "System Events" to if exists then click
end tell
end tell
tell application "System Preferences" to quit
display notification "Hey siri is now off."
--END
repeat until PowerSource contains "AC Power" or quitting is 1
set PowerSource to (do shell script "pmset -g ps")
end repeat
--START
tell application "System Events"
set ProcessList to name of every process
if "System Prefrences" is in ProcessList then
set ThePID to unix id of process "System Prefrences"
do shell script "kill -KILL " & ThePID
end if
end tell
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.keyboard"
end tell
tell application "System Events"
--Part 1:
tell process "System Preferences"
--Part 2:
delay 1
--Part 3:
tell application "System Events" to tell radio button "Dictation" of tab group 1 of window "Keyboard" of application process "System Preferences" of application "System Events" to if exists then click
tell application "System Events" to tell radio button "On" of radio group 1 of tab group 1 of window "Keyboard" of application process "System Preferences" of application "System Events" to if exists then click
end tell
end tell
tell application "System Preferences" to quit
display notification "Hey siri is now on."
--END
end repeat
on quit
set quitting to 1
continue quit
end quit
Just open it in script editor and compile it into an application. Then launch the application. Hope this helps!

Related

Getting several apps in fullscreen in a row via Applescript

So I am writing a script that would set up my working environment by opening and then getting several apps in fullscreen via a single command line. I am having trouble with the "getting the apps in fullscreen" part, and the script execution is very confusing to me.
My hardware is a MacBook Pro (15-inch, 2017) running on macOS Catalina 10.15.7.
Here is the part of the script that behaves erraticaly when executed:
script1.scpt:
tell application "Sublime Text"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
When executed while the app (here, Sublime Text) is not in fullscreen via the command osascript script1.scpt, the app is activated and goes fullscreen, but key code 123 using {control down} does not work, without producing any error message or sound.
When executed while the app is already in fullscreen via the command osascript script1.scpt, the app is activated, stays in fullscreen as intended but I get this error message:
execution error: System Events got an error: Can't get window 1 of process 1 whose frontmost = true. Invalid index. (-1719)
Also, key code 123 using {control down} does not work either.
When executed via a shell script calling script1.scpt while the app is not in fullscreen, the app behaves exactly as in case 1.
When executed via a shell script calling script1.scpt while the app is already in fullscreen, the app behaves exactly as it should and everything seems fine. But if I lengthen the script to get 2 apps in fullscreen in a row as in script2.scpt (see below), the 2nd app does stricly nothing, and I get the error message of case 2 again.
script2.scpt:
tell application "Sublime Text"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
tell application "Skim"
activate
tell application "System Events"
tell front window of (first process whose frontmost is true)
set value of attribute "AXFullScreen" to true
end tell
key code 123 using {control down}
end tell
end tell
I have been spending hours trying to understand what is going on without success, so any help or clue would be very welcome.
The only way I found to get things working is to separate the code in separate steps with 0.5sec pauses in-between them, like in the following shell script code block. But I would like to find a faster and "cleaner" solution to my problem.
osascript -e "tell application \"Sublime Text\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Skim\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Finder\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell" ;
sleep 0.5 ;
osascript -e "tell application \"Terminal\" to activate" ;
sleep 0.5 ;
osascript -e "tell application \"System Events\"" -e "tell front window of (first process whose frontmost is true)" -e "set value of attribute \"AXFullScreen\" to true" -e "end tell" -e "end tell"'
EDIT: For the moment being, I managed to get my script doing what I wanted it to do pretty reliably. Not completely satisfied though, because I still have to rely on fixed delays and this makes the execution a bit slow. This is what the code looks like:
in .bash_profile:
cd ~/<path>/ ;
osascript workspace_on.scpt
workspace_on.scpt:
tell app "System Events" to tell process "Terminal" to set the value of attribute "AXFullScreen" of window 1 to true
delay 0.6
tell app "Sublime Text" to activate
delay 0.6
tell app "System Events" to tell process "Sublime Text" to set the value of attribute "AXFullScreen" of window 1 to true
delay 0.6
do shell script "open -a skim ~/<path>/main.pdf"
delay 1
tell app "Skim" to set interaction mode of document 1 to full screen mode
delay 0.6
do shell script "open ~/<path>"
delay 1
tell app "System Events" to tell process "Finder" to set the value of attribute "AXFullScreen" of window "latex-template" to true
Just to copy-and-paste my most recent comment from above:
“Like you, I've spent a bit too much time figuring out this problem. It's quite a frustrating situation because I had a few light bulb moments of inspiration that, in theory, ought to have worked, and worked well, but ultimately didn't work at all. ¯_(ツ)_/¯ But in the process, I've at least written out a naive implementation that incorporates conditional checks and operates on each app one-by-one. It's not what I had aimed for in my head, but it works. See below.”
set Apps to {"TextEdit", "ManOpen", "Usenapp", "QuickTime Player"}
tell application id "com.apple.SystemEvents"
set dock to list 1 in the process named "Dock"
repeat with A in the Apps
tell my application named A to activate
set _P to (a reference to the process named A)
set _W to (a reference to _P's front window)
set _B to (a reference to the value of _W's ¬
attribute "AXFullScreenButton")
set _M to (a reference to (_P's menu bar 1's ¬
menu bar items's menu 1's menu items ¬
whose name = "Enter Full Screen"))
tell (a reference to (the dock's ¬
first UI element whose ¬
name = A)) to if ¬
exists then click
tell _W to repeat 60 times -- 60 x 0.5 = 30 seconds
delay 0.5
if it exists then
click _B
if (the value of attribute ¬
"AXFullScreen") ¬
then exit repeat
end if
end repeat
end repeat
end tell
System information:
AppleScript version: 2.8 system version: 12.6 ("Monterey")

How to open multiple panes on iTerm?

I have created aliases of applescripts to open multiple panes on iTerm. However, ever since the latest update the scripts stopped working. I keep getting this error:
syntax error: Expected end of line but found identifier. (-2741)
Here's the script:
newPaneDown() {
osascript -e "
tell application \"iTerm\"
make new terminal
tell the current terminal
activate current session
tell the last session
tell i term application \"System Events\" to key code 2 using {shift down, command down}
end tell
end tell
end tell"
}
newPaneLeft() {
osascript -e "
tell application \"iTerm\"
make new terminal
tell the current terminal
activate current session
tell the last session
tell i term application \"System Events\" to key code 2 using command down
end tell
end tell
end tell"
}
newPanes4x4() {
/usr/bin/env osascript <<-EOF
tell application "iTerm"
activate
launch session "Panes"
tell i term application "System Events" to keystroke "d" using command down
tell i term application "System Events" to keystroke "D" using command down
tell i term application "System Events" to keystroke "[" using command down
tell i term application "System Events" to keystroke "[" using command down
tell i term application "System Events" to keystroke "D" using command down
end tell
EOF
}
alias p2='newPaneLeft'
alias p3='newPaneDown && newPaneLeft'
alias p4='newPanes4x4'
Applescript is not backwards compatible since iTerm2 Version 3.
The new Applescript syntax is described here.
You should replace:
make new terminal
with
create window with default profile

AppleScript for QuickTime recording: "AppleEvent handler failed"

I am using this AppleScript, that intends to activate QuickTime on OS X and automatically start recording audio.
When running the debug in Automator, the script seems to suddenly stop at the line "set lastLength to duration of x", and throws the error:
"AppleEvent handler failed".
What may be wrong with the script?
on run {input, parameters}
tell application "QuickTime Player"
activate
set x to new audio recording
start x
delay 1
set lastLength to duration of x
delay 1
set newLength to duration of x
try
repeat while lastLength is not equal to newLength
delay 1
set lastLength to newLength
set newLength to duration of x
end repeat
end try -- display alert name of document 1
set v to file of front document
set audioNotePath to "/Users/me/Dropbox/Inbox/- Voice Memos"
set thePath to POSIX path of audioNotePath
set dateVariable to do shell script "date '+%m.%d.%y'"
set timeVariable to do shell script "date +'%l.%M %p'"
tell x to activate
delay 1
tell application "System Events"
keystroke "w" using {command down}
delay 1
keystroke thePath
delay 1
keystroke return
delay 1
keystroke "AudioNote "
keystroke dateVariable
keystroke " "
keystroke timeVariable
delay 1
click pop up button "Format:" of group 1 of sheet 1 of window "Untitled" of application process "QuickTime Player" of application "System Events"
delay 1
key code 125
delay 1
keystroke return
delay 1
keystroke return
delay 1
open thePath
end tell
return input
end tell
end run
If you save the following as a stay open application in Script Editor, when you run the application, it will create a new audio recording and keep checking to see if you have manually stopped the recording. Once you do stop the recording, it will export the file, open it, and quit. If you really want it as an Automator action, just have the Automator action open the script application.
property savePath : "Desktop:" --update e.g.,: "Dropbox:Inbox:- Voice Memos:"
property defaultName : "AudioNote"
my start_recording()
on start_recording()
tell application "QuickTime Player"
activate
tell (new audio recording) to start
end tell
end start_recording
on export_recording()
set homeFolder to (path to home folder) as string
set dateTimeStamp to do shell script "date '+%m%d%y-%H%M%S'"
set audioNotePath to (homeFolder & savePath & defaultName & " " & dateTimeStamp & ".m4a")
tell application "QuickTime Player"
tell document 1
export in file audioNotePath using settings preset "Audio Only"
close saving no
end tell
end tell
return audioNotePath
end export_recording
on open_recording(audioNotePath)
tell application "QuickTime Player" --you could change this if you wanted to open the file in another app
repeat --keep trying until the file is exported and ready to be opened
try
delay 1
open file audioNotePath
exit repeat
end try
end repeat
end tell
end open_recording
on idle
try
tell application "QuickTime Player" to set documentName to name of document 1
if (documentName ≠ "Audio Recording") then
set audioNotePath to my export_recording()
my open_recording(audioNotePath)
quit
end if
end try
return 1
end idle

Go to the Running Safari And Press Enter Using AppleScript

I'm very new to AppleScript and I'm trying to do this. I have an Safari process running on my machine. I wish I could go to the running safari and stimulate an enter key event.
Is it possible to do so?
The code I'm trying is:
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
set safRunning to is_running("Safari")
if safRunning then
run script "tell application \"Safari\"
key code 36
end tell"
return "Running"
else
return "Not running"
end if
Inside the run script the key code 36 (for stimulating enter) is giving me error:
error "Expected end of line but found identifier." number -2741
Any guesses where I'm making mistake?
Try:
if application "Safari" is running then
activate application "Safari"
tell application "System Events" to keystroke return
else
return "Not running"
end if

Why does this not work when I make it an app?

Why does this not work when I make it an app? Im trying to make this work but all it does is keystroke the commands in finder not terminal. It even works in the applescript editor just not as an app. Please help!
on run
tell application "Terminal" to quit
delay 5
tell application "Terminal" to activate
delay 3
tell application "System Events" to keystroke "n" using {command down}
delay 1
tell application "System Events" to keystroke "top" & return
delay 1
tell application "System Events" to keystroke "n" using {command down}
delay 1
tell application "System Events" to keystroke "open -a Bartender" & return
delay 1
tell application "System Events" to keystroke "open -a MobileMouseServer" & return
delay 1
tell application "System Events" to keystroke "open -a gfxCardStatus" & return
delay 1
tell application "System Events" to keystroke "open -a Caffeine" & return
delay 1
tell application "System Events" to keystroke "open -a Alfred" & return
delay 1
tell application "System Events" to keystroke "open -a smcFanControl" & return
delay 1
tell application "System Events" to keystroke "w" using {command down}
tell application "Finder" to display dialog "System startup successful." with title "T.R.A.V.I.S." with icon file "Macintosh HD:SCRIPTS:ICNS:travis.icns" giving up after 5
end run
Thanks
Consider this structure:
tell application "Terminal"
if not (exists window 1) then reopen
-- targets an existing window
do script "top" in window 1
-- open a new window
do script "open -a Bartender"
end tell

Resources