Voice Control with Mac's Standard Features
Operating with voice commands feels like something out of a sci-fi movie, doesn't it?
If you have a smartphone, you can easily try it out with Siri or Google Assistant.
However, to increase efficiency in your daily PC work, your computer needs to recognize your voice and execute commands. Since Siri, which comes standard, won't execute commands for you, you need to use a different method.
Fortunately, Mac has a voice control feature, so let's use it to achieve this.
Capabilities
- You can call scripts with your voice
- More specifically, you can call Automator's Workflow, which allows you to do various things
- You can set your favorite invocation words
- This feature is unique, as you can't change the invocation words for Siri or Google Assistant (at least for now)
- Giving it a good name might make you more attached to it...
- You can perform parallel tasks
- Commands will be executed in the background without opening a new terminal tab
Overview
The process is as follows:
- Enable Dictation
- Create a Workflow with Automator
- Call the Workflow with Dictation Command
Let's take a look at the settings.
Settings
Assuming you're using macOS Sierra.
Enable Dictation
Go to "Setting" → "Keyboard" → "Dictation" and turn on "Dictation" and "Use Enhanced Dictation".
Also, go to "Setting" → "Accessibility" → "Dictation" and set "Enable the dictation keyword phrase:". The default is "Computer", so change it to your favorite name.
You'll see various settings in "Dictation Commands", but if you don't plan to use them, it's better to delete them. We'll add custom commands here.
Create a Workflow with Automator
We'll execute commands through Workflow. Launch Automator and create a new Workflow.
Choose "Run Shell Script" as the type, and "/bin/bash" as the shell. Enter the absolute path of the shell script or write the command directly in the text area. It's recommended to prepare a separate shell script and call it to make management easier.
Try running it to see if it works, and then move on to setting up voice control.
Call the Workflow with Dictation Command
Go to "Setting" → "Accessibility" → "Dictation" → "Dictation Commands" and add a new item.
Enter the phrase you want to be recognized in "When I say:", and select "Run Workflow" in "Perform:" and specify the Workflow you created earlier.
If you call it with "${prefix} ${when I say}" and see a gear icon in the taskbar, you've succeeded!
Scripting Techniques
To confirm whether the script was executed correctly, it's a good idea to keep a log of the start and end times. Also, if the process takes some time, it's nice to receive a notification when it's finished.
I've created a general-purpose library like the one below, and individual commands read and execute it.
#!/bin/bash
SCRIPT_DIR=$(cd $(dirname $0);pwd)
function display_notification () {
osascript -e 'on run argv
display notification current date as text with title item 1 of argv
end run' -- "$*"
}
slog() {
echo "[$(date)] $0 start" >> ${SCRIPT_DIR}/log.txt
}
elog() {
echo "[$(date)] $0 end" >> ${SCRIPT_DIR}/log.txt
display_notification $0
}
Individual commands will look like this:
#!/bin/bash
set -eux
SCRIPT_DIR=$(cd $(dirname $0);pwd)
. ${SCRIPT_DIR}/common_util.sh
slog
# Process goes here
elog
This way, a log.txt file will be created in the same directory as the script, and logs will be written. You'll also receive an OS notification when it's finished.
Difficulties
The standard Dictation Command has limitations, and it's not suitable for complex tasks or scripts that take arguments. You might want to rely on other apps like Siri for those.
Since there's no feedback, it's often unclear whether the key phrase was recognized or not.
Also, short key phrases and commands often lead to false positives. For commands with significant impact, it's better not to use voice control or combine multiple commands ().
Conclusion
This article introduced a method to call shell scripts with voice commands, giving you a sense of futuristic efficiency. By the way, this approach also encourages you to think about efficiency in your daily work by consolidating processes into shell scripts.
Since Mac provides this feature for free, please give it a try.