MACRO or PLUGIN: Open or Refresh a URL in a Tab (Safari & Chrome)

sub_macro
plugin
safari
jxa
chrome

#1

#MACRO or PLUGIN: Open or Refresh a URL in a Tab (Safari & Chrome)

REQUIRES: OS/X Yosemite or later.

UPDATE: Version 1.1.

  1. Now treats "http" and "https" the same, when trying to match URLs.
  2. Always select the active tab first, if it matches.
  3. Fix for issue with multiple browser windows open.

As an experiment in ease-of-use, I've provided a Plugin version, and a Macro version. It's your choice which to use. See the Installation Instructions below for more information.

MACRO - Open or Refresh a URL in a Tab (Sub-Macro).v1.1.kmmacros (36.2 KB)

PLUGIN - Open or Refresh a URL in a Tab (Sub-Macro).v1.1.zip (9.2 KB)

##PURPOSE:

Open a URL in a Safari or Chrome tab. If the URL is already open in a tab, then that tab is selected, and optionally, reloaded.

When determining if the URL us already open in a tab, you can choose to have the URL match exactly, or only the start of the URL has to match.

##USAGE:

###Plugin

##Macro

Execute this macro using "with parameter", and pass the parameters (separated by a pipe | character).


###NOTES:

  • If the browser isn't running, it will be launched.
  • If the browser does not have a window, a new window will be created.
  • When opening the URL, the browser will become the front application.
  • If multiple tabs match the URL, then the first tab encountered is the one that is used.

##Which Should You Use - Plugin or Macro?

Whichever you prefer.

  1. The Plugin is easier to use, since the parameters have individual editors.
  2. The Plugin can't be "synced" between computers - it has to be installed on each computer you wish to use it on.
  3. When installing an update, it's a teensy-bit more work to install the updated Plugin than it is to import the updated Macro.

###Which do I personally use?
I use the plugin, because it's easier to enter the parameters. And it's cooler.


##INSTALLATION INSTRUCTIONS

##MACRO:

Import the .kmmacros file in the link at the start of this post.

If you're installing an updated version, I recommend you delete the old macro first. Your existing macros will continue to function as before.

##PLUGIN:

###First-time install:

  1. Download the .zip file in the link at the start of this post.
  2. Drag the zip file and drop it on the Keyboard Maestro Editor icon in the Dock.

###Installing an Updated Plugin:

A) Remove Existing Plugin Folder:

  1. In the Keyboard Maestro Editor, click the menu Help->Open Preferences Folder.
  2. In the Finder window that opens, navigate to the folder "Keyboard Maestro Actions".
  3. Move the existing folder "Open or Refresh a URL in a Tab" to the trash.

B) Restart the Keyboard Maestro Engine:

  1. Back in the Editor, stop and start the Engine:
    File->Quit Engine
    File->Start Engine

C) Install the New Plugin, same as first-time install:

  1. Download the .zip file in the link at the start of this post.
  2. Drag the zip file and drop it on the Keyboard Maestro Editor icon in the Dock.


Browser tab tips—finding the tab you need
Bring a specified window of an app to the front (if there are more than one window)
Cycle Chrome windows depending on its Google account
#2

Oh wow, thank you for making and sharing this. I was just thinking of making something similar and this seems to be exactly what I needed.

However it doesn't work for me. If I understood correctly, it should open the url that I provide, and if url is already opened in one of the tabs, it would go to it?

Well, this macro will just open new urls every time for me. Here is the macro I am testing with :

I've left your macro untouched and followed installation instructions. Only thing I did is lowercased the macro title but that shouldn't matter I think.

Thank you for any help.


#3

It’s because the URL actually gets changed to “https” (note the “s”). So change the URL to start with https://, and that should work.

I’ll work on a fix so you can provide either. I should have thought of that…

Thanks for the report!


#4

Thank you, this works flawlessly.

If I may make a feature request. It would be amazing if you could extend this so that if there are multiple instances of one website that falls under the parameters, it would cycle through the options. So for example let's say I have this :

I very often have many github sites open and using this macro, it would switch to one tab of all of them. It would be amazing, if you could cycle through all tabs that fall under 'github', if there are multiple tabs available with that site. Thus make this macro even more awesome than it is and only adding functionality to it.

Thank you for sharing this once again. :yellow_heart:


#5

Thanks!

