Using Mac Dictation in Python

Does anyone have any ideas on how to use Mac's built-in dictation tool to create strings that Python will use?

To start the dictation, you need to press the Fn key twice inside any text editor. If so, is there a way to combine a keypress command with an input command? Sort of:

Step 1: Simulate a keystroke to press the Fn key twice, start the dictation tool, and then Step 2. Create a variable using the speech-text content as part of the input function, that is text_string = input ("Start dictation:")

In this thread ( Can I use speech recognition / dictation of OS X 10.8 without a graphical interface? ), The user offers him to deal with CGEventCreateKeyboardEvent (src, 0x3F, true), but there is no code.

Any ideas? Code samples will be appreciated.

UPDATE: thanks to the suggestions below, I have imported AppScript. I try to make the code work along these lines, without success:

from appscript import app, its
se = app('System Events')
proc = app.processes[its.frontmost == True]
mi = proc.menu_bars[1].menu_bar_items['Edit'].menus[1].menu_items['Start Dictation']
user_voice_text = input(mi.click())
print(user_voice_text)

Any ideas on how I can turn on a dictation tool to enter a string?

UPDATE 2:

Here is a simple example of the program I'm trying to create:

Ideally i want to launch the program, and then have it ask me: "what is 1 + 1?"
Then I want the program to turn on the dictation tool, and I want the program to record my voice, with me answering "two".
The dictation-to-text function will then pass the string value = "two" to my program, and an if statement is then used to say back "correct" or "incorrect".

I try to pass commands to the program without even typing on the keyboard.

+4
source share
1 answer

-, FnFn NSText (, , NSTextView?) Cocoa. , . ( .) , NSTextView, "", " " , FnFn - , , , , , , , .

, GUI, , .

, - NSMenu ,

- , PyQt Tkinter, . , Cocoa ( PyObjC, Python Apple, pip install, Python):

import AppKit
mb = AppKit.NSApp.mainMenu()
edit = mb.itemWithTitle_('Edit').submenu()
sd = edit.indexOfItemWithTitle_('Start Dictation')
edit.performActionForItemAtIndex_(sd)

, ( Terminal.app , iTerm), , , "", .

, , . OS X " " . 10.10 "" " " " " "", , . , , , , , , , , , - .

AppleScript :

tell application "System Events"
    click (menu item "Start Dictation" of menu of menu bar item "Edit" 
        of menu bar of (first process whose frontmost is true))
end tell

"" Python - ScriptingBridge, PyObjC... appscript:

from appscript import app, its
se = app('System Events')
proc = app.processes[its.frontmost == True]
mi = proc.menu_bars[1].menu_bar_items['Edit'].menus[1].menu_items['Start Dictation']
mi.click()

Fn, API Quartz Events Services, ( API- CoreFoundation C, API- Cocoa ObjC) PyObjC. , , , , . , Fn-key, :

evt = Quartz.CGEventCreateKeyboardEvent(None, 63, True)
Quartz.CGEventPost(Quartz.kCGSessionEventTap, evt)

, True False.

+3

All Articles