0

I have on Applescript to start and another one to stop Dictation. I tried to run them together and it only starts Dictation but does not stop it. I tried the key code way but this script is to be used with VoiceOver and that method does not work. Is there a way to have this script check if start/stop is ready and to act accordingly?

    tell application "System Events"
        tell (get first process whose frontmost is true)
           tell menu bar 1
                tell menu bar item "Edit"
                    tell menu 1
                        click menu item "Start Dictation"
                    end tell
                end tell
            end tell
        end tell
    end tell
    tell application "System Events"
        tell (get first process whose frontmost is true)
            tell menu bar 1
                tell menu bar item "Edit"
                    tell menu 1
                        click menu item "Stop Dictation"
                    end tell
                end tell
            end tell
        end tell
    end tell
Elias
  • 217
  • 1
  • 3
  • 13

1 Answers1

0

You can use the exists command

tell application "System Events"
    tell (get first process whose frontmost is true)
        tell menu 1 of menu bar item "Edit" of menu bar 1
            if exists of menu item "Start Dictation" then
                click menu item "Start Dictation"
            else if exists of menu item "Stop Dictation" then
                click menu item "Stop Dictation"
            end if
        end tell
    end tell
end tell

--

Or you can use a word in the name of the menu, like this:

tell application "System Events"
    tell (get first process whose frontmost is true)
        tell menu 1 of menu bar item "Edit" of menu bar 1
            click (first menu item whose name contains " Dictation") -- start or stop
        end tell
    end tell
end tell
jackjr300
  • 7,111
  • 2
  • 15
  • 25
  • Thanks for the help. Both scripts work fine when launching them with a script launcher like Better touch tool, however, I am trying to assign this to a gesture in the OSX built in VoiceOver utility (Trackpad commander). When assigned to gesture like command or shift swipe right, it launches dictation and when I invoke the gesture again, it launches dictation again instead of stopping it. Any ideas on why this is happening? I tried adding a delay with no success. – Elias Sep 11 '14 at 21:27
  • I had not thought of that, but it is impossible to stop the dictation by script, since the dictation stops automatically when launching a service that opens a menu by emulation, click or shortcut. – jackjr300 Sep 11 '14 at 22:43