Interacting with Siri via the command line in macOS
Asked Answered
P

5

19

I use Siri on my phone and watch to create reminders on the go. When I'm in the office I don't want to disturb the quiet by using Siri, so I usually use an Alfred workflow that is integrated with the Reminders app, or use the Reminders app directly.

However, both have a rather clunky interface, and it would be much easier if I could just type at the command line:

$ siri "remind me to check stack overflow for responses to my question in 15 minutes"

macOS Sierra has introduced Siri to the desktop, but so far I have been unable to find a way to interact with Siri in any way other than literally talking out loud, and Spotlight does not match Siri with natural language comprehension.

Apple has announced the Siri SDK, but it seems primarily related to adding functionality to Siri, not for exposing the Siri API.

Does Apple expose any kind of API to Siri on macOS such that one could make Siri requests via the command line, system call, or other executable?

Note: I understand that this question could conceivably find a better home at Ask Different, Super User, or Unix & Linux. In the end, I decided that some programmatic integration with an API or SDK was the most probable solution, and thus Stack Overflow seemed the most appropriate place to post. If mods disagree, please do migrate to whichever community is best.

Pavonine answered 28/9, 2016 at 23:41 Comment(1)
Just installed the Sierra update and had the same thought as you. Don't want to talk to Siri in the office. Would rather just type commands in bash. This should be a natural feature to have, since Siri's first job upon hearing a command most likely involves parsing that command to text.Grope
S
9

This isnt from the command line, but closer... and I haven't tested it, but in High Sierra there's a way to use Accessibility settings to enable you to use your keyboard to ask Siri questions.

How to enable it:

  • System Preferences > Accessibility > Siri.
  • Click in the box beside Enable Type to Siri so that a tick appears.
  • Now when you trigger Siri, a keyboard will appear into which you can type your query.

Snagged from here: https://www.macworld.co.uk/news/mac-software/how-use-siri-on-mac-3536158/

Subplot answered 28/2, 2018 at 17:11 Comment(0)
H
8

I was wanting the same feature today - I got it working but could be improved upon: https://youtu.be/VRLGCRrReog

TLDR is use LoopBack by Rogue Amoeba and change Siri’s input Mic to Loopback. Then Use the Say command in Terminal for example.

Hugibert answered 8/11, 2016 at 16:37 Comment(2)
Nice hack, but I wish there was a more direct method :)Ocelot
great solution!Screech
S
6

As mentioned by Brad Parks, you can enable 'Type to Siri' from the Accessibility menu. You can use this to interact with Siri using simulated keypresses.

I've created a simple Python script which behaves like requested in your question when invoked from the command line.

The script uses the keyboard Python module.

#!/usr/bin/python
import sys
import time
import keyboard

def trigger_siri():
    keyboard.press('command+space')
    time.sleep(0.3)
    keyboard.release('command+space')
    time.sleep(0.2)  # Wait for Siri to load

if __name__=='__main__':
    trigger_siri()
    keyboard.write(sys.argv[1])
    keyboard.send('enter')
Swithbert answered 24/1, 2019 at 10:51 Comment(1)
That keyboard library seems to download the entire pypi registry....Agave
I
3

Cliclick is a great (and free) tool for triggering mouse and keyboard events via the command line. After installing Cliclick, I enabled "Type to Siri" (System Preferences > Accessibility > Siri). I also changed Siri's keyboard shortcut to "Press Fn (Function) Space" (System Preferences > Siri). The other keyboard shortcut options require you to "Hold" a key, which can be done, but it makes things a bit trickier.

With all that done, I can invoke Siri from the terminal with something like this:

$ cliclick kd:fn kp:space ku:fn w:250 t:"turn on the living room lights" kp:return

Going a step further, if you are familiar with terminal aliases and functions, you can create a "siricli" function:

siricli(){
    cliclick kd:fn kp:space ku:fn w:250 t:"$1" kp:return
}

Open a new terminal window after adding that function, and now you can invoke Siri from the command line like this:

siricli "turn on the living room lights"
Ionic answered 9/10, 2019 at 19:10 Comment(1)
This is my favorite solution. Thank you!Ovoviviparous
A
0

if you enable Type to Siri in the Settings, you can use an AppleScript like this one (you can invoke it thru Terminal's osascript or Shortcuts):

on run {input, parameters}
    tell application "System Events"
        key code 49 using {shift down, command down}
        delay 0.5
        keystroke "Listen to the News"
        delay 0.2
        key code 36
        delay 1
        set visible of application process "Podcasts" to false
    end tell
    return input
end run

Take a look at this page if your Siri shortcut is not cmd+shift+space.

Anarchy answered 7/2 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.