AppleScript:在传递给 Firefox 的 URL 中编码任意查询字符串值 [英] AppleScript: encoding arbitrary query-string values in URLs passed to Firefox

查看:18
本文介绍了AppleScript:在传递给 Firefox 的 URL 中编码任意查询字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PC 或 Mac 上使用 firefox 作为我唯一的浏览器很长时间了.简而言之,我的问题是:我想在 mac 上使用 automator 和使用 translate.google.com 进行即时翻译的 Applescript.什么最适合 Safari 或 Chrome(低于 4 或 5 行脚本)

I use firefox for a long time as my only browser on Pc or Mac. In a few words my problem: I want to create a service on mac with automator and Applescript for quite instant translation using translate.google.com. What works great with Safari or Chrome (below the 4 or 5 lines of script)

On run {input, parameters}
Tell application "Safari"
activate
try
Open location "https://translate.google.com/#auto/en/" & (the clipboard)
end try
end tell
end run

同样的事情(脚本)在 Firefox 上根本不起作用,我尝试了不同的方式规避不可能的问题

The same thing (script) does not work at all with Firefox, I try by different ways To circumvent the impossible problem

On run {input, parameters}
Set theProcess to "Firefox"
Set info to {}
Set y to 0
Set x to 0
Set n to 0

Tell application "Applications / Firefox.app"
activate
Open location "http://translate.google.com/#auto/en/"
end tell
Tell application "System events"
Repeat with theProcess in (process "Firefox")
try
Set info to info & (value of (first attribute whose name is "AXWindows") of theProcess)
end try
end repeats
Set n to count of info
info
end tell
Tell application "System Events" to tell process "Firefox"
Set x to "1"
Set frontmost to true
try
Click menu item "Paste" of menu "Edit" of menu bar 1
end try
Repeat while x is "1" -
If x is "1" then
Keystroke "V" using command down
Set x to "0"

end if
end repeat
end tell
end run

通过复制和粘贴,操作会在页面完全加载之前发生,甚至会减慢复制和粘贴过程.经过多次观察,剪贴板中包含的文本格式与URL的关联存在问题,我对此进行了改进,但尚不完善.

By copying and pasting, the actions take place before the complete loading of the page, even by slowing down the Copy and Paste procedure. After many observations there is a problem of formatting the text contained in the clipboard with the association of the URL, I improved this but it is not perfect yet.

tell application "Applications/Firefox.app" to activate
tell application "System Events" to tell process "Firefox"
    set frontmost to true
    set sentence to text of (the clipboard)
    set thesentences to paragraphs of sentence
    set thenewsentences to thesentences as string
    set the clipboard to thenewsentences
    keystroke "t" using command down
    keystroke "https://translate.google.com/#auto/fr/" & (the clipboard) & return
end tell

无论如何,如果它在不做任何修改的情况下与 Safari 一起使用,那么问题出在 Firefox 入口处,所以如果您能看一下这个问题,那将对我们所有人都非常有用.感谢您的关注.感谢您的回答.

推荐答案

Safari 和 Chrome 执行 为您在 URL 中对保留字符进行必要的编码,但 Firefox 没有.

Safari and Chrome perform the necessary encoding of reserved characters in the URL for you, but Firefox doesn't.

因此,您需要对查询字符串值(要嵌入 URL 的文本)进行显式编码.

Therefore, you need to perform the encoding of the query-string value (the text to embed in the URL) explicitly.

最简单(虽然不明显)的方法是使用 perl,通过 shell 命令,感激地改编自 这里:

The easiest (though not obvious) approach is to use perl, via a shell command, gratefully adapted from here:

# Example input that contains chars. that have special meaning in a URL ('&' and '?')
set the clipboard to "Non, Je ne regrette rien & rien ne va plus?"

# Get text from the clipboard and URL-encode it.
set encodedText to do shell script ¬
  "perl -MURI::Escape -lne 'print uri_escape($_)' <<<" & quoted form of (the clipboard)

# Now it's safe to append the encoded text to the URL template.
tell application "Firefox"
    activate
    open location "https://translate.google.com/#auto/en/" & encodedText
end tell

上述方法适用于提到的所有三种浏览器:Firefox、Safari 和 Google Chrome.

The above approach works with all three browsers mentioned: Firefox, Safari, and Google Chrome.

注意:

从(至少)Firefox v50 开始,Firefox 默认在当前前窗口的新标签中打开 URL.

As of (at least) Firefox v50, Firefox opens the URL in in a new tab in the current front window by default.

您可以让 Firefox 在新的窗口中打开 URL,方法是取消选中 General 上的 Open new windows in a new tabFirefox 首选项选项卡.

You can make Firefox open the URL in a new window instead, by unchecking Open new windows in a new tab instead on the General tab of Firefox's preferences.

但是,请注意,这是一个永久性设置,会影响从 Firefox 外部打开的所有 URL.

Note, however, that this is a persistent setting that affects all URLs opened from outside of Firefox.

有关在不依赖于更改设置的新窗口中打开的临时解决方案,请参阅此答案我的.

For an ad-hoc solution for opening in a new window that doesn't rely on changing the setting, see this answer of mine.

这篇关于AppleScript:在传递给 Firefox 的 URL 中编码任意查询字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