-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_text.sh
More file actions
executable file
·109 lines (89 loc) · 2.99 KB
/
process_text.sh
File metadata and controls
executable file
·109 lines (89 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# Process Text Utility
# Sends selected text to Ollama with a custom prompt and copies result to clipboard
# Ensure UTF-8 encoding throughout
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# Read selected text from stdin
SELECTED_TEXT=$(cat)
if [ -z "$SELECTED_TEXT" ]; then
osascript -e 'display notification "No text selected" with title "Process Text"'
exit 1
fi
# Check if Ollama is running
if ! curl -s --connect-timeout 2 http://localhost:11434/api/tags > /dev/null 2>&1; then
osascript -e 'display alert "Ollama Not Running" message "Please start Ollama first. Run: brew services start ollama"'
exit 1
fi
# Prompt user for the instruction
PROMPT=$(osascript -e 'tell application "System Events"
activate
set userInput to display dialog "How should I process this text?" default answer "" buttons {"Cancel", "Process"} default button "Process" with title "Process Text"
return text returned of userInput
end tell' 2>/dev/null)
# Check if user cancelled
if [ -z "$PROMPT" ]; then
exit 0
fi
# Show processing notification
osascript -e 'display notification "Processing text with Ollama..." with title "Process Text"'
# Use Python to handle JSON properly and make the API call
export SELECTED_TEXT
export PROMPT
RESULT=$(python3 << 'PYTHON_SCRIPT'
import json
import urllib.request
import urllib.error
import sys
import os
# Ensure stdout uses UTF-8
sys.stdout.reconfigure(encoding='utf-8')
selected_text = os.environ.get('SELECTED_TEXT', '')
prompt = os.environ.get('PROMPT', '')
full_prompt = f"""{prompt}
Here is the text to process:
{selected_text}
Respond with ONLY the processed text, no explanations or additional commentary."""
payload = {
"model": "llama3.2",
"stream": False,
"messages": [
{
"role": "user",
"content": full_prompt
}
]
}
try:
req = urllib.request.Request(
"http://localhost:11434/api/chat",
data=json.dumps(payload).encode('utf-8'),
headers={"Content-Type": "application/json"}
)
with urllib.request.urlopen(req, timeout=120) as response:
data = json.loads(response.read().decode('utf-8'))
if 'message' in data and 'content' in data['message']:
print(data['message']['content'], end='')
else:
print("ERROR: Unexpected response format", file=sys.stderr)
sys.exit(1)
except urllib.error.URLError as e:
print(f"ERROR: Connection failed - {e}", file=sys.stderr)
sys.exit(1)
except json.JSONDecodeError as e:
print(f"ERROR: Invalid JSON response - {e}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"ERROR: {e}", file=sys.stderr)
sys.exit(1)
PYTHON_SCRIPT
)
# Check exit status
if [ $? -ne 0 ]; then
osascript -e "display alert \"Process Text Error\" message \"$RESULT\""
exit 1
fi
# Copy result to clipboard
echo -n "$RESULT" | pbcopy
# Show success notification
osascript -e 'display notification "Result copied to clipboard!" with title "Process Text" sound name "Glass"'