使用 Appcelerator 在 mac 上获取 safari 和 chrome 打开的选项卡

发布于 2024-12-21 22:16:11 字数 471 浏览 3 评论 0原文

我需要读取 Safari、Chrome 和 Firefox 中打开的选项卡 URL(可选)并将它们转换为数组。 我正在使用 Titanium Appcelerator 开发桌面应用程序,它支持 Python。

也可以通过调用返回我正在寻找的内容的 AppleScript 来完成。 例如,这个简单的 AppleScript 显示了我正在寻找的内容

tell application "Safari"
    get URL of every tab of every window
end tell

现在我如何从 Python 或 JavaScript 调用它(我不知道 JavaScript 是否可以)?

有人有个好主意吗? 谢谢你!

I need to read opened tabs URL in Safari, Chrome and Firefox (optionally) and turn them into an array.
I'm using Titanium Appcelerator to develop a Dektop Application, and it support Python.

It can be also done by calling an AppleScript that return what I'm looking for.
For example this simple AppleScript show what I'm looking for

tell application "Safari"
    get URL of every tab of every window
end tell

Now how I can call this from Python or JavaScript (I don't know if it's possible with JavaScript) ?

Someone have a great idea?
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

无人问我粥可暖 2024-12-28 22:16:11

有两种方法可以通过 python 执行此操作...

1.(我的偏好)安装 appscript:pip install appscript

import appscript
urls = appscript.app('Safari').windows.tabs.URL()

2.Shell 到命令行并调用 osascript:

from subprocess import Popen, PIPE 
cmd = "/usr/bin/osascript -e 'tell application \"Safari\"' -e 'get URL of every tab of every window' -e 'end tell'"
pipe = Popen(cmd, shell=True, stdout=PIPE).stdout
urls = pipe.readlines()

There are two ways you can do this via python...

1.(My preference) Install appscript: pip install appscript

import appscript
urls = appscript.app('Safari').windows.tabs.URL()

2.Shell out to the commandline and call osascript:

from subprocess import Popen, PIPE 
cmd = "/usr/bin/osascript -e 'tell application \"Safari\"' -e 'get URL of every tab of every window' -e 'end tell'"
pipe = Popen(cmd, shell=True, stdout=PIPE).stdout
urls = pipe.readlines()
影子的影子 2024-12-28 22:16:11

你可以像这样将 AppleScript 嵌入到 python 脚本中,

import os
cmd = """osascript -e 'tell application \"Safari\" to get the URL of every tab of every window'"""
def url():
    os.system(cmd)
url()

我希望这会有所帮助!

You can just embed the AppleScript in a python script like so

import os
cmd = """osascript -e 'tell application \"Safari\" to get the URL of every tab of every window'"""
def url():
    os.system(cmd)
url()

I hope this helps!

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