简体   繁体   English

Deepl Translator 的 Automator 快速操作 (AppleScript)

[英]Automator Quick Action (AppleScript) for Deepl Translator

I have an Automator quick action (service) to get texts from applications and open them on the Deepl website to translate them.我有一个 Automator 快速操作(服务)来从应用程序中获取文本并在 Deepl 网站上打开它们来翻译它们。 It works, but the spaces between the words get replaced with + signs.它有效,但单词之间的空格被替换为 + 符号。 The translation is filled with + signs, like this:翻译用 + 号填充,如下所示:

"...+in+third+place+on+the+list+of+the+most+..." “...+in+第三+位置+on++list+of+of+最+...”

What is causing this?这是什么原因造成的?

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

I'd use ' Listing 32-7 AppleScriptObjC: Handler that URL encodes text' from Encoding and Decoding Text .我会使用来自Encoding and Decoding Text的“列表 32-7 AppleScriptObjC:URL 编码文本的处理程序”。

Example AppleScript code :示例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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM