Deepl Translator 的 Automator Quick Action (AppleScript)

Automator Quick Action (AppleScript) for Deepl Translator

我有一个 Automator 快速操作(服务)可以从应用程序中获取文本并在 Deepl 网站上打开它们进行翻译。它有效,但单词之间的空格被替换为 + 号。翻译中充满了+号,像这样:

"...+in+third+place+on+the+list+of+the+most+..."

这是什么原因造成的?

on run {input, parameters}
    set output to "https://www.deepl.com/translator#pt/en/" & urldecode(input as string)
    return output
end run

on urldecode(x)
    set cmd to "'require \"cgi\"; puts CGI.escape(STDIN.read.chomp)'"
    do shell script "echo " & quoted form of x & " | ruby -e " & cmd
end urldecode

我会使用来自 Encoding and Decoding Text 的“清单 32-7 AppleScriptObjC:URL 编码文本的处理程序”。

示例 AppleScript 代码:

use framework "Foundation"
use scripting additions

on run {input, parameters}
    set output to "https://www.deepl.com/translator#pt/en/" & encodeText(input as string)
    return output
end run

on encodeText(theText)
    set theString to stringWithString_(theText) of NSString of current application
    set theEncoding to NSUTF8StringEncoding of current application
    set theAdjustedString to stringByAddingPercentEscapesUsingEncoding_(theEncoding) of theString
    return (theAdjustedString as string)
end encodeText