I have a macro that takes me to my time tracking website multiple times per day but every time it runs it opens a new tab. Is there a way to return to the open tab?
Hey there, welcome to the community! You probably realize this already but it’s a great forum with lots of helpful people.
First things first, what browser do you use? There is a way to use AppleScript to find an open tab, even if it’s buried behind other tabs, and then bring it to the front, but it’s scripting varies depending on the browser.
EDIT: I just realized that you mentioned Firefox in the title. I’ll look through my AppleScript library tomorrow to see if I have anything that will work for you and get back to you if nobody else has responded yet.
Thanks a bunch!
I used to really overthink these types of tasks but I'm changing my behavior. I went down a similar rabbit hole years ago.
Here is what I would do/did, in Firefox. The website that you use for time tracking, make sure it is in the tab 1 position (first tab position) and insert this in the beginning of your macro.
If it's your second tab position, use cmd-2 etc...
Firefox may not have much AppleScript support, but it does have a slew of shortcuts.
Check here for the shortcuts.
I know this is simple and you may have expected a more complex solution, but sometimes the simple ones work as well.
Hey @Baruti, so unfortunately Firefox doesn’t really have any AppleScript support which means using that as a means to search for a tab is not really possible.
@kcwhat offered a valid alternate solution.
If you're not married to Firefox however, and were willing to use Safari or Google Chrome, they do have decent AppleScript support, and I have macros for both (using AppleScript) that can search for a specific tab and bring it to the foreground if you're interested. Let me know if you would like more info on that!
Indeed. I agree that KM has actions baked in for Safari and Chrome to make the above work more seamless. I know Firefox has different plugins and such so many people love it. Chrome drove me crazy with memory leaks, at one time, so I 86d it off of my machine. However, the Firefox shortcut way was the best and easiest avenue that I could muster for this situation. As usual, I try to indicate and prefab how I do things because I know I may not have the most efficient or the exact solution compared to others.
Thanks. It works. I'm going to also ask cdthomer for his Safari solution.
@kcwhat 's solution works but I would like to see your safari solution. I may switch default browsers if automation is easier with safari.
Safari is definitely better for KM automation. For the Firefox solution, which is what you initially asked for, be sure to click the solution check box so others that use Firefox will be able to identify this as a solution.
Clicked (I have to learn the protocol of the wiki).
I’ve been here 5 years and I’m still learning protocols.
When I ask for help, I’m normally at the edge of defeat so when I do ask and there is a solution provided, I give a like and send my gratitude. I believe that increases the chance of that someone helping me in the future. Even when I see good information or see a solution for an issue that I didn’t ask for, I still like the entry. This forum is a special gift and I don’t take these intelligent and kind folks for granted. I’ve seen people act entitled and feel they deserve a response to their issue. Then when they get it, you don’t hear from them again until the next episode. People give their time and expertise so it’s great to acknowledge them. However, these are not hard rules. This is simply the way I maneuver here. And personally, I give when I have a solution or workaround.
It’s a journey but a wonderful journey.
Here's an example macro to recall a specific Safari tab.
Technically you can set the search string from within AppleScript too, but for sharing I usually have things like that passed via KM variables so folks don't have to mess with the AppleScript itself. But if you want to put the search string right in the AppleScript, just remove (or comment out) the first block of script that mentions Requires KM 8.0.3+, uncomment the line starting with set tabSearch and put in the name of the tab where Keyboard Maestro is now.
Otherwise just put the tab name in the variable and call it a day.
Here's the AppleScript itself (click to expand/collapse)
---------------------------------------------------------- # Author: Chris Thomerson (@cdthomer) # # Current Version: 1.1 (Added explanatory notes and retrieving tab name vi KM variable) # Version History: 1.0 (Initial script) # # Created: Monday, September 28, 2020 # Modified: Saturday, February 5, 2022 # macOS: 10 (Catalina), 11.6.3 (Big Sur), 12 (Monterey) # Tags: # # DISCLAIMER # Permission to use, copy, modify, and/or distribute this # software for any purpose with or without fee is hereby granted. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL # THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ---------------------------------------------------------- ### Requires Keyboard Maestro 8.0.3+ ### set kmInst to system attribute "KMINSTANCE" tell application "Keyboard Maestro Engine" set tabSearch to getvariable "local__tabSearch" instance kmInst end tell # set tabSearch to "Keyboard Maestro" # for debugging purposes only tell application "Safari" repeat with w in windows --cycles through open windows if name of w is not "" then --verify that window is not an empty window like when there are no open windows repeat with t in tabs of w --cycles through tabs of each window if name of t contains tabSearch then --searches tab for a name match tell w to set current tab to t --activates that tab set index of w to 1 --sets that window to index 1 end if end repeat end if end repeat end tell
16)Search Safari for specified tab and bring that tab to the front.kmmacros (16 KB)