Install Fastscripts
Install FastscriptsSet up an Applescript, which looks like this:
on run -- Copy the currently selected text to the clipboard tell application "System Events" keystroke "c" using {command down} delay 0.5 -- Wait for the data to be available end tell -- Get the selected text from the clipboard set selectedText to (the clipboard as text) -- Call the Python script and pass the selected text set translatedText to do shell script "echo " & quoted form of > selectedText & " |
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3 /PATH/TO/MY/SCRIPT/DK_openAi.py"
-- Put the translated text back into the clipboard set the clipboard to translatedText -- Paste the translated text tell application "System Events" keystroke "v" using {command down} end tell end run
- REPLACE THE PATH TO THE PYTHON ("/Library/Frameworks/Python.framework/Versions/3.11/bin/python3") WITH YOUR PATH
- REPLACE THE PATH ("/PATH/TO/MY/SCRIPT/DK_openAi.py")TO WHERE YOU STORE THE PYTHON SCRIPT
3.Set up a python script that looks like this:
on run
import openai
import-- sysCopy the currently selected text to the clipboard
tell application "System Events"
# Read the API key keystroke "c" using {command down}
openai.api_key = 'YOUR delay 0.5 -API-KEY' Wait for the data to be available
end tell
# -- Get the selected text from the clipboard
selected_text = sys.stdin.read().strip set selectedText to (the clipboard as text)
# -- Call the OpenAIPython APIscript toand translatepass the selected text
model = "gpt-3.5-turbo"
language = "Danish"
responseset =translatedText openai.ChatCompletion.create(
to do shell script model=model,
"echo " & quoted messages=[
form of > selectedText & " {|
/PATH/TO/YOUR/python3
"role": "user",
/PATH/TO/MY/SCRIPT/DK_openAi.py"
-- Put the translated text "content":back f"Translateinto the following textclipboard
to {language} without changingset the formatting:clipboard {selected_text}",to translatedText
-- Paste the translated }text
]
)
# Grabs thetell responseapplication from"System theEvents"
API and strips it of leading/trailing newlines
translated_text =keystroke response["choices"][0]["message"]["content"].strip()
#"v" Replaceusing the{command selecteddown}
text with the translatedend texttell
print(translated_text,end end="")run
- Replace "YOUR-API_KEY" with your API key and save according to the path defined
- Set up
DK_openAi.py
that looks like this:
4.Set-up shortcuts to run the apple scripts via Fastscripts
import openai
import sys
# Read the API key
openai.api_key = 'YOUR-API-KEY'
# Get the selected text
selected_text = sys.stdin.read().strip()
# Call the OpenAI API to translate the text
model = "gpt-3.5-turbo"
language = "Danish"
response = openai.ChatCompletion.create(
model=model,
messages=[
{
"role": "user",
"content": f"Translate the following text to {language} without changing the formatting: {selected_text}",
}
]
)
# Grabs the response from the API and strips it of leading/trailing newlines
translated_text = response["choices"][0]["message"]["content"].strip()
# Replace the selected text with the translated text
print(translated_text, end="")
- Set-up shortcuts to run the apple scripts via Fastscripts