Get Url from all tabs

Hello,
I feel like this is something simple but I cannot write a correct script to do it.
How would I write a script that just gets the urls of all tabs in safari?
then if one exists, it selects that tab to be opened.

Mac Book Pro, Mac OS X (10.6.4)

Posted on Sep 13, 2010 8:19 PM

Reply
16 replies

Sep 14, 2010 4:55 PM in response to Camelot

I worded it badly. What I was trying to say was if a certain text is there then open that tab.
As an example you have a tab for youtube.com, google.com, and yahoo.com.

So when you run the script you are returned:
tell application "safari"
set url1 to URL of every tab of every window
end tell

{{"http://www.youtube.com","http://www.google.com", "yahoo.com"}}

Now i want a script that says

if url1 contains google.com
open tab "http://www.google.com (or simplified, set front window of safari to the tab that has the word youtube in it)
end if

* just to give more info, I have a script that checks safari for a url waits a second, then checks again and compares the 2 to do some stuff.
At the moment it just gets the front windows url

I would like to be able to get all the urls but just single ones with certain keywords and compare those 2, that way the front window does not have to be the window I get the url from.

I can further explain If you need me to

Message was edited by: filmmakerc14

Sep 15, 2010 1:33 PM in response to filmmakerc14

OK, that makes more sense…

My original code just gets the URLs of all tabs, but that's not sufficient for what you want here. The issue is that there's a disconnect between the URL and the tab it belongs to (which makes it hard to go back and activate the tab later).

All is not lost, though - the simplest fix is to iterate through all windows/tabs looking for the URL in question. If you find it, activate it, like:

tell application "WebKit"
repeat with theWindow in every window
set theTabs to theWindow's tabs --of every window
repeat with eachTab in theTabs
if URL of eachTab = "http://www.google.com/" then
set current tab of theWindow to eachTab
if index of theWindow is not 1 then
set index of theWindow to 1
end if
end if
end repeat
end repeat
end tell

Sep 15, 2010 3:26 PM in response to Camelot

Ok, I will try a few things later on and see how they work.
Would It be easier that instead of telling safari to open that tab if it exists, to set the beginning part of my script that gets info from safari to get info from all tabs, for a specific tab only?

Basically it looks like this:

repeat
tell application "Safari"
set fronturl to URL of front document
end tell
delay 1
tell application "Safari"
set fronturl2 to URL of front document
end tell

repeat while the fronturl2 does not contain fronturl

That way when the front url changes it notifies the person.
But could I do the search for a website like "http://www.youtube.com/watch?" and only get the info from that tab?

I can explain more or show more of my script as needed.

One more thing, I purchased a book http://apress.com/book/view/1430223618 which I am going to use to better help me make applescripts as well as cocoa-applescript applications in xcode. Do you think this book will be able to help, or is there another I should look at?

Sep 15, 2010 4:33 PM in response to filmmakerc14

Would It be easier that instead of telling safari to open that tab if it exists, to set the beginning part of my script that gets info from safari to get info from all tabs, for a specific tab only?


I don't understand your goals to be able to tell you what you want. Given what you've said in earlier posts it sounded like you wanted to see if a particular URL was present in any tab and if so activate it. That doesn't seem to be the case any more. You'll need to more clearly explain what you're trying to do in order to progress.

I purchased a book http://apress.com/book/view/1430223618 which I am going to use to better help me make applescripts as well as cocoa-applescript applications in xcode. Do you think this book will be able to help, or is there another I should look at?


That book, like most others, covers a lot of the theory behind AppleScript - the concepts behind AppleScripts/Apple Events, and things like handlers, process flow, etc.
It will not tell you how to do what you want, because what you want is something very specific - unique, even. What it might do is give you the fundamental building blocks to work out how to build your script to do what you want. For example, I've shown you some examples of how to find out what tabs are present, how to activate a tab, how to switch windows, etc. The book might do the same thing (albeit spread throughout several chapters). It's a matter of putting that together in a way that achieves your goal.

