6 Replies Latest reply: Nov 17, 2014 9:23 AM by Tony T1
seblavoie Level 1 Level 1 (0 points)

Is there a way to start OS X new dictation from AppleScript?

 

I tried :

 

 

tell application "DictationIM" to activate

 

but it did not seem to work although it didn't give me an error.

 

Any clue?


MacBook Pro (15-inch Mid 2009), OS X Mountain Lion (10.8.2)
  • Pierre L. Level 5 Level 5 (4,095 points)

    Hi,

     

    If you don't mind using GUI Scripting (you must enable access for assistive devices in the Accessibility System Preference pane), the following script should toggle the Dictation switch between On and Off.

     

    tell application "System Preferences"

        reveal pane id "com.apple.preference.speech"

        -- activate -- only if you wish to make the Dictation & Speech window visible

        tell application "System Events"

            tell process "System Preferences"

                tell window "Dictation & Speech"

                    tell tab group 1

                        click radio button "Dictation"

                        tell radio group 1

                            if value of radio button "On" is 0 then

                                click radio button "On"

                            else

                                click radio button "Off"

                            end if

                        end tell

                    end tell

                    if sheet 1 exists then

                        tell sheet 1

                            click button "Enable Dictation"

                            repeat while sheet 1 exists

                            end repeat

                        end tell

                    end if

                end tell

            end tell

        end tell

        quit -- optional

    end tell

     

    Hope it can help.


     

    Message was edited by: Pierre L.

  • BenS. Level 1 Level 1 (5 points)

    I made this script for a usb controller, hopefully it's what you're looking for!

     

    tell application "System Events"

              set theProcess to first process whose frontmost is true

              tell theProcess

                        repeat 2 times

                        key down {command}

                        key up {command}

                        end repeat

              end tell

    end tell

  • yoghourtbrain Level 1 Level 1 (0 points)

    That's EXACTLY what I was looking for. There's lots of things you can do with the new dictation commands in automator (yosemite), and your script makes turning dictation automatically off afterwards possible.

    Thanks

  • Loren Ryter Level 2 Level 2 (495 points)

    I wish we didn't have to resort to GUI scripting. If there is another way you know of, please post. In the meantime, I've combined the two very helpful scripts above, and added notifications and error checking. It also  auto-quits system preferences if it hadn't been open before, and adds appropriate delays to launch it.  Hope this helps.

     

    set ttoggle to "Off"
    set sprunning to application "System Preferences" is running
    try
         tell application "System Preferences"
              reveal pane id "com.apple.preference.speech"
              if not sprunning then
                   repeat
                        delay 1
                        tell me
                             if application "System Preferences" is running then exit repeat
                        end tell
                   end repeat
                   activate -- seems to be needed if sys pref hadn't been running
                   reveal pane id "com.apple.preference.speech"
                   delay 2
              else
                   activate -- only if you wish to make the Dictation & Speech window visible
              end if
              
              tell application "System Events"
                   tell process "System Preferences"
                        tell window "Dictation & Speech"
                             tell tab group 1
                                  click radio button "Dictation"
                                  tell radio group 1
                                       if value of radio button "On" is 0 then
                                            click radio button "On"
                                            set ttoggle to "On"
                                       else
                                            click radio button "Off"
                                       end if
                                  end tell
                             end tell
                             if sheet 1 exists then
                                  tell sheet 1
                                       click button "Enable Dictation"
                                       repeat while sheet 1 exists
                                       end repeat
                                  end tell
                             end if
                        end tell
                   end tell
                   if ttoggle = "On" then
                        set theProcess to first process whose frontmost is true
                        tell theProcess
                             repeat 2 times
                                  key down {command}
                                  key up {command}
                             end repeat
                        end tell
                   end if
              end tell
              if not sprunning then quit -- optional
         end tell
    on error errmsg
         display notification errmsg with title "Dictation Toggle Error"
    end try
    display notification with title "Dictation Toggle" subtitle ttoggle
    

     

    PS: sorry I have no idea how to properly paste an applescript in these forums.  I've seen the following thread and tried several suggestions, but nothing worked correctly. So I just used 'pre' tags. Hints on that in this thread appreciated.

     

    Format AppleScript for Forum Post?

  • Loren Ryter Level 2 Level 2 (495 points)

    I added this portion (the else part in this if/then block), to kill the daemon, which took up a lot of memory (in my case almost 900 MB), see this thread:

     

    com.apple.SpeechRecognitionCore.speechrecognitiond using significant memory

    if ttoggle = "On" then
      set theProcess to first process whose frontmost is true
      tell theProcess
      repeat 2 times
      key down {command}
      key up {command}
      end repeat
      end tell
      else
      -- optionally kill the daemon; comment out if afraid 
      try
      do shell script "killall -9 com.apple.SpeechRecognitionCore.speechrecognitiond"
      end try
      end if
    
  • Tony T1 Level 6 Level 6 (8,805 points)

    seblavoie wrote:

    Is there a way to start OS X new dictation from AppleScript?

     

    If you're using Yosemite, there's a new Dictation Command Automator:

     

    http://macosxautomation.com/yosemite/index.html

    http://www.macworld.com/article/2834532/ok-mac-using-automators-dictation-comman ds-new-in-yosemite.html