Here I am just storing the App Name, but I could just as well store the Window Title, like I do here:
. . .
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,https://forum.keyboardmaestro.com/t/macro-to-track-chrome-tabs-by-usage/7119,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.
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.
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
Ok, the full workflow I’m looking to achieve is:
Create/update a file that tracks all my currently open chrome tabs, sorted by most recently active as mentioned above
Create a Launchbar action that when activated will read this file and display this list in the same order
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!
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:
LB excels at showing these kinds of results
It’s easier to filter the results using LB
I prefer LB’s UI styling
One less shortcut trigger to remember
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.
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 , 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
First check if the current title exists (using regex?)
If it doesn’t then add the info to KM variable
If it does then replace the “last viewed” date with current time (using regex?)
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.
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:@Tabs Track Use of Chrome Tabs @Example
~~~ VER: 0.2 2017-06-12 ~~~
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