A fun project that uses the OpenAI TTS (text-to-speech) model to read selected text on your Mac with a keyboard shortcut. This is instead of using the default macOS accessibility text reader, since OpenAI's text-to-speech sounds much better.
As of June 2024, the OpenAI TTS model is priced at $15 per 1 million characters processed.
Select some text on a webpage or in the Apple Notes app, for example, and press the keyboard shortcut you defined. A window will appear letting you know how much the request will cost. (You can easily disable this feature if you want.)
![cost](https://private-user-images.githubusercontent.com/50992194/338054867-76ea01cf-fed3-4ee9-a766-5f5c2308aff6.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA5NDI3MTUsIm5iZiI6MTcyMDk0MjQxNSwicGF0aCI6Ii81MDk5MjE5NC8zMzgwNTQ4NjctNzZlYTAxY2YtZmVkMy00ZWU5LWE3NjYtNWY1YzIzMDhhZmY2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE0VDA3MzMzNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTU3ZjJjYjZkMTI5NjQ5MjVhZjIwMzQ0OGQyYmIzODk2MDlhMzc4NDM3ZmY0YTBiNzAwYTlhOWM0NTYzOWNkZWYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.2XN-0i2h4lxB4JUeEMXK2m0ntG5-bavl2Pm3mf7wQcY)
If you press continue, after a few seconds, the OpenAI-generated audio will start playing out of your system's default audio output and reading your text aloud.
I'm using a combination of Python and Apple Scripts, plus an Automator Workflow to tie it all together and turn it into a macOS "Quick Action". Here's the Automator Workflow:
-
If you don't already have one, sign up for an OpenAI account and generate an API key. See their quickstart guide for more info.
-
Install the OpenAI Python library
pip install openai
-
Download both Python scripts
openai-text-to-speech.py
andopenai-tts-cost-estimator.py
and theOpenAI Text to Speech.zip
file containing the Automator Workflow from this repo. -
Open
openai-text-to-speech.py
and replaceYOUR_OPENAI_API_KEY_GOES_HERE
with your OpenAI API key. -
Place both Python scripts in
/usr/local/bin
. -
Unzip the
OpenAI Text to Speech.zip
file and double click theOpenAI Text to Speech.workflow
file to install it as a System Quick Action, or right click and open it with Automator to modify it to your liking first. -
Finally, set up a keyboard shortcut on your Mac by visiting System Settings > Keyboard > Keyboard Shortcuts... > Services. Expand the
Text
list and find theOpenAI Text to Speech
we just installed. Define your desired shortcut.
You can also run the Quick Action/Service by right clicking on any selected text, and selecting the action from the Services
menu option:
![Screenshot 2024-06-10 at 1 02 47 AM](https://private-user-images.githubusercontent.com/50992194/338056895-d018055e-e8c8-417c-8eca-836e2028a80f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA5NDI3MTUsIm5iZiI6MTcyMDk0MjQxNSwicGF0aCI6Ii81MDk5MjE5NC8zMzgwNTY4OTUtZDAxODA1NWUtZThjOC00MTdjLThlY2EtODM2ZTIwMjhhODBmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE0VDA3MzMzNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTcxNWIwNDBjZGQ0ZmZhODliN2UyMjUwMzgwYTM4MzM4OWVkNWM2NGIwNzBhYzVmOWUxMGI0ZWUzODU2ZjFjYTYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.q2AgCCHqQPDk_mrJw_silLrtZXFlxNlnJsI_qRLM6_c)