Sep 15, 2010 7:18 PM in response to Camelot

Sorry, I have confused myself on how to go about creating something. Basically I have a script that checks safari twice with a one second delay in between. Then it tests the two URL's returned and if they are not the same it gets info from the front document and displays it in a dialog box. However, this only works when the page you want to get the info from is the front, open page. I would like to be able to have it in any tab, and have an applescript filter through all the tabs, find the url with the most similar keyword, then return the info from just that tab.

Is this explained a little better? Sorry about the confusion.

Sep 16, 2010 8:04 PM in response to filmmakerc14

Let me see if I understand.


tell application "WebKit"
set oldTabs to get URL of every tab of every window
delay 1
repeat with currentWindow in every window
set allTabs to currentWindow's tabs
repeat with currentTab in allTabs
if oldTabs does not contain URL of currentTab then
--here is where you want the info of currentTab
--eg display dialog currentTab --though display dialog may stop/ruin your script
end if
end repeat
end repeat
end tell

Is that what you are looking for?

Sep 16, 2010 8:26 PM in response to taylor.henderson

I think it is what I am looking for. What exactly is this part about though:

repeat with currentWindow in every window
set allTabs to currentWindow's tabs
repeat with currentTab in allTabs

I see where it eventually goes but I am a little confused by the statement.

Thank you for the script ideas. However, I still need to incorporate a way to only focus on getting info from one tab, based on the link in the tab.

Such as:
set oldTabs to get URL of every tab of every window
then since you get a list of URLs, single out one with the text "http://www.youtube.com"
delay 1
set oldTabs2 to get URL of every tab of every window
then since you get a list of URLs, single out one with the text "http://www.youtube.com"

if oldtabs does not contain old tabs 2 then
then the rest of my script which gets information from that page containing "http://www.youtube.com" and displays it in a dialog box.

Sep 16, 2010 8:47 PM in response to filmmakerc14

filmmakerc14 wrote:
I think it is what I am looking for. What exactly is this part about though:

repeat with currentWindow in every window
set allTabs to currentWindow's tabs
repeat with currentTab in allTabs


Window appears to be a level above tab so you need to iterate through each window and then repeat for each tab in each window as well.

As for the rest of what you need I'm afraid I don't understand. Is youtube just an example or are you actually trying to constantly and actively monitor every website you go to every literal second and if you happen to open a youtube site display a dialog box containing the "site information?"?

Sep 17, 2010 4:12 AM in response to taylor.henderson

Ok, i will give a better example later. Ill try to simplify it.
my script checks the front window for a url then delays a second and checks again. If the url has changed it gets thname if the website and displays it in a dialog box.

I want to be able to do this, when the window that i want is not the front window. Basically it does the part above with the addition of me setting a keyword, then it scans through the tabs and the window with the keywords url is the only one taken down. It does this again after the delay. Then it compares just that windows ole url to the new url and if it is different, it gets the page properties for the page that it got the urls from.

Is this a little better? Thanks for your help

Sep 17, 2010 11:37 AM in response to filmmakerc14

I think that helps. This then?


on idle ()
if not oldURL then set oldURL to getURL()
if oldURL then set newURL to getURL()
if not newURL then set oldURL to null
if oldURL is not equal to newURL then
display dialog newURL
set oldURL to newURL
end if
return 1
end idle
on getURL()
tell application "WebKit"
repeat with currentWindow in every window
set allTabs to currentWindow's tabs
repeat with currentTab in allTabs
if URL of currentTab contains "keyword" then
return (URL of currentTab)
end if
end repeat
end repeat
end tell
return null
end getURL

Sep 17, 2010 11:48 AM in response to taylor.henderson

Everything appears to be correct. However, the display dialog needs to not display the url, but a property from a the tab where the url is being taken from. How would i incorporate this into the above script?
I believe its:
Tell application" safari"
set info1 to the properties of document 1 as text
End tell
Display dialog info1

But i need to have it get the info only from the page with the url being taken from.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Get Url from all tabs

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.