I think if you put all the github tabs in a separate window, you can use ^Tab to cycle through them. So I suppose you could have a macro that calls my macro, then issues a ^Tab.

Hmm. So it would be nice if the macro returned something to tell you if it the tab was already active, so you know whether to navigate to the next tab or not.

What do you think about that?


#6

⌃ + Tab cycles through all tabs going right. I want to cycle only github tabs or whatever website I specify as my parameter. And I only use one safari window. Never really had the need of separating my browsing activity into separate windows.


#7

…except for now, right? :wink: I mean, I’m with you - I only use one window. But this would be the ideal reason to use more than one.

So here’s how I’m thinking it would work:

You trigger my macro. It the target URL is in the active tab of the active window, it would set the variable “Tab Was Active” to 1. Otherwise, it would set it to 0.

So your macro would work like this:

Execute my macro.
if “Tab Was Active” = 1, issue ^Tab


#8

Yeah but then once again, ⌃ + tab would go right. I want for it to ideally go to the other site of same domain.

That is I have multiple github tabs scattered across my window, not going one after another. If I am in one of these tabs, and trigger your macro, since I am already on github tab, it would go to the next one, there, it would go to the next one, and so on, cycling tabs. I suppose it would be too difficult to implement this, but this would be the perfect scenario for me and I hope you can agree on the usefulness of it. :slight_smile:

Right now, the macro is great but it is not robust enough I think. This would really make the browsing experience heavenly if such thing can be implemented.

What do you think?


#9

I think I may have a better way to implement what you’re asking for, but let me think about it a little.

Right now I’m trying to track down a bug I just encountered, with multiple windows open (no worries for you :slight_smile:).


#10

Updated to version 1.1. See comments in the first post.


#11

Thank you Dan. I am curious, did you get any success with implementing of cycling windows of same domain? This would be really really handy I think.


#12

Nicely done! Thank you so much for doing this and making it available!


#13

Truth time. I could do it. But I don’t need it, and I don’t want to spend time on it.

Sorry! :blush:


#14

I understand. :cry:

Can you help guide me on how I can try to make this? It would be a complete life changer for me if I can make it.


#15

The only way I’d know how to do it would be in JXA, and I suspect walking you through that is too hard.

@ccstone - Do you happen to have something for this? See his post in this topic, post # 6.

Thanks.

If Chris doesn’t have anything, I’ll see what I can do.


#16

Thank you. :green_heart:


#17

@DanThomas: OMG that is some beautiful regex and javascript!!


#18

Hey Dan,

Not a thing.

<…Time passes…>

This is a bit simple-minded.

Given a domain filter it will move to the first tab whose URL contains that domain.

IF already on a tab containing a URL with that domain it will move serially from left-to-right through the tabs corresponding to the filter.

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/01/06 19:04
# dMod: 2017/01/07 12:50
# Appl: Safari
# Task: Serially switch between tabs having the same domain (or other filter text in the URL) in the front window.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Serially, @Switch, @Tabs, @Domain, @Filter, @Front, @Window
--------------------------------------------------------------------------------

set urlFilterText to "forum.keyboardmaestro.com"

tell application "Safari"
   tell front window
      set filteredTabIndexList to index of tabs where its URL contains urlFilterText
      set currentTabIndex to index of current tab
      
      if currentTabIndex is in filteredTabIndexList and currentTabIndex ≠ (last item of filteredTabIndexList) then
         set listItemOffset to getListItemOffset(filteredTabIndexList, currentTabIndex) of me
         set current tab to tab (item (listItemOffset + 1) of filteredTabIndexList)
      else
         set current tab to tab (item 1 of filteredTabIndexList)
      end if
      
   end tell
end tell

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on getListItemOffset(theList, listItemValue)
   if listItemValue is not in theList then error "getListItemOffset():  “listItemValue” is not in “theList”!"
   repeat with listItemNum from 1 to (length of theList)
      if (item listItemNum of theList) = listItemValue then
         return listItemNum
      end if
   end repeat
end getListItemOffset
--------------------------------------------------------------------------------

Nikita – while you’re welcome to make more requests, I will probably not spend more time on this problem.

-Chris


#19

Hey Nikita,

BTW – you may want to scope-out these Safari Extensions:

-Chris


#20

Thank you Chris, I will try and make sense of this and apply it. I get the idea of how you want to go on and achieve this but not fully sure how exactly this works. Will try to google around and hope I can get something working.