Apple script & safari tabs

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

  1. macrumors newbie

    Joined:
    Nov 29, 2008
    #1
    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. macrumors regular

    hiddenpremise

    Joined:
    Jul 25, 2008
    Location:
    Somewhere between my imagination and reality
    #2
    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
    Code:
    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. macrumors regular

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

    Code:
    #!/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.)
     
  4. thread starter macrumors newbie

    Joined:
    Nov 29, 2008
    #4
    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. thread starter macrumors newbie

    Joined:
    Nov 29, 2008
    #5
    Hello,
    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