使用 Applescript 通过上下文菜单在 Firefox 中打开链接

发布于 2024-09-26 23:40:29 字数 760 浏览 7 评论 0原文

我刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为一人 2024-10-03 23:40:29

我在网上搜索了一段时间没有找到合适的解决方案后刚刚写了另一种方法。

它是 AppleScript 中的一项服务,可以从任何其他应用程序 URL 字段打开 Firefox 中的任何 URL。例如,只需选择 Chrome URL 中的文本,然后从列表中选择服务。

要创建服务:

  1. 在 Automator 主对话框中选择服务。
  2. 选择“服务接收选定的”[URL]
  3. 选择“在”[任何应用程序]
  4. 选择“输入是”[仅限 URL]

  5. 然后添加操作“运行 Shell 脚本”,将其拖至脚本序列。

  6. 选择“Shell”[/bin/bash] 选择
  7. “传递输入”[作为参数]
  8. 并粘贴以下脚本:
for f in "$@"
do
    echo "$f"
    if [ ${f:0:4} = "http" ]; then
        open -a Firefox "$f"
    else
        prefix="http://"
        prefix+=$f
        open -a Firefox "$prefix"
    fi
done

该脚本检查是否添加了“http”前缀,因为 Chrome 默认情况下不会传递它,并使用以下命令打开 Firefox OSX“打开”命令。

将此服务命名为“在 Firefox 中打开 URL”,然后就完成了。右键单击任何 URL 字段选择后,该服务将在服务列表菜单中可用。

完毕。

该服务保存在 ~/Library/Services 中。该目录在 Lion 中默认是隐藏的,要查看它只需在终端发出以下命令:

# chflags nohidden ~/Library/

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:

  1. Choose service in the main Automator dialog.
  2. Choose "service receives selected" [URLs]
  3. Choose "in" [any application]
  4. Choose "input is" [only URLs]

  5. Then add the action "Run Shell Script" dragging it to the script sequence.

  6. Choose "Shell" [/bin/bash]
  7. Choose "Pass input" [as arguments]
  8. And paste the following script:
for f in "$@"
do
    echo "$f"
    if [ ${f:0:4} = "http" ]; then
        open -a Firefox "$f"
    else
        prefix="http://"
        prefix+=$f
        open -a Firefox "$prefix"
    fi
done

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:

# chflags nohidden ~/Library/
一世旳自豪 2024-10-03 23:40:29

您不需要特殊的程序来执行此操作。 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.

晨与橙与城 2024-10-03 23:40:29

我不确定我是否完全理解你想要做什么,但如果你想使用 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.

短暂陪伴 2024-10-03 23:40:29

由于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文