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

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

问题描述

我长时间使用Firefox作为PC或Mac上唯一的浏览器。
用几句话我的问题:我想用automator和
Applescript在mac上创建一个服务,以便使用translate.google.com进行即时翻译。
使用Safari或Chrome(在脚本的第4行或第5行以下)的效果很好



pre $ 运行{input,参数}
告诉应用程序Safari
激活
尝试
打开位置https://translate.google.com/#auto/en/& (剪贴板)
结束尝试
结束告诉
结束运行

同样的事情(脚本)根本无法使用Firefox,我尝试了不同的方式
为了规避不可能的问题

pre $ $ c
设置进程为Firefox
设置信息为{}
将y设置为0
将x设置为0
Set n到0

告诉应用程序Applications / Firefox.app
激活
打开位置http://translate.google.com/#auto/en/
end tell
告诉应用程序系统事件
在进程Firefox中重复处理
尝试
将信息设置为info& (进程的名字为AXWindows的第一个属性的值)
结束尝试
结束重复
将n设置为信息计数
info
end tell
告诉应用程序System Events告诉进程Firefox
将x设置为1
将最前面设置为true
尝试
单击菜单项粘贴菜单栏的编辑1
结束尝试
x为1时重复 -
如果x为1,则
使用命令向下击键V
将x设为0

如果
结束重复
结束tell
结束运行

通过复制和粘贴,即使通过减慢复制和粘贴过程,操作也会在完成加载页面之前进行。
经过很多观察,有一个格式化剪贴板中包含的文本与URL的关联的问题,我改进了这一点,但它还不完美。



<$告诉应用程序Applications / Firefox.app激活
告诉应用程序系统事件告诉进程Firefox
设置最前面为true
设置句子到文本(剪贴板)
将这些句子设置为句子的段落
设置新的句子为这些句子作为字符串
设置剪贴板为新语句
按键t使用命令向下
keystrokehttps://translate.google.com/#auto/fr/& (剪贴板)&返回
结束告诉

无论如何,问题出在Firefox入口,所以如果你能看看这个问题,那对我们大家都是非常有用的。
感谢您的关注。
感谢您的回答。

://en.wikipedia.org/wiki/Percent-encodingrel =nofollow noreferrer>保留字符必须编码给您,但是Firefox不会。



因此,您需要显式地执行查询字符串值(要嵌入到URL中的文本)的编码。



最简单(虽然不是很明显)的方法是使用 perl ,通过一个shell命令,感激地从 here $ b

 #Example包含字符的输入。在URL('&'和'?')中有特殊含义
将剪贴板设置为Non,Je ne regrette rien& rien ne va plus?

#从剪贴板获取文本并对其进行网址编码。
设置encodedText做shell脚本¬
perl -MURI :: Escape -lne'print uri_escape($ _)'<<< &安培; (剪贴板)的引用形式

#现在将编码文本附加到URL模板是安全的。
告诉应用程序Firefox
激活
打开位置https://translate.google.com/#auto/en/& encodedText
end tell

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


$ b 注意:

从(至少)Firefox v50开始,默认情况下,在当前的前窗口中新的标签



您可以让Firefox在新的窗口中打开URL em>取而代之,取消选中在Firefox的首选项的常规选项卡上的新选项卡中打开新窗口,而不是请注意,这是一个永久性设置,影响从Firefox外部打开的所有 URL。



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

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

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

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

Anyway if it works with Safari without modifying anything, the problem is at the Firefox entry, so if you can look at this problem, that would be very useful to us all. Thank you for your attention. Thank you for your answers.

解决方案

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

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

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

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

Note:

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

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.

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天全站免登陆