Jump to content

Alfred + Dictation = Keyboard-Free Power


Recommended Posts

Alfred Remote combined with Apple's built-in Dictation seems to have amazing potential.

 

First, set up the following script in Alfred Remote:

 

1. Start Alfred Preferences

2. Click Remote

3. Click System Commands (or wherever you want to put the commandon your remote)

4. Click one of the + signs on the grid

5. Select Run Script from the pop-up menu

6. In the window that opens, set the Language drop-down to /usr/bin/osascript

7. In the script field, paste in the following short Applescript:

tell application "System Events"

-- mute volume while dictating, or turn it back on when done
-- optionally one could add commands here to play/pause iTunes etc. here
set myVolume to get volume settings
		if output muted of myVolume is false then
    set volume with output muted
		else
    set volume without output muted
		end if
-- toggle dictation
	key code 63 -- fn
	key code 63
end tell

I then added this icon as well: https://ntk.me/assets/img/2012/12/17/dictation/icon.png

 

Second, if you don't have it enabled already, turn on Dictation and enable Advanced Commands as described here:

 

http://support.apple.com/en-us/HT203085

 

Now you can use Alfred Remote to turn on Dictation, Show Alfred (or your custom search, workflow, etc.), speak your search terms, (optionally edit as necessary with advanced dictation commands), then speak the command "Press Return key".  Magical.

 

Now....this could be even more awesome if Alfred could turn on Dictation then redirect the iPhone microphone to the Mac audio input...then I could stand at the back of my classroom with 500 students and run my laptop on the podium....

Edited by dfay
Link to comment

You can also add Advanced Commands to Dictation via Automator workflows in Yosemite (see http://www.macworld.com/article/2834532/ok-mac-using-automators-dictation-commands-new-in-yosemite.html for an overview).  Here are a pair which add commands to Hold and Release the Control key (useful for Alfred workflows that offer options when Control is down).  (I also describe how to do this from Alfred Remote here:  http://www.alfredforum.com/topic/5414-sending-keyboard-modifiers-ctrl-etc/ ).

 

ctrl-automator.png

Edited by dfay
Link to comment

Hi dfay,

Thanks a lot, this is amazing!

I'm new to this - I'd like to add one action to your script, but I'm having trouble with Apple Script and key strokes.

The idea would be to open the Alfred window at the same time as dictation is activated, allowing dictation in the search box right away.

As my shortcut is set top option+space, i tried the adding one line as shown below  but no dice - it breaks your script functionality.

key code 58 using {Space}
key code 63 -- fn  
key code 63

Could you help?

Thanks!

Link to comment

People are going a bit too wild with trying to send key codes rather than using native AppleScript functionality (I'm seeing this in multiple threads). If you have shortcuts that link to some functionality, it's always better for your code to aim for the functionality rather than the shortcuts. It makes the code more reliable, especially because hot keys are context dependent, can easily be remapped, and depend (sometimes) on a certain keyboard layout.

 

So, if you're trying to get Alfred to show, don't send the keycodes to launch Alfred. Instead, launch Alfred with the command:

tell application "Alfred 2" to search ""

That will always work.

Link to comment

Doing a Skim search with dictation and then advancing page results with Alfred remote. 

 

 

1. Remote Trigger --> Keyword

2. Dictate Query --> Skim Search Applescript

3. Use Run Script on the Remote to advance page results. (icon)

 

 

The search Applescript is "listening" (not really, but you get the idea) for keypress with: 

 

on isControlKeyPressed()

return (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSControlKeyMask > 1'") is "True"

end isControlKeyPressed

 

To advance the page use:

 

tell application id "sevs"

control key down

delay 0.2

control key up

end tell

Edited by DrLulz
Link to comment

This sounds awesome. Can someone simplify all the steps and code to use this? I am not savvy enough to follow the details and implement it. If not, perhaps Vero can do a detailed post on it. I am so excited by Alfred Remote and use it all the time now.

Edited by HehMan
Link to comment

@DrLulz

I was curious to see if this can be done entirely in a native AppleScript version or at least cutting out the python by using AppleScriptObjC....

 

I got the following parsimonious bit of code to work in Script Editor, but I haven't had time to try to import it as a library into your script:

 

          use framework "Cocoa"

 

on isControlKeyPressed()

current application's NSEvent's modifierFlags as integer = current application's NSControlKeyMask

end isControlKeyPressed

 

delay 3

isControlKeyPressed()

 

 

(last two lines are just there for testing purposes)

 

Doing a Skim search with dictation and then advancing page results with Alfred remote. 

 

 

1. Remote Trigger --> Keyword

2. Dictate Query --> Skim Search Applescript

3. Use Run Script on the Remote to advance page results. (icon)

 

 

The search Applescript is "listening" (not really, but you get the idea) for keypress with: 

 

on isControlKeyPressed()

return (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSControlKeyMask > 1'") is "True"

end isControlKeyPressed

 

To advance the page use:

 

tell application id "sevs"

control key down

delay 0.2

control key up

end tell

Edited by dfay
Link to comment

I have entered the Apple Script exactly as dfay indicated in the first post. Dictation works perfectly. When I say "Press Return Key", a short message box (Press Return Key) appears above the dictation microphone and the cursor advances one line. I still must manually hit the Enter key to stop dictation. And yes, I have enabled dictation commands in System Preferences-Accessibiliry-Dictation Commands-Enable Advanced Commands. What am I missing?

Edited by HehMan
Link to comment

HehMan ... it's not entirely clear what you're trying to do.

 

If you're in Alfred and say "Press Return Key", Alfred will behave as if you hit the key, by running the search / query / workflow in the input box.

 

If you're in a text editor and say "Press Return Key", it will also behave as if you hit they key, by advancing the cursor one line.

 

To stop dictation you can

1) use Alfred Remote and the same command above - it will toggle Dictation on or off depending on the state at the time it's run; or,

2) say "Stop Dictation" if you have Advanced Commands turned on as described above

3) press the function key twice

 

Hope this helps.

Link to comment
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...