當所選文本只有一個單詞時,代碼正常作業,但不止一個單詞。我認為單詞之間的空格會導致這個問題,但我不知道如何解決它。
on run {input, parameters}
set phrase to input as string
set phrase to quoted form of phrase
set ui_lang to "en"
set from_lang to "en"
set to_lang to "tr"
return "https://translate.google.com/?hl=" & ui_lang & "&sl=" & from_lang & "&tl=" & to_lang & "&text=" & phrase
結束運行
uj5u.com熱心網友回復:
on run {input, parameters}
set phrase to input as string
set ui_lang to "en"
set from_lang to "en"
set to_lang to "tr"
open location "https://translate.google.com/?hl=" & ui_lang & "&sl=" & from_lang & "&tl=" & to_lang & "&text=" & phrase
end run
uj5u.com熱心網友回復:
如果它是 URL 的一部分,那么使用quoted form將不起作用,因為它準備了用于 shell 的文本。
對于網址,特殊字符(包括空格)必須為url encoded. 空格被替換, 其他特殊字符有自己的 '%' 代碼。
解決此特定情況的一種快速而骯臟的方法涉及將空格替換為 ' ',將這些作為前三行:
set phrase to input as string -- no changes
set AppleScript's text item delimiters to {" "}
set phrase to words of phrase as text
基本上,它將您的字串分解成單詞并用 而不是空格重新連接它們。它也應該適用于單個單詞輸入。您應該在后續行中將 TID 改回任何值(或默認的 {""})。
例如:
"and empathetic programmers"變成"and empathetic programmers"變成"https://translate.google/?hl=en&sl=en&tl=tr&text=and empathetic programmers"
該站點上有許多討論該主題的問題,包括一些特定于 Applescript 的問題。您還可以在此處找到更多,包括一些處理程式:文本編碼 | 解碼和編碼和解碼文本
有很多關于這個主題的文章,所以很容易搜索。還有許多在線編碼器/解碼器,可用于測驗比較。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/334420.html
上一篇:如何在C 中強制重新定義
