Create a trigger that reads selected text

I want to create a trigger that reads selected text using the standard 'Start speaking' speech

Tried this apple script (Using Blocking and Async options in BTT)

tell application "System Events"
    set selectedText to the clipboard
    set the clipboard to (the clipboard)
    say selectedText using "Alex"
end tell

But it didn't work

Then I asked Claude to generate a shell script

#!/bin/bash

# Get the text from clipboard
selected_text=$(pbpaste)

# Read the text aloud using the default system voice
say "$selected_text"

# Uncomment the line below and comment the line above if you want to specify a voice
# say -v Alex "$selected_text"

And it didn't work either

I also want this to work in platforms like google docs where the standard macos context menu isn't default

Thanks!

You can use this using the "Run Real Java Script" action:


async function speakSelectedText() {
    let selectedText = await get_string_variable("selected_text");
    await runShellScript({script: `/usr/bin/say "${selectedText}"  -v Alex`})
		return false;
}

It works, Is there an option to show the controls for speed & play/pause similar to the standard speech option in Macos?
image

I selected a paragraph and now it's still reading :slight_smile: :joy:

1 Like

Ah, I needed to figure out how to display this menu. ChatGPT couldn't help me with this, unfortunately. :(((

You can create part of these buttons via floating menu. The say command have rate option, but not sure for pause and the other ones. You can check Tutorial / New Feature: Text Selection Did Change Trigger - #198 by swn and modify it for your needs.

1 Like

to cancel a "speaking session" you could use this command

killall say
1 Like

This works for me

Thanks