Created
May 12, 2021 15:15
-
-
Save WestonThayer/724fb02b04c73e179e6f039d6be42fbc to your computer and use it in GitHub Desktop.
Via https://github.com/TyflosAccessibleSoftware/VoiceOverTranslator/blob/master/VoiceOverTranslator.scpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
VoiceOver Translator | |
Version: 1.1 | |
Copyright 2020 Tyflos Accessible Software. All rights reserved. | |
You may incorporate this Tyflos Accessible Software sample code into your system and program(s) without restriction. | |
This sample code has been provided "AS IS" and the responsibility for its operation is yours. | |
You are not permitted to redistribute this Tyflos Accessible Software sample code as "Tyflos Accessible Software sample code" after having made changes. If you're going to redistribute the code, we require that you make it clear that the code was descended from Tyflos Accessible Software sample code, but that you've made changes. | |
If you have any suggestion or petition about this software please send an e-Mail to Tyflos Accessible Software using this e-Mail address: | |
Tyflosaccessiblesoftware@gmail.com | |
*) | |
global destinationLanguage, pythonInterpreterPath, voiceOverOutput, retriesForError | |
on setup() | |
set destinationLanguage to "es" | |
set pythonInterpreterPath to "/usr/local/bin/python3" | |
set voiceOverOutput to true | |
set retriesForError to 5 | |
end setup | |
on run | |
do shell script "afplay /System/Library/Sounds/Morse.aiff" | |
setup() | |
set sourceText to getSourceText() | |
set translationFound to false | |
set retries to 0 | |
repeat while translationFound = false | |
set translation to getTranslatedText(sourceText, destinationLanguage, pythonInterpreterPath) | |
if translation = "%error%" then | |
delay (2) | |
else | |
set translationFound to true | |
end if | |
set retries to retries + 1 | |
if retries ≥ retriesForError then | |
set translationFound to true | |
do shell script "afplay /System/Library/Sounds/Basso.aiff" | |
set translation to "Se produjo un error accediendo al servicio de traducción." | |
end if | |
end repeat | |
sayWithVoiceOver(translation) | |
end run | |
-- VoiceOver functions | |
on sayWithVoiceOver(textToSay) | |
if voiceOverOutput then | |
try | |
tell application "VoiceOver" | |
output textToSay | |
end tell | |
on error | |
say textToSay | |
end try | |
else | |
say textToSay | |
end if | |
end sayWithVoiceOver | |
on getSpokenTextByVoiceOver() | |
set textLine to "" | |
tell application "VoiceOver" | |
set textLine to content of last phrase | |
end tell | |
return textLine | |
end getSpokenTextByVoiceOver | |
-- Helper functions | |
on replaceChars(this_text, search_string, replacement_string) | |
set AppleScript's text item delimiters to the search_string | |
set the item_list to every text item of this_text | |
set AppleScript's text item delimiters to the replacement_string | |
set this_text to the item_list as string | |
set AppleScript's text item delimiters to "" | |
return this_text | |
end replaceChars | |
-- Translation functions | |
on getTranslatedText(textToTranslate, destinationLanguage, pythonPath) | |
try | |
set code to "from googletrans import Translator; translator = Translator();print(translator.translate('" & textToTranslate & "',dest='" & destinationLanguage & "').text)" | |
set translationResult to do shell script pythonPath & " -c \"" & code & "\"" | |
do shell script "afplay /System/Library/Sounds/Pop.aiff" | |
return translationResult | |
on error | |
do shell script "afplay /System/Library/Sounds/Funk.aiff" | |
return "%error%" | |
end try | |
end getTranslatedText | |
on getSourceText() | |
set result to replaceChars(getSpokenTextByVoiceOver(), "'", "\\'") | |
set result to replaceChars(result, "\"", "\\\"") | |
set result to replaceChars(result, " | |
", " ") | |
return result | |
end getSourceText |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment