Skip to content

Instantly share code, notes, and snippets.

@deepaks7n
Created May 31, 2025 15:48
Show Gist options
  • Select an option

  • Save deepaks7n/e2aa421f1ddb7c1f6a766201dcefd28f to your computer and use it in GitHub Desktop.

Select an option

Save deepaks7n/e2aa421f1ddb7c1f6a766201dcefd28f to your computer and use it in GitHub Desktop.
a popclip extention to TTS using gpt-4o-mini-tts
#popclip
name: Azure TTS
identifier: com.custom.azuretts
icon: symbol:message.and.waveform
options:
- identifier: api_key
type: string
label: Azure API Key
description: Your Azure OpenAI API Key
- identifier: endpoint
type: string
label: Azure Endpoint
description: Your Azure OpenAI endpoint
- identifier: deployment
type: string
label: Deployment Name
description: Your TTS deployment name
default value: gpt-4o-mini-tts
- identifier: voice
type: string
label: Voice
default value: nova
values:
- nova
- alloy
- echo
- fable
- onyx
- shimmer
- identifier: speed
type: string
label: Speed
default value: "0.85"
values:
- "0.75"
- "0.85"
- "1.0"
- "1.25"
- "1.5"
actions:
- title: Convert to Speech
icon: symbol:message.and.waveform
shortcut: command shift t
interpreter: zsh
shell script: |
# Azure OpenAI API key and endpoint from PopClip options
AZURE_API_KEY="$POPCLIP_OPTION_API_KEY"
AZURE_ENDPOINT="$POPCLIP_OPTION_ENDPOINT"
DEPLOYMENT="$POPCLIP_OPTION_DEPLOYMENT"
VOICE="$POPCLIP_OPTION_VOICE"
SPEED="$POPCLIP_OPTION_SPEED"
# Create temporary directory
temp_dir=$(mktemp -d)
temp_audio_file="$temp_dir/speech.mp3"
trap "rm -rf $temp_dir" EXIT
# Preprocess text - remove problematic characters and trim lengthy text
TEXT_TO_CONVERT=$(echo "$POPCLIP_TEXT" | tr -d '\000-\010\013\014\016-\037' | cut -c 1-4000)
# If text is too long, truncate and add message
if [[ ${#TEXT_TO_CONVERT} -gt 4000 ]]; then
TEXT_TO_CONVERT="${TEXT_TO_CONVERT:0:3970}... [text truncated]"
fi
# Call the Azure OpenAI API
curl -s "$AZURE_ENDPOINT/openai/deployments/$DEPLOYMENT/audio/speech?api-version=2025-03-01-preview" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_API_KEY" \
-d '{
"model": "tts-1-hd",
"input": "'"$TEXT_TO_CONVERT"'",
"voice": "'"$VOICE"'",
"response_format": "mp3",
"speed": '"$SPEED"'
}' --output "$temp_audio_file"
# Play the audio if file exists and has content
if [[ -s "$temp_audio_file" ]]; then
afplay "$temp_audio_file"
else
osascript -e 'display notification "API call failed" with title "Azure TTS Error"'
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment