使用 Applescript 通过上下文菜单在 Firefox 中打开链接
我刚开始使用 Applescript 在 Snow Leopard 中创建服务。我发现自己经常尝试使用 Firefox 在 Safari 中打开链接。我知道有多种方法可以使用 FF 打开页面 url,但我想使用 FF 打开页面内的任何链接。我认为使用 Applescript 创建服务可能是一个好主意,到目前为止我发现了这一点:
openFirefoxURL("http://www.apple.com/")
on openFirefoxURL(x)
return do shell script "open -a Firefox" & space & quoted form of x
end openFirefoxURL
这很好地在 FF 中创建了一个新选项卡。有什么建议可以调整它以打开页面上的链接吗?
更新:我通过反复试验找到了一个解决方案:
tell application "Safari"
set myURL to (do JavaScript "(getSelection().anchorNode.parentNode.href)" in document 1)
end tell
do shell script "open -a Firefox" & space & myURL
现在脚本满足了我的目的,我不知道是否有更好的方法来做到这一点,例如通过Applescript而不是javascript获取选择。如果您有更好的解决方案,请告诉我。谢谢!
I'm new to use Applescript to create services in Snow Leopard. I found myself often trying to open a link in Safari with Firefox. I know there are ways to open a page url with FF but I want to open any link inside a page with FF. I think using Applescript to create a service might be a good idea and so far I found this:
openFirefoxURL("http://www.apple.com/")
on openFirefoxURL(x)
return do shell script "open -a Firefox" & space & quoted form of x
end openFirefoxURL
This creates a new tab in FF nicely. Any advice to adapt it to open a link on a page?
UPDATE: I found a solution through trial and error:
tell application "Safari"
set myURL to (do JavaScript "(getSelection().anchorNode.parentNode.href)" in document 1)
end tell
do shell script "open -a Firefox" & space & myURL
Now that the script serves my purpose, I don't know if there are better ways to do it, e.g. getting the selection via Applescript rather than javascript. Let me know if you have better solutions. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在网上搜索了一段时间没有找到合适的解决方案后刚刚写了另一种方法。
它是 AppleScript 中的一项服务,可以从任何其他应用程序 URL 字段打开 Firefox 中的任何 URL。例如,只需选择 Chrome URL 中的文本,然后从列表中选择服务。
要创建服务:
选择“输入是”[仅限 URL]
然后添加操作“运行 Shell 脚本”,将其拖至脚本序列。
该脚本检查是否添加了“http”前缀,因为 Chrome 默认情况下不会传递它,并使用以下命令打开 Firefox OSX“打开”命令。
将此服务命名为“在 Firefox 中打开 URL”,然后就完成了。右键单击任何 URL 字段选择后,该服务将在服务列表菜单中可用。
完毕。
该服务保存在 ~/Library/Services 中。该目录在 Lion 中默认是隐藏的,要查看它只需在终端发出以下命令:
There is another approach I just wrote after searching the web for a while an not finding a suitable solution.
It is a service in AppleScript that will open any URL in Firefox from any other application URL field. Just select the text in the URL of Chrome, for instance, and choose the service from the list.
To create the service:
Choose "input is" [only URLs]
Then add the action "Run Shell Script" dragging it to the script sequence.
The script checks if the "http" prefix is added, as Chrome does not pass it by default, and opens Firefox with the OSX 'open' command.
Save this Service with the name "Open URL in Firefox" and you are done. The service will be available at the services list menu after a right click over any URL field selection.
Done.
The service is saved at ~/Library/Services. This directory is hidden by default in Lion, to see it just issue this command at terminal:
您不需要特殊的程序来执行此操作。 Safari 已经做到了。您必须首先启用 Safari 中的“开发”菜单。转到 Safari 的首选项,转到高级部分,然后选中底部的“在菜单栏中显示开发菜单”框。现在您已经有了“开发”菜单,您可以通过转到“开发 -->”在另一个浏览器中打开 Safari 中的任何页面。打开页面菜单。
这样做将在 Firefox 中打开当前页面,然后只需在 Firefox 中单击您想要的任何链接。
You don't need a special program to do this. Safari already does it. You have to first enable the "Develop" menu in Safari. Go to Safari's preferences, go to the advanced section, and check the box at the bottom called "Show Develop menu in menu bar". Now that you have the Develop menu, you can open any page from Safari in another browser by going to Develop --> Open Page With menu.
So doing that will open the current page in Firefox, then just click on whatever link you want in Firefox.
我不确定我是否完全理解你想要做什么,但如果你想使用 Firefox 作为默认浏览器,那么你不需要 Applescript。只需进入 Safari,打开“首选项”,然后在第一个选项卡顶部选择 Firefox 作为默认浏览器。
I'm not sure if I entirely understand what you are trying to do, but if you wish to use Firefox as your default browser, then you don't need Applescript. Just go into Safari, open Preferences, then select Firefox as the default browser at the top of the first tab.
由于 Safari 的 AppleScript 字典不提供任何访问 DOM 的方法,因此 JavaScript 解决方案是一个非常好的解决方案。如果即使所选文本中没有链接也有可用的服务(“在 Firefox 中显示 URL”)让您烦恼,我建议 Choosy,一个由 George Brocklehurst 开发的辅助应用程序/首选项窗格,它将为您提供浏览器的用户提示(以及更多,根据基于模式的选择规则)。有配套的 Safari 扩展,它提供了您正在寻找的上下文菜单选项。
As Safari’s AppleScript dictionary does not provide any way to access the DOM, the JavaScript solution is a pretty good one. If it annoys you to have a service (“Show URL in Firefox”) available even when there is no link in the selected text, I’d recommend Choosy, a helper application / Preference pane by George Brocklehurst which will give you a user prompt for browsers (and much more, up to pattern based selection rules). There is a companion Safari extension which provides the contextual menu option you are looking for.