Recall a Safari Window That Does NOT Match or Contain a Specific Name

Howdy folks. I am attempting something that I thought would be fairly simple but it has me stumped so far.

I recently made big changes to the apps I use for my workflow and now am needing to be able to recall any Safari window that does NOT contain or match a specific name (Voice Portal).

I used to have a Safari tab for my email constantly open that contained my email address in the window name. To search the web or open other websites I used an AppleScript to recall that email window/tab and then create a new tab and go from there. This insured that all my web browsing was done in the window where my email tab sat, and NOT my Safari work window that must remain by itself all the time. My work tab contains the name "Voice Portal", and I would like to find out a way to recall ANY tab or window that does NOT contain that name, that way I can modify my existing macros to open new tabs OUTSIDE of that window/tab.

If anybody can point me in the right direction I'd greatly appreciate it. I hope everybody is staying safe out there!

Bump. Anybody have any ideas? :grin:

I had the reverse of this problem where I wanted to bring to the front any Safari tab that contained a specific text. I modified the AppleScript to reverse the logic and open any Safari tab that is not Duckduckgo.com. This macro runs an AppleScript that looks for any Safari tab name that does not contain the text "Duckduck" and brings that tab to the front. It there is no open Safari tabs, it open one to Google.

Change the variable nameTextToFind to whatever you want. The macro is saved in the disabled state.

Open to Safari Window that is not Duckduck Macro (v10.0)

Open to Safari Window that is not Duckduck.kmmacros (4.1 KB)

Hey Chris,

This is probably how I'd do it.

-ccs

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/11/14 12:11
# dMod: 2021/11/14 12:11 
# Appl: Safari
# Task: Return First Window Whose Tab Names to NOT Contain a Search String.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @First, @Window, @Tab, @Names, @NOT, @Contain, @Search, @String
--------------------------------------------------------

set searchText to "Gmail"

set AppleScript's text item delimiters to linefeed
tell application "Safari"
   repeat with theWindow in windows
      set winData to ({theWindow's id as text, name of theWindow's tabs as text} as text)
      if winData does not contain searchText then
         set targetWindow to window id (paragraph 1 of winData)
      end if
   end repeat
   
   return targetWindow
   
end tell

--------------------------------------------------------

Oh man I forgot all about this post!

@cyoungers and @ccstone thanks so much for yalls examples. I'll take a look more closely at them tomorrow!

-Chris

Thank you @ccstone. I barely know AppleScript but I cobbled together stuff from other scripts. I'm going to steal your much more elegant solution!

1 Like