-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscribe.scpt
More file actions
132 lines (96 loc) · 4.06 KB
/
transcribe.scpt
File metadata and controls
132 lines (96 loc) · 4.06 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-- Get the path to the script
set scriptFilePath to POSIX path of (path to me)
-- Remove the script's file name to get only the directory
set AppleScript's text item delimiters to "/"
set scriptDir to (text items 1 thru -2 of scriptFilePath) as text
set AppleScript's text item delimiters to ""
-- Log or display the script directory
log "Script directory: " & scriptDir
on removeFiles(scriptDir)
-- Remove the output files from the script directory
do shell script "rm -f " & scriptDir & "/_output.wav && rm -f " & scriptDir & "/_output.log && rm -f " & scriptDir & "/_output.txt"
end removeFiles
-- Define the FFmpeg command to record audio from the default microphone
removeFiles(scriptDir)
set ffmpegCommand to "PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin; ffmpeg -f avfoundation -i :1 " & scriptDir & "/_output.wav >> " & scriptDir & "/_output.log 2>&1 & echo $!"
-- Start recording and get the process ID (PID) of the FFmpeg process
try
set ffmpegPID to do shell script ffmpegCommand
on error errMsg
removeFiles(scriptDir)
display dialog "Error starting FFmpeg: " & errMsg
return
end try
log "ffmpegPID: " & ffmpegPID
-- Show dialog with "Czech" and "English" buttons
set userResponse to button returned of (display dialog "Speak now!" & return & return & "Listening..." & return & return & "...select the spoken language to transcribe" buttons {"cs", "en", "Close"} default button "cs")
-- Set the selected language into the variable
set lang to userResponse
if lang is "Close" then
-- Stop the FFmpeg recording process using the PID
log "Stopping the FFmpeg recording process..."
try
do shell script "kill -INT " & ffmpegPID
on error errMsg
display dialog "Error stopping FFmpeg: " & errMsg
end try
return
end if
-- Stop the FFmpeg recording process using the PID
log "Stopping the FFmpeg recording process..."
try
do shell script "kill -INT " & ffmpegPID
on error errMsg
removeFiles(scriptDir)
display dialog "Error stopping FFmpeg: " & errMsg
return
end try
delay 0.2
-- Check if openai.txt exists and read the API key
set openaiFilePath to scriptDir & "/openai.txt"
set useOpenAI to false
set apiKey to ""
try
set apiKey to do shell script "cat " & openaiFilePath
set useOpenAI to true
on error
log "openai.txt not found, using local Whisper"
end try
if useOpenAI then
set whisperCommand to "curl -X POST https://api.openai.com/v1/audio/transcriptions -H 'Authorization: Bearer " & apiKey & "' -F file=@" & scriptDir & "/_output.wav -F model=whisper-1 -F language=" & lang & " -F response_format=text > " & scriptDir & "/_output.txt"
display notification "Processing with OpenAI API..." with title "MacScriptable"
else
set whisperCommand to "PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin; whisper " & scriptDir & "/_output.wav --output_dir " & scriptDir & " --language " & lang & " --model turbo --output_format txt --task transcribe >> " & scriptDir & "/_output.log"
display notification "Processing with local whisper..." with title "MacScriptable"
end if
log "Running transcribe..."
try
do shell script whisperCommand
on error errMsg
removeFiles(scriptDir)
display dialog "Error running Whisper: " & errMsg
return
end try
-- Read the transcribed text from the output filewww.hradeckralove.org
try
set transcription to do shell script "cat " & scriptDir & "/_output.txt"
on error errMsg
removeFiles(scriptDir)
display dialog "Error reading the transcribed text: " & errMsg
return
end try
log "Transcription: " & transcription
removeFiles(scriptDir)
-- Set the clipboard to the transcribed text
set the clipboard to transcription
display notification "Transcription in clipboard :-)" with title "MacScriptable"
-- Paste the transcribed text into the current focused input
tell application "System Events"
tell application "System Events"
set lastApp to item 1 of (get name of processes whose frontmost is true)
end tell
do shell script "open -a " & quoted form of lastApp
delay 0.2
keystroke "v" using command down
end tell
log "Done..."