Apple script & safari tabs

Discussion in 'Mac Programming' started by jamsaver, Nov 29, 2008.

  1. jamsaver macrumors newbie

    Nov 29, 2008
    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
  2. hiddenpremise macrumors regular


    Jul 25, 2008
    Somewhere between my imagination and reality
    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.
  3. hhas macrumors regular

    Oct 15, 2007
    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:

    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:
                elif url != k.missing_value:
                    openurls[url] =
        f = open(pth, 'a')
        for url in foundurls:
            f.write('%s\t%s\n' % ( - openurls.pop(url), url.encode('utf8')))
    (Python has better data structures which makes the code a bit simpler than the AppleScript equivalent, but you get the idea.)
  4. jamsaver thread starter macrumors newbie

    Nov 29, 2008
    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 :)
  5. jamsaver thread starter macrumors newbie

    Nov 29, 2008
    The appscrpt lib was missing on my system ,
    I just installed appscrpt lib and now it works !!
    thank you for this great script ...

Share This Page