【问题标题】:Automator Quick Action (AppleScript) for Deepl TranslatorDeepl Translator 的 Automator 快速操作 (AppleScript)
【发布时间】:2022-01-16 05:23:16
【问题描述】:

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

“...+in+第三位+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

【问题讨论】:

  • 您需要将空格编码为%20 +
  • @user3439894 非常感谢!

标签: macos applescript translation


【解决方案1】:

我会使用来自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

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2021-12-26
  • 2015-01-12
  • 2019-06-11
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 2016-09-08
  • 2019-08-30
  • 1970-01-01
相关资源
最近更新 更多