Do you get the expected result when you run the script in Terminal.app?
Not sure whether that works but you could try to add export LANG="en_US.UTF-8" && to the beginning of the script. If it doesn’t work you need to try another language. Unfortunately I can’t test this.
No idea how to do directly it in a shell script, but as you’re using AppleScript (to call the shell script) this should work:
set the clipboard to (text 1 thru -2 in (do shell script "export LANG='en_US.UTF-8' && echo '{popclip text}' | /opt/homebrew/bin/autocorrect --stdin"))
Generally, the script works well, but it has an issue if there isn’t an empty line after the line of the selected texts, the final character will be stripped. I.e., Hello世界 would be Hello 世.
I am not familiar with AppleScript. Could you please help explain what text 1 thru -2 means here. It seems text 1 thru -1 works as expected.
text 1 thru -2 is the whole text except the last character.
If the output varies (i.e. sometimes contains a trailing new line and sometimes doesn’t) then you need to use a conditional that checks whether it’s necessary to strip the last character
set theOutput to (do shell script "export LANG='en_US.UTF-8' && echo '{popclip text}' | /opt/homebrew/bin/autocorrect --stdin"))
if (character -1 of theOutput is in {linefeed, return}) then set theOutput to (text 1 thru -2 in theOutput)
set the clipboard to theOutput
# popclip AutoCorrect
name: AutoCorrect
title: CJK Formatting
icon: square CJK
applescript: |
set theOutput to (do shell script "export LANG='en_US.UTF-8' && echo '{popclip text}' | /opt/homebrew/bin/autocorrect --stdin"))
if (character -1 of theOutput is in {linefeed, return}) then set theOutput to (text 1 thru -2 in theOutput)
set the clipboard to theOutput
after: paste
# popclip AutoCorrect
name: AutoCorrect
title: CJK Formatting
icon: square CJK
applescript: set theOutput to (do shell script "export LANG='en_US.UTF-8' && echo '{popclip text}' | /opt/homebrew/bin/autocorrect --stdin"))
if (character -1 of theOutput is in {linefeed, return}) then set theOutput to (text 1 thru -2 in theOutput)
set the clipboard to theOutput
after: paste
I won’t add to the discussion specifics (thanks @Pete) but just to say if you are doing shell scripting for anything more complicated than a very simple comand, I would recommend doing it with a “traditional” extension inside a popclipext directory, with an actual shell script file. This way, you don’t have to worry about escaping your shell script inside an applescript inside a YAML file.
Also if you do it that way, in general you don’t need to pbcopy at the end of a script then paste. You can return the result as stdout from the script then paste-result directly instead.