Search Titles of Safari or Chrome Tabs in Multiple Windows and Bring Match to Front

Discourse question: what mechanism makes a link to a macro appear to the right like the two above?

Just post a link to another topic, and the forum software will automatically show it on the right column.

Quite impressive. Suggestion: Escape should close the dialog, like Cancel. When you have a long list of tabs it is tedious to drag the mouse all the way to the bottom in order to cancel.

The gray bar in the middle of the list should have a title inside it. I assume it separates Chrome tabs from Safari tabs*, but I don’t know which is on top and which is on bottom. This would become more important if…

can get a version of this that also works for Firefox? I realize their browser implementation is unusual, it might require an entirely different approach (in particular programming inside Firefox with their functions and web page structural i haven’t looked at your code closely enough to guess what kind of effort would be required to implement it in Firefox.

I have found a bizarre case, but I can’t give you much information about it. The same problem occurs with this script:


tell application "Safari"
    activate
    repeat with theWindow in (every window where its document is not missing value)
        log " " & the name of theWindow as item
        repeat with theTab in every tab of theWindow
            log "      " & the name of theTab
        end repeat
    end repeat
end tell

What happens is that there are two tabs —the same two — that get included for every window that are not in those windows. I do recognize them, and I am pretty sure they are in some window, but according to the output of my script and to Safari’s Window menu, they are not the front tab of any window.

Your script finds many copies of these tabs too. If I select one of the occurrences of these tabs I get to a window that (a) does not contain that tab and (b) even though it has 4 tabs, none of them are active — the window is empty.

I have quit and restarted Safari but the same problem was there. I don’t doubt that if I close all the windows, quit Safari, and reopen it the problem will have been solved, and I have never noticed this problem before, so I guess I can just let it pass, but might you have any idea what’s going on.

I don’t want to lose my windows and tabs – there are about 250 open. Now, I know this ridiculous, and in fact the script I showed above is the outline of one I am working on that saves all the window names and tab links to an HTML file. I am working on another script that reopens the windows and tabs. (As far as I know there is no session saver add-on for Safari, and I am not aware of any scripts that do this.) These scripts should not be difficult, but I have encountered a variety of interesting problems along the way, some of which I have posted to the applescript-users mailing list.

I'm curious. What is the use case that requires 250 windows/tabs open at once?

Hey Mitchell,

Sure there is

Session Restore on Apple's Safari Extension's site.

My preference by far: Sessions

Try out my script (in the post below) to visualize your windows and tabs:

I've written several scripts over the years to save and restore Safari sessions, but these days I just use the Sessions extension.

I was playing with one not too long ago to write them to an HTML file, but I can't find it right now.

-Chris

Hey Mitchell,

While I like brevity as much as the next guy, I generally find this type of construct to be counterproductive – because you can't look a the every-window list to visualize or debug.

This will get you everything you need:

tell application "Safari"
   set {tabNameList, tabURL} to {name, URL} of tabs of (windows where its document is not missing value)
end tell

Iterating through the text lists it creates is much faster than other methods (I've spent hours proving this).

Each list item represents the tabs of a window. so you can segregate by window if desired.

tabNameList

{
  {
    "Apple Mac OS X Software & Apps - Discover & Download : MacUpdate", 
    "MacScripter / Odd FileMaker result", 
    "MacScripter / Convert content of txt file back to a list of lists", 
    "MacScripter / copy source files to multiple new filenames in new folders from csv"
  }, 
  {
    "Search titles of Safari or Chrome tabs in multiple windows and bring match to front - general - Keyboard Maestro Discourse"
  }, 
  {
    "Forms and Form Fields :: Eloquent JavaScript"
  }, 
}, 

tabURL

{
  {
    "http://www.macupdate.com/", 
    "http://macscripter.net/viewtopic.php?id=45101", 
    "http://macscripter.net/viewtopic.php?id=45099", 
    "http://macscripter.net/viewtopic.php?id=45095"
  }, 
  {
    "https://forum.keyboardmaestro.com/t/search-titles-of-safari-or-chrome-tabs-in-multiple-windows-and-bring-match-to-front/2516/15"
  }, 
  {
    "http://eloquentjavascript.net/18_forms.html"
  }, 
}

-Chris

Thanks. I never even knew there were macros past the first page. In the past there were so few macros, and search never seemed to find anything, that I had given up on there being anyway to find extensions other than scrolling, and I didn't see any way to get past the first page (is there one?). But it was dumb of me to not to Google for it.

I like that formulation. The code I showed is not my real code, it was a minimization that revealed the problem. What yours doesn't do that led me into some contortions was that I thought I'd be clever and not only restore the tabs of each window but bring to the front the tab that had been at the front when I took the snapshot.

Looks good. Too bad it will be frozen at the end of this year.

Crud, I missed that.

Someone should take it over. It's by far the best session-manager available for Safari.

-Chris

Hey Mitchell,

If you look at the post I linked to that has my _Safari » Switch Tabs-Windows Using Pick-List macro – and actually try out the macro – you'll see it marks the front tab of each window.

-Chris

Hey Mitchell,

The first page has a Popular, Recent and Categories links.

So you can search or browse.

-Chris

Expanding upon my scriptlet above and excerpted from the referenced macro (with a slight embellishment):

tell application "Safari"
   tell (every window where its document is not missing value)
      set docCount to count
      set currentTabIndex to index of current tab
      set tabCount to count of tabs
      set tabNames to name of tabs
      set tabURLs to URL of tabs
   end tell
end tell

-Chris

Mostly just a haphazard impatient working style (as you probably could have guessed from all my too-hasty and misinformed posts :-).

But there are some underlying factors:

  • I work with a lot of different technologies and applications and often have multiple pages of documentation open for many of them at any given time
  • Working with all these technologies leads to a proliferation of searches for how to do things or resolve problems — I bet I have at least 20-30 stackoverflow tabs open at any given time
  • It often isn't enough to read answers to my questions or searches, I keep them around until I've read them and tried what they say enough to be done with them
  • Often I hit a topic in a search and open 5-10 results from the results page into tabs of the same window, and I don't read them (all) right away
  • To some extent I am using windows and tabs as a structured version of Safari's reading list, which I've never found a use for.
  • I also have very many bookmark folders, and subfolders, etc. and I often bookmark the kinds of pages I mentioned above, but there's no good way to filter out bookmarks according to whether I've read their pages, and in any case I might not have finished with them when I bookmarked them.
  • I use Evernote a little but find it too cumbersome to deal with this flood of bookmarked pages.
  • Here's an example: I encounter so much good stuff on the KM forum that often all I do is just click the topic that looks interesting and move on. Send up with typically 5 or so windows each with typically 5 or so tabs all open to KM forum pages

At the moment my Safari Windows and tabs are quite a bit more cleaned up than usual. In the hope I won't embarrass myself by anything included there, I put in my Dropbox the HML file generated for my current Safari state.

One of the things I often do to cut back on the documentation-related tabs is use Firefox for, and only for, technology documentation (unless I am developing or debugging something for which Firefox tools are helpful). I used to use OmniWeb for that, loving its workspace and vertical tab feature and highly Mac-Style interface (Omnigroup originating as a provider of applications for NeXT). I was very happy recently to discover a tree-structured vertical tab extension for Firefox (and many other vertical tab and tab-grouping extensions listed on that page).

Ill probably think of more to say about this, but I doubt there would be any point.

Working style, is the key, I think. In fact, the link cited above for the Firefox tree-tab extension says at one point "If you often use many many tabs…", so I know there is at least one person out there who shares this style :-).

Yeah, thats good. There are so many tab-managing macros floating around the KM forum that I haven’t kept up with all of them, no less read their implementations carefully. Best way to learn better ways to do things is to discover them in other people’s code, of course.

No, it's a matter of a change in Apple's extensions policy that the author objected to. I don't know where that might be on the extension's web page, but I read it in a popup that showed up when I first installed the extension.

I found the problem. It was hidden because it was the result of accidentally using a feature of Safari I never used — pinned tabs. I don’t even know how they work. I can’t imagine why a pinned tab would show up as one of the tabs of every window.

Because pinned tabs are added to every window.

Perfectly insane if you ask me...

-Chris

How'd you like to be able to create bookmarks in the Finder and auto-categorize and tag them?

-Chris