PDA

View Full Version : Apple script & safari tabs




jamsaver
Nov 29, 2008, 08:02 AM
Hello everyone,
I'm very new to applescript and I'm trying to write the url of every tab I've on safari and their duration (starts when I open the tab end when the tab is closed) to a txt file.
The problem is I don't even know how to do a "print" or "trace" to see if I'm receiving any values...
Can anyone help me out ?? a little script would be great ...

Thanks alot



hiddenpremise
Nov 29, 2008, 09:58 PM
In script editor, the last variable or value returned is listed after the script is run. If you need more than that, just do a
display dialog x
where x is the variable you want to see if you are recieving a value from

or you declare an array (called a list in applescript) and assign each variable to a key of that array and then call it at the very end of your script. doing so would list all values in the bottom half of the script editor window after the script has run.

hhas
Dec 1, 2008, 04:51 AM
Hello everyone,
I'm very new to applescript and I'm trying to write the url of every tab I've on safari and their duration (starts when I open the tab end when the tab is closed) to a txt file.

Safari doesn't provide any sort of notifications, so you'll need to poll it for a list of tab urls at periodic intervals. I would do something like this:

#!/usr/bin/python

from os.path import expanduser
from datetime import datetime
from time import sleep
from appscript import *

pth = expanduser('~/watched tabs.txt')

openurls = {}
while 1:
foundurls = set(openurls.keys())
for urls in app('safari').windows.tabs.URL():
for url in urls:
if url in openurls:
foundurls.remove(url)
elif url != k.missing_value:
openurls[url] = datetime.now()
f = open(pth, 'a')
for url in foundurls:
f.write('%s\t%s\n' % (datetime.now() - openurls.pop(url), url.encode('utf8')))
f.close()
sleep(1)

(Python has better data structures which makes the code a bit simpler than the AppleScript equivalent, but you get the idea.)

jamsaver
Dec 3, 2008, 04:21 PM
hey man,
thank you very much for this piece of code this looks great
but I've one more question
I never used python I dont even know how to compile it ...
if I make a .py file and than use the Build Applet thing in osx should this work?
is it really that simple python & mac :)

This made me think about starting to learn python ..

Thanks again

PS:I tried creating a app with Build Applet and saw that it's not working..
I wasnt counting on it anyways :)

jamsaver
Dec 6, 2008, 04:44 AM
Hello,
The appscrpt lib was missing on my system ,
I just installed appscrpt lib and now it works !!
thank you for this great script ...