Macro to track Chrome tabs by usage


I’m thinking through an idea and wanted to ask more experienced users if this would be something that KM could handle.

Basically I’m looking for a way to track open Google Chrome tabs in a file, sorted by most recently used.

The way I’ve thought of tackling this would be to:

  • Create a macro that tracks changes in Chrome’s Window Title
  • Everything time the title changes,
    • get the title, url, and index
    • open/create a file in either plist format or some other format that’s easy to manipulate
    • iterate through all tabs in the file and check if the title/url already exists
    • if it does, then move it to the front of the list
    • if it doesn’t then add it to the front of the list
  • save/close the file

Some questions I have are:

  • is KM quick/reliable enough to manage this over multiple windows and 100s of tabs?
  • is there any way to track the closure of a tab and remove it from the file?

Any existing macros to build off or help would be much appreciated :slight_smile:

First, I'd say that your concept is definitely doable, as I have done similar things in KM.

Answers to your questions:

[1] Yes. No problem.
[2] Yes, but why bother?


  1. Don't mix reporting the results with storing the data in real time
  • Keep the tab count in a KM variable that is updated in real time
  • Have a separate macro that processes that Variable, does the sorting, and stores as a file (if you like).
  • Use RegEx to find existing tab entries in the KM Variable, and update the count.
    • If not found, just add to the bottom of the list (do the sorting later).

I have a similar system that tracks the mouse position when the window changes, and stores the last mouse position to be used when returning to a window. Here's the data I store in a KM Variable:

Google Chrome,2663,791
Keyboard Maestro,3460,69
. . .

Here I am just storing the App Name, but I could just as well store the Window Title, like I do here:

Keyboard Maestro,ALL,3067,45,1028,1105
Microsoft Outlook,ALL,1147,23,893,1125
. . .

I use this RegEx to search for an existing App, and get the Last Mouse position:

And here is the RegEx that UPDATES the Mouse List:

###For Your Setup

I think your task is actually easier than the ones like it that I have previously done.

Macro Trigger: This should work:

When that happens, your macro would search a KM Variable (say, "DND__ChromeTabList") for the Window Name, or maybe the URL.

So, in your case you would want to store:

CNT,URL,WINDOW_NAME (which is Tab Name),INDEX?
1,,Macro to track Chrome tabs by usage - Questions & Suggestions - Keyboard Maestro Discourse,4
. . .

You would have a similar RegEx (I can help you later with that if you like) to search for and existing tab/window, and update its count using another RegEx. If not found, just add to the bottom of the list.

Does all that make sense? Please feel free to ask any follow-up questions.

BTW, be prepared for this to take a while to get fully developed and debugged. I've written to macros that use this pattern, and both of them took me days to write, and then weeks to fully debug/tweak. :wink:

Hi @JMichaelTX, thanks for the quick and detailed response!!!

Something I should’ve mentioned in my original post was that the reason why I need to store this to a file is because I’m looking to use this data in another application. Ideally what I’m aiming for is a file that contains all my current Chrome tabs ordered by when they were last used i.e. when they were last the frontmost, focused tab. Could be stored in any format csv, plist, json, etc…

Apologies if I’m being daft here but I’m not following what I would use the CNT for?

Lastly, it feels like I need some level of scripting to handle the sorting and reading/writing to disk unless that’s something I can also do in KM?

That's fine. But why would you need to store the data in a file in real-time, while you are collecting the data? You can have a separate macro that writes to file at any time.

CNT is the number of times that tab has been activated.

Perhaps I misread your OP, thinking that you wanted this.
If not, no problem, just remove it from the list. That actually simplifies things.

Maybe, but probably not. KM has Actions to read/write files.
But nothing on sorting. A simple JXA script could do the sort for you, and write to file. The sort is a one line statement, and I have functions for read/write that you can use.

If this doesn't answer all your questions/issues, it would be best to provide your complete workflow, including the external apps you want to use. This will avoid waste of time on both our parts.

Hi @JMichaelTX, thanks again for the quick response.

I don't necessarily need to store the data in realtime although I'd imagine that updating the file on a frequent basis could work as well.

Sounds awesome! I've tried looking into AppleScript but for some reason my brain just doesn't grok the way that language works :tired_face:

Ok, the full workflow I'm looking to achieve is:

  1. Create/update a file that tracks all my currently open chrome tabs, sorted by most recently active as mentioned above
  2. Create a Launchbar action that when activated will read this file and display this list in the same order
  3. Selecting a tab would allow me to quickly jump to that tab from anywhere, similar to alt-tab for applications.

I've been thinking about something like this for a while and initially thought of going down the route of a chrome extension however it doesn't seem easy/possible to access data from a chrome extension outside of Chrome which led me to KM. I'm still a noob when it comes to KM and am continually impressed by the depth of this app.

Regarding the setup of the macro I'm ok having multiple macros to achieve this goal as long as the tab list file is kept up to date relatively frequently.

Any further assistance would be much appreciated, thx!

OK, this is a key point, that you want only OPEN tabs to be tracked.

So, let me restate to see if I understand.

You would like a list of currently OPEN Chrome tabs, sorted by order of last accessed, correct?

If that is the case, I don't see any reason/benefit to use LaunchBar (and I'm a big LB user). Seems like a KM Macro, which can be triggered from anywhere, would do the job just fine.

Turns out there is already an existing macro that does something similar:
Find and activate specific tab (Chrome and:or Safari)

What it does NOT do is track and sort the tabs by last accessed.

But maybe you could try out that macro to see how you like it.
If you do, then maybe we could just modify it to do the sorting.

Correct. I'm thinking it might even be easier to store the last viewed date along with the tab title, url and then simply replace that date with the current date when the title changes. This then leaves the need to either sort the list when saving to disk or sort the list by viewed date when reading the file.

The main reasons I want to make this into a LB action include:

  1. LB excels at showing these kinds of results
  2. It's easier to filter the results using LB
  3. I prefer LB's UI styling
  4. One less shortcut trigger to remember :slight_smile:

Another main reason I'm looking to go the file route is to improve the performance. It's not uncommon for me to have 100+ tabs open over several windows and using applescript to get all the open tabs can take a while.

Thanks for sharing this!! Looks like a really cool macro although as mentioned above I'd prefer to go the LB route.

I'm a big LB fan, but still learning how to use it. I don't understand the reasons you provided. Do you have an example?

IAC, I doubt I'll be of much help with the LB part of your workflow.
It seems to me that KM has much more powerful processing tools, like RegEx, than LB, and are easier to use/build.
Are you aware of the KM Trigger Macro by Name action (KM Wiki)? I use it all the time, and it works great!

Updating a file every time you change tabs will slow things down.
A KM Variable can easily handle data for 100+ tabs (and much more). So you would add/update the tab data when you change tabs/windows. Thus, no need to rebuild the entire list via AppleScript every time.

OK, that's great, if you know how to do that.
I'll back off for now. Let us know if you need any further help.

This is really cool, thx for the heads up!

I guess all I meant is that LB is designed to show a list of results and allow you to filter against those results as you type. [quote="JMichaelTX, post:8, topic:7119"]
Updating a file every time you change tabs will slow things down.A KM Variable can easily handle data for 100+ tabs (and much more). So you would add/update the tab data when you change tabs/windows. Thus, no need to rebuild the entire list via AppleScript every time.

Yea, I imagine as much. My thinking here was to try and save this info to disk on some type of schedule, i.e. every 10mins?

Would still love some help figuring out how to do the KM part of this :slight_smile: , irrespective of whether I end up/need the LB part.

ie Creating a macro that saves the current title, url, last viewed, and index to a KM variable every time the Chrome Title changes

  1. First check if the current title exists (using regex?)
  2. If it doesn't then add the info to KM variable
  3. If it does then replace the "last viewed" date with current time (using regex?)
  4. Sort the rows in this KM variable by "last viewed" desc

You mentioned that you had some regex to do #1-3 and had some ideas on #4?

Thanks again for all you help and time with this!!

OK, this macro should get you started.
It is NOT finished by any means, but does give you the foundation you need.

##example Results
The macro tracks your tab usage using the tab-delimited list shown in the below window.
Currently it is sorting the entire list after every change, and the displaying this window. The most recent tab is on top.
Clearly you would not do that in production, but this will show you that the macro is working properly.

###Special Macro Group Required
This is needed to prevent triggering of the macro in many cases where it is not needed. You could probably check for these conditions after the Macro was triggered, but I prefer to never trigger the macro unless it is actually needed.

##Macro Library   @Tabs Track Use of Chrome Tabs @Example

<img src="/uploads/default/original/2X/9/9fa034801953dfe7d1745afcb7087c7530a06e23.gif" width="70" height="17"> 2017-06-12 01:20 CT    **Ver 0.2**
  • Added Checks to Prevent Macro from Preventing Chrome to Quit
  • Added Count of Tabs/URLs to Report

<a class="attachment" href="/uploads/default/original/2X/c/c0adec8fef25560ffa508ed81cffba9caccf716e.kmmacros">@Tabs Track Use of Chrome Tabs @Example.kmmacros</a> (20 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**



** ⁉️: This macro/script is just an _Example_**

* It has had very limited testing.
* You need to test further before using in a production environment.
* It does not have extensive error checking/handling.
* It may not be complete.  It is provided as an example to show you one approach to solving a problem.


1. Open Chrome to New Tab/Window/URL
2. Macro will track Tab URL by Date
  * It uses the Chrome URL to identify the page/Tab
3. It will Display a list of Tab URLs in reverse chronilogical order every time the window title changes.
  *  This is just for demo purposes
  *  You will probably want to move the Sort and Display to another macro.
4. Modify as needed to complete.


* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  😉
* This macro requires this trigger:
  * the focused window title changes
* Move this macro to a Macro Group that is only Active when you need this Macro.
* ENABLE this Macro.
(all shown in the magenta color)

TAGS:  @Chrome @Tabs @Track @URL @Example


* Any Action in _magenta color_ is designed to be changed by end-user


* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions


1.  Keyboard Maestro Ver 7.3+ (don't even ask me about KM 6 support).
2.  El Capitan 10.11.6+
  * It make work with Yosemite, but I make no guarantees.


* While I have given this limited testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.


<img src="/uploads/default/original/2X/c/c25a64c23d5f24f967765682d5dd071fb50e2a6c.png" width="511" height="1870">

@s.newave, just wanted to let you know that the response is near instantaneous even with 100+ Tab URLs in the list, even when I am doing a sort:

Of course, this will depend on the power of your Mac. I'm running:
iMac 27-Inch Late 2015, MK482LL/A,Quad Core 3.3 i5, Retina 5K, 2TB Fusion Drive.

BTW, just to be clear, most often when I refer to "Tab" I mean the "URL" for the Chrome Tab at the time. This is what we are really tracking: URLs

Obviously, over time and between sessions, the same URL could appear in different Chrome Tabs.

Please let us know if this gives you the start you need.

Just updated Macro in the above post.