Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get safari and chrome opened tabs on mac with Appcelerator

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!

like image 755
stefano di luca Avatar asked Feb 22 '23 02:02

stefano di luca


1 Answers

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()
like image 109
jdi Avatar answered Feb 24 '23 16:02

jdi