If Tab exists go to Tab, if NOT then make Tab

Hi!

I´m trying to find a condition that can check if for example KBM forum is open in a tab on Google Chrome.

If it is open, go to tab
If it is not open, make a new tab and open forum

Anyone who knows about a way of setting that condition up in a If then Else?

-Isak

@isak -

Would the "window title" condition work for that?

(Using Firefox here, so I don't know about Chrome.)

Hey Isak,

See this thread:

How to open Chrome mail without to restart the program

It should be fairly easy to adapt my scripts to work with the Keyboard Maestro forum instead of Gmail.

See in particular the AppleScripts I’ve posted.

Let me know if you need help adapting them.

-Chris

It works almost perfectly for my need! Thanks Chris!

Only thing is would like it to make a new tab instead of a new window in the else action.

This is one of my first scripts I have used, and I tried to rewrite it but it didn´t seem to work.

tell application "Google Chrome"
set newWin to make new window <— Changing new window to new tab didn´t work
tell newWin to set URL of active tab to "https://fronter.com/oa/main.phtml"
end tell

Hey Isak,

Easily done when you know how.

You can compare the original with this new script and see the change was pretty minor.

TextWrangler (freeware) can do direct file-compares to make that task a bit easier.

Don't let that bother you too much – programming is not always very intuitive – particularly if you haven't studied the language.

-Chris


-----------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/05/14 12:30
# dMod: 2016/12/08 11:40
# Appl: Google Chrome, System Events
# Task: Bring Chrome Tab with Keyboard Maestro Forum to front if it exists - otherwise open KMF in new tab.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Google_Chrome, @System_Events, @Open, @Keyboard_Maestro, @KM, @Forum
# Test: macOS 10.11.6 with Google Chrome 54.0.2840.59
-----------------------------------------------------------------

set _win to false
tell application "Google Chrome"
   set {idList, urlList} to {id, URL} of every tab of every window
end tell

set AppleScript's text item delimiters to linefeed

if (urlList as text) contains "https://forum.keyboardmaestro.com" = true then
   set theWin to 1
   repeat with i in urlList
      set theTab to 1
      repeat with n in i
         if n starts with "https://forum.keyboardmaestro.com" then
            set {_win, _tab} to {theWin, theTab}
         end if
         set theTab to theTab + 1
      end repeat
      set theWin to theWin + 1
   end repeat
end if

if _win ≠ false then
   tell application "System Events"
      if quit delay ≠ 0 then set quit delay to 0
      tell process "Google Chrome"
         perform action "AXRaise" of window _win
      end tell
   end tell
   tell application "Google Chrome"
      tell front window to set active tab index to _tab
   end tell
else
   tell application "Google Chrome"
      tell front window
         make new tab at end of tabs with properties {URL:"https://forum.keyboardmaestro.com"}
      end tell
   end tell
end if

-----------------------------------------------------------------
1 Like

Well, yeah, but that's like the old Steve Martin joke:

"How do you get to be a millionaire? First, get a million dollars."

:slight_smile:

1 Like

Luckily for me I have guys like you who are always there to help out! So I feel optimistic about getting that “first million” :slight_smile:

Thanks alot Chris!

1 Like