###What is Best Method to Activate Google Chrome Tab for Existing URL?
All help and suggestions are welcome and appreciated.
When I get done with this script for Chrome, I need to do the same for Safari.
I have three main questions:
- How do I compose a whose statement that excludes “missing value” items?
- How do I get the tab index from the tab object?
- How do I get the window of the tab object?
**1. How do I compose a whose statement that excludes "missing value" items?**
These always give me trouble. I'm hoping it is something simple I'm missing.
```applescript
### THIS WORKS, but
-- returns an item of "missing value" for each window that does not have the URL
set tabFoundList to first tab of windows whose URL is urlToFind
### This Does NOT Work ###
-- ERROR: Google Chrome got an error: Can’t make missing value into type text.
-- set tabFound2 to first tab of windows whose ((URL is urlToFind) and (URL is not missing value))
### This Works, but does NOT exclude items with "missing value" ###
set tabFound2 to first tab of windows whose (URL is urlToFind) and (it is not missing value)
```
**2. How do I get the tab index from the tab object?**
The _tab_ object has an _id_ property, but NOT an _index_ property, and I don't see any way to get the _index_ other than looping the the tabs for the window.
<img src="/uploads/default/original/1X/1d60fb9c0a75ccf2c5cd0205249ec00508a74b90.png" width="425" height="111">
**3. How do I get the window of the tab object?
Also notice in the above screenshot that the SD Results panel shows "tab id 4 of window id 1".
How can I get that info into a variable? I need to know which window the tab belongs to.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
###Draft Script to Show/Create Chrome Tab with Given URL
Any/All suggestions for improvements are welcome and appreciated.
```applescript
(*
PURPOSE: Show/Create Tab with Given URL
* If Tab with URL exists, just Activate it
* If not, create new Tab with URL
*)
tell application "Google Chrome"
--- URL TO SHOW/CREATE ---
--set urlToFind to "https://forum.keyboardmaestro.com/"
set urlToFind to "http://macscripter.net/"
--- Not Needed for Script. Just for debug info ---
set tabList to tabs of windows
(*
### I can quickly determine if the URL is already open in a Tab, BUT ###
• It returns an item of "missing value" for each window that does not have the URL
• So I have to loop through the list to get the found tab
* It does NOT provide the Tab index, which is needed to set it as the active tab
• So I have to loop through all windows and all tabs to get the tab index
set tabFoundList to first tab of windows whose URL is urlToFind
*)
set winList to every window ## where its tab is not missing value
set urlFound to false
repeat with oWin in winList
set tabIndex to 0
### ? Surely there must be a better way to get the tab index ###
repeat with oTab in (tabs of oWin)
set tabIndex to tabIndex + 1
if ((URL of oTab) = urlToFind) then
set active tab index of oWin to tabIndex
set index of oWin to 1
set urlFound to true
exit repeat
end if
end repeat
end repeat
if (urlFound is false) then
--- OPEN URL IN NEW TAB ---
set oTab to make new tab at window 1 with properties {URL:urlToFind}
end if
end tell
```
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**EDIT: 2017-05-14 9:53 PM CT -- SUMMARY**
[quote="JMichaelTX, post:19, topic:600"]
Congratulations Ed! Your script is the fastest by 2X when run against my standard test from above (10 win with 20 total tabs):
@JMichaelTX: 0.39
@ccstone: 0.38
@estocky: 0.17
[/quote]
I encourage all who are interested in this to read the entire thread. There is a lot of good information/insights from the participants.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~