If any window title of Safari contains x then

I'm using an if statement with
Any window title of:
Safari
Contains Authorize

If going to Safari and that 'authorize' tab is not the selected tab, But, there is a tab with the name (title) containing 'authorize' - should it not return a 'true' - it currently returns false.
If it is the selected tab then it returns true, but I would think it would return true if it existed but was not selected.

Maybe I'm misunderstanding the syntax.

@ccstone - I know you have a bunch of AppleScripts that deal with Safari tabs. I searched the forum and found a lot of examples, but they all appeared to do more than what the OP asked for, and I thought you might have an example more to the point.

Thanks!

I do have some of @ccstone’s great applescripts and could work that into the equation easily. I was more wondering if I misunderstood the language of the macro I was making.
Shouldn’t it return true if any tab in safari contains authorize.net?
btw I only use one window with safari, all tabs are opened in that single window. (just for reference)

Oh, cool. No, as I understand it, "Window" means a window object, from OS/X's point of view. So the tabs don't count.

You may already have this macro, but if not, get it. It shows what Windows are available to KM at the moment:

the window name is the tab name, I can get the window name for any tab (while selected)

I’m not sure if that post asked a question, or answered one. So if it was asking a question, I didn’t get it. :slight_smile:

I guess Safari is not able to ‘see’ the tabs that are not the currently selected tab.
I’ll work with the applescripts.
thanx for your time, appreciated

It's not that Safari can't see them. It's that the Safari window title changes to what tab is selected. KM only sees the actual window title, therefore it only sees the active tab's title.

For example:

Using the aforementioned Window Discovery Tool, this is what windows are available for Safari:

------------------------------
Safari
------------------------------
window →  Google 

So, the active tab is "Google", and that's what Safari's window title is.

Now if I switch to the Amazon tab:

...we get this:


-------------------------------------------------------------------------------------------
Safari
-------------------------------------------------------------------------------------------
window →  1 
window →  Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more 

(I have no idea what window "1" is.)


So when KM looks for window titles, it looks at the actual window title, not the tab title. It just so happens that Safari changes its window title to match the active tab's title.

Hope that makes sense.

you’re the best, I see you on this forum helping everyone, including me with the greatest of patience and kindness, thank you for that. I Pray to be more patient and kind, -
OK, great, I’ve now put ccstone’s? great AppleScript at the top of the macro and if that tab exists it goes to it, now the fun if then loops to see if we’re logged in or not.
Good stuff,
Cheers.
Troy

Thanks! That's really nice to hear. I truly enjoy helping on this forum, and I guarantee that I learn as much as I help. :slight_smile:

I Pray to be more patient and kind

Ha! If you only knew... I get impatient with people all the time. I've just learned to let some of the others on this forum deal with those situations. Patience is NOT my middle name. Ask my wife! :open_mouth:

Good luck.

1 Like

Hey Troy,

Nyet – a window and a tab are two different critters. Tabs are elements of document windows and cannot be seen by Keyboard Maestro without using AppleScript.

The current tab can be confused for the front document window, but while the two share similar characteristics they are not the same.

Here's a relatively bombproof way to work with tabs in the front document window:

--------------------------------------------------------------------------------
# Stupidly – Safari-scripting has no concept of “front window” if the app is hidden.
# So we have to make sure Safari isn't hidden before trying to manage the front window.
# This is NOT an issue if Safari is already active when you run your AppleScripts.
--------------------------------------------------------------------------------
tell application "System Events"
   tell application process "Safari"
      if not visible then set visible to true
   end tell
end tell
--------------------------------------------------------------------------------
tell application "Safari"
   if exists of front window then
      
      # Make sure the front window is a document window and not prefs or something else.
      tell front window
         try
            set winCurrentTab to current tab
         on error
            error "Safari's front window is not a document!"
         end try
         
         set tabList to its tabs
         set tabNameList to name of its tabs
         
         if tabNameList contains "Apple" then -- name must be verbatim
            return true
         end if
         
      end tell
   end if
end tell
--------------------------------------------------------------------------------

Here's some quick-and-dirty code for working with a partial name of a tab in the front document window:

--------------------------------------------------------------------------------
set partialTabName to "apple"

tell application "Safari"
   tell front window
      repeat with theTab in (get tabs)
         if name of theTab contains partialTabName then
            set current tab to theTab
            exit repeat
         end if
      end repeat
   end tell
end tell
--------------------------------------------------------------------------------

It's an invisible window:

If you run this you'll find it shows more windows than are open in Safari.

tell application "Safari"
   windows
end tell

That's very irritating from a scripter's perspective, because some of those windows are invisible and unavailable.

To work around this we use a filter-form to get only document windows:

tell application "Safari"
   set winList to windows where its document is not missing value
end tell

This is very clumsy. Apple should have given windows sub-classes, so you could say something like get every document window – but inexplicably they assiduously avoid such niceties.

-Chris

3 Likes