【问题标题】:AppleScript Hash Symbol (#) in URLURL 中的 AppleScript 哈希符号 (#)
【发布时间】:2022-01-16 11:40:35
【问题描述】:

我有这个 Automator AppleScript 来翻译文本。它有效,但只有一个词。如果我选择两个或更多单词进行翻译,它会将 URL 中的哈希 (#) 更改为 %23,如下所示

https://www.deepl.com/translator%23pt/en/

我得到一个 404 Not Found。

on run {input, parameters}
    open location "https://www.deepl.com/translator#pt/en/" & input
end run

【问题讨论】:

  • input 是一个list,需要展平为string/text 并正确URL 编码
  • @user3439894 太棒了!非常感谢!

标签: javascript macos applescript translation


【解决方案1】:

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

示例 AppleScript 代码

use framework "Foundation"
use scripting additions

on run {input, parameters}
    open location "https://www.deepl.com/translator#pt/en/" & encodeText(input as string)
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

【讨论】:

  • 太棒了!非常感谢!
猜你喜欢
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2017-01-16
  • 2020-06-05
  • 2012-03-31
  • 2014-02-22
  • 1970-01-01
相关资源
最近更新 更多