I am working on a SpeechRecognition project and I am pretty much done. However, there is one thing I cant quite figure out. When I launch my application it listens to my commands, and if I stay quite or don't say the commands then it is giving me this Script Error:
it is timing out. I want it to stay open 24 hour even if I am not saying anything.
This is my code:
tell application "SpeechRecognitionServer"
with timeout of 10000 seconds
set FRIDAY_AI to listen for commands
end timeout
end tell
If you could help me solve this problem I would really appreciate it.
Thank you!
About 70% of everything I do on my computer is controlled by dictation commands and my own custom dictation commands. Very powerful stuff once you get the hang of it. I set you up with a small script with a few examples to have the SpeechRecognitionServer app continuously listen for a set of spoken commands to act upon. I think it's pretty self-explanatory by looking at the code to be able to adjust it to your needs.
In Script Editor, save this following code as a stay open application. Make sure to add your new app in System Preferences to the list of apps allowed to control your computer. Now all you need to do is launch your new app.
on idle
try
tell application "SpeechRecognitionServer"
set theChoice to listen continuously for ¬
{"Open Google", "Close Windows", "Enter Name", "Enter Password", "Close My Commands"} ¬
with identifier "mine" with section title "WeeeHaaa's Commands"
if theChoice is "Open Google" then
tell application "Google Chrome" to activate
else if theChoice is "Close Windows" then
tell application "Finder" to close windows
else if theChoice is "Enter Name" then
set myFullname to "Crazy Eddie"
tell application "System Events"
keystroke myFullname
end tell
else if theChoice is "Enter Password" then
set myPassword to "secretpassword"
tell application "System Events"
keystroke myPassword
end tell
else if theChoice is "Close My Commands" then
quit me
end if
end tell
end try
return 0.5
end idle
on quit
-- stop listening
tell application "SpeechRecognitionServer"
stop listening for identifier "mine"
end tell
continue quit
end quit
Related
I want Applescript that trigger as soon as Mail app opens and if mail app opens then it should wait for 10 seconds and after that it should activate the previous app (like what cmd + tab does).
I have used the following code but in order to achieve that, it should run in the background all the time. I have no clue how to achieve this.
tell application "System Events"
set frontmostApplicationName to name of 1st process whose frontmost is true
end tell
tell application "Mail"
activate
end tell
tell application frontmostApplicationName
activate
end tell
I am trying to make an AppleScript that toggles automatic rearranging of spaces. I am able to get the AppleScript to open system preferences and go into mission control settings, however i am not sure how to check the box which i want to change.
tell application "System Preferences"
activate
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "Mission Control" of menu "View" of menu bar 1
delay 2
tell window "Mission Control"
//additional code goes here
end tell
end tell
end tell
Is there a way to see what the components of the window are so i know if i need to go into a table, or something else, before i am able to access the check boxes that toggle the settings
This is an alternative method using a shell command via AppleScript, which has the benefit of not requiring System Preferences to be open/running/visible. It's also much faster.
If you do happen to have it open in order to monitor whether the script below works, bear in mind that the GUI (what you see) is not updated until you close System Preferences, and open it up again.
To set Automatically rearrange Spaces based on most recent use to true (i.e. so the checkbox is ticked):
do shell script "defaults write com.apple.dock 'mru-spaces' -bool true; killall Dock"
To set it to false, i.e. untick the checkbox, change true to false in the line of code above.
To toggle the setting, this short script will achieve that:
set currentSetting to ¬
(do shell script ¬
"defaults read com.apple.dock 'mru-spaces'") ¬
as integer as boolean
do shell script ¬
"defaults write com.apple.dock 'mru-spaces' -bool " & ¬
(not currentSetting) & ¬
"; killall Dock"
Note: Tested with MacOS High Sierra, but should work (I believe) in OS X Mavericks and later.
Other Settings
When switching to an application, switch to a Space with open windows for the application
do shell script "defaults write -g AppleSpacesSwitchOnActivate -bool true"
(or false if you want the option off.)
Group windows by application
do shell script "defaults write com.apple.dock 'expose-group-apps' -bool true; killall Dock"
(or false if you want the option off.)
Let me start by saying while both of the other answers prior to this one do work, nonetheless I wouldn't use either one of them for the following reasons.
The answer presented by shadowsheep works however it needlessly exposes the System Preferences GUI and I believe unless your system is really slow the value of the delay command is excessive by 50% and only one should be necessary in this use case.
The answer presented by CJK works however it uses killall Dock which is visually distracting and causes all minimized windows on all Desktops to be unminimized leading to further visual distractions, and clutters the Desktop(s), which can then require the User to cleanup the mess. Even without other windows open it's still more so a visual distraction then what I'll present.
Now every User has different work habits so maybe none of the reasons mentioned are of any consequence to you. Personally, I work between four virtual Desktops and can have dozens of windows opened in numerous apps across the Desktops with many, if not most minimized at times. Using killall Dock for me is the last thing I want to do most of the time.
With that said, here's an alternative to both of the existing answers prior to this one.
It's probably safe to say that most Users don't open and leave open System Preferences however the following example AppleScript code checks to see if it's running and if so closes it. This is so it can be opened without showing the GUI, so as not to have to see the the visual distraction of have the GUI change as the script progresses.
This example AppleScript code simply toggles the state of the target checkbox:
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
tell application "System Preferences"
reveal pane id "com.apple.preference.expose"
delay 1
tell application "System Events"
tell group 2 of window 1 of application process "System Preferences"
click checkbox "Automatically rearrange Spaces based on most recent use"
end tell
end tell
quit
end tell
This example AppleScript code conditionally clicks the target checkbox using 0 or 1 in if value is equal to 0 then click it. Use 0 to only click it if it's not checked and 1 to only click it if it's checked.
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
tell application "System Preferences"
reveal pane id "com.apple.preference.expose"
delay 1
tell application "System Events"
tell group 2 of window 1 of application process "System Preferences"
tell checkbox "Automatically rearrange Spaces based on most recent use"
if value is equal to 0 then click it
end tell
end tell
end tell
quit
end tell
Both example AppleScript code blocks shown work fast and without seeing the System Preferences GUI and the only visual effect is the Dock Tile for System Preferences does a single bounce and may not even be noticeable, especially when compared to the visual distraction of killall Dock.
Note that the value of the delay commands may need to be adjusted for your system, and or additional delay commands may or may not be needed. Adjust values of and or add/remove the delay commands as appropriate.
Note: The example AppleScript code is just that and does not employ any other error handling then what's shown and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.
This should to what you want.
In this example Automatically rearrange Spaces based on most recent use is the checkbox you want to check.
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.expose"
delay 2
tell application "System Events"
click checkbox "Automatically rearrange Spaces based on most recent use" of group 2 of window "Mission Control" of application process "System Preferences"
end tell
quit
end tell
And this if you wanna check it only if it's not checked:
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.expose"
delay 2
tell application "System Events"
tell checkbox "Automatically rearrange Spaces based on most recent use" of group 2 of window "Mission Control" of application process "System Preferences"
if (get its value) = 0 then click it
end tell
end tell
quit
end tell
And if you wanna list all the UIElements in the window:
set myArray to {}
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.expose"
delay 2
tell application "System Events"
tell window "Mission Control" of application process "System Preferences"
repeat with uiElem in entire contents as list
set myArray to myArray & ((class of uiElem as string) & " : " & name of uiElem as string)
end repeat
end tell
end tell
end tell
I'm trying to do a script to quit Xcode with currently building application. My current script only cover to quit Xcode but I'm unable to handle the confirmation dialog box that pop-ups on the Xcode. On the confirmation box, there are two button options: Cancel or Stop Tasks. I wanted to click on Stop Tasks button. Please help.
Here's my current AppleScript so far:
set apps to {"Simulator","Xcode"}
repeat with thisApp in apps
tell application thisApp to quit
end repeat
tell application "System Events"
tell process "Xcode"
click button "Stop Tasks" of sheet 1 of window 1
end tell
end tell
This is my current applescript that didn't work.
I'm able to fix my issue regarding terminating Xcode with confirmation dialog box prompt. Here's my the applescript I've come up:
try
with timeout of 10 seconds
quit application "Simulator"
quit application "Xcode"
end timeout
on error number e
if e is -1712 then
activate application "Xcode"
tell application "System Events"
tell application process "Xcode"
keystroke return
end tell
end tell
end if
end try
Does anyone experience AppleScript in working with IKEv2 network service?
In El Capitan, I can create an IKEv2 VPN connection and connect correctly. However AppleScript doesn't work with that kind of connection/service, it cannot get the service with name, it cannot list the connection from the service.
tell application "System Events"
tell current location of network preferences
set service_name to "IKEv2_connection_name"
do shell script (do shell script "scutil --nc start \"" & service_name & "\"")
end tell
end tell
And here is the error:
error "System Events got an error: No service" number 1
It appears that AppleScript cannot recognize the IKEv2 VPN connection. So I tried to run another script which to print out all the current internet connections in the system:
tell application "System Events"
tell current location of network preferences
set names to get name of every service
end tell
end tell
The result shows all the network connections (including "Wi-Fi", "USB Ethernet", "Bluetooth PAN", "Thunderbolt Bridge", all VPN connections of type L2TP, PTPP, IPSec) but it doesn't list any IKEv2 connections although I have set a few of them and they're all working.
I was able to get this to work using UI scripting. So far this script seems to work well, and I think I've made it about as user friendly as possible. It requires System Preferences to be opened at all times, but the window can be hidden. If System Preferences is closed when the script starts, the script will launch and then auto-hide System Preferences. Major inconvenience is that System Preferences is essentially locked to the Network pane while the script is running. Feel free to give it a try, let me know if you have any problems or suggestions! You'll need to replace "MY VPN SERVICE NAME" with the name of the VPN service you want to keep connected. You may also want to modify the delay at the bottom.
repeat while true
tell application "System Events"
tell application process "System Preferences"
if not (window 1 exists) then
tell application "System Preferences"
activate
end tell
repeat while not (menu bar 1 exists)
end repeat
repeat while not (menu "System Preferences" of menu bar 1 exists)
end repeat
repeat while not (menu item "Hide System Preferences" of menu "System Preferences" of menu bar 1 exists)
end repeat
delay 3
click menu item "Hide System Preferences" of menu "System Preferences" of menu bar 1
end if
click menu item "Network" of menu "View" of menu bar 1
tell window 1
repeat while not (rows of table 1 of scroll area 1 exists)
end repeat
repeat with current_row in (rows of table 1 of scroll area 1)
if value of static text 1 of current_row contains "MY VPN SERVICE NAME" then
select current_row
exit repeat
end if
end repeat
repeat with current_button in (buttons in group 1)
if name of current_button is equal to "Connect" then
click current_button
exit repeat
end if
end repeat
end tell
end tell
end tell
delay 60
end repeat
This is not a direct answer to your question, but another way to accomplish the same;
This guy created an app (source available also) to do what scutil and AppleScript cannot : https://blog.timac.org/2018/0719-vpnstatus/
There are a few reports of this and it seems to be some changes in AppleScript made in OSX 10.10 which stopped services in connection object to list IKEv2 VPNs.
You are on the right path though, just don't use location:
tell application "System Events"
set service_name to "IKEv2_connection_name"
do shell script "scutil --nc start \"" & service_name & "\""
end tell
Based on this answer, where you can see more options for scutil as well:
In Mac OS X 10.11, Opening a VPN connection window with the command line gives me an error
Basically my goal is to code a key command (option-s) to activate Stickies and create a new note. Right now I have an Alfred 2 generated Automation which links the hot key to the following script:
on alfred_script(q)
tell application "Stickies" to activate
delay .2
tell application "Stickies" to activate
delay .01
tell application "System Events"
keystroke "n" using command down
end tell
end alfred_script
The two activate commands are my attempt to deal with a bug where it opens the application, but doesn't bring it to front. It works seamlessly when the application is open in the background, but it's slow and creates a screen flash when the application isn't already running. The delay is not coming from the application itself because I can open the application and hit command-n as fast as possible, and it always works.
(By the way if you have an idea for how I could hide all other notes and just show the new one, that would be awesome!)
Try this:
launch application "Stickies"
tell application "System Events" to tell process "Stickies"
click menu item "New Note" of menu "File" of menu bar 1
set frontmost to true
end tell
If you run the script by pressing option-s, there might not be enough time to release option before keystroke "n" using command down.
Or this doesn't raise the windows for other notes:
launch application "Stickies"
tell application "System Events" to tell process "Stickies"
click menu item "New Note" of menu "File" of menu bar 1
end tell
do shell script "open -a Stickies"
activate app "Appname" and set frontmost of "Appname" to true raise all windows, but do shell script "open -a Appname" raises only one window.
Hotkeys also have a short delay by default in Alfred, but you can reduce it by changing the trigger behavior:
You could try this alternate way, might have a different effect.
tell application "System Events"
tell process "Stickies"
set frontmost to true
keystroke "n" using command down
keystroke "Hello World" & linefeed & "I'm a new note!"
end tell
end tell
Hiding all other notes, i'd say start a new question for that.