Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 29, 2008, 08:02 AM   #1
jamsaver
macrumors newbie
 
Join Date: Nov 2008
Apple script & safari tabs

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
jamsaver is offline   0 Reply With Quote
Old Nov 29, 2008, 09:58 PM   #2
hiddenpremise
macrumors regular
 
Join Date: Jul 2008
Location: 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
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.
__________________
MBP i7 | iPhone 4 | iPad
hiddenpremise is offline   0 Reply With Quote
Old Dec 1, 2008, 04:51 AM   #3
hhas
macrumors member
 
Join Date: Oct 2007
Quote:
Originally Posted by jamsaver View Post
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:

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.)
__________________
Learn AppleScript, 3rd edition, Sanderson & Rosenthal:
http://apress.com/book/view/9781430223610
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net
hhas is offline   0 Reply With Quote
Old Dec 3, 2008, 04:21 PM   #4
jamsaver
Thread Starter
macrumors newbie
 
Join Date: Nov 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
jamsaver is offline   0 Reply With Quote
Old Dec 6, 2008, 04:44 AM   #5
jamsaver
Thread Starter
macrumors newbie
 
Join Date: Nov 2008
Hello,
The appscrpt lib was missing on my system ,
I just installed appscrpt lib and now it works !!
thank you for this great script ...
jamsaver is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Lion. Safari not quitting, and closing tabs. donfishinghocke Mac OS X 10.7 Lion 7 Jun 27, 2012 02:55 AM
apple script for safari Dr Kevorkian94 Mac Programming 2 Aug 23, 2011 07:21 PM
Safari 5.1 Tab Behaviour Jas0n Mac Applications and Mac App Store 13 Jul 27, 2011 05:35 AM
Safari tab issue, safari crashing and macbook heating up - help Pipsicle10 Mac Applications and Mac App Store 1 Nov 4, 2010 05:45 PM
Apple Script & Text i.shaun Mac Programming 0 May 11, 2008 04:02 PM


All times are GMT -5. The time now is 06:00 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC