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

Oh hey Chris,

Let me try it again. Last time I remember trying it there was something off with it.

Hey Chris,

For it to work, do I replace his part of Dan's sub macro with your applescript?

Because when I do that, I get an error.

No, my script is stand-alone.

-Chris

1 Like

Oh my, this is exactly what I was looking for.

Except for one thing. It doesn’t initially open the web page if no web page of the url exists. With that it would be complete.

Another daily script I use from you. :slight_smile:

I am actually extremely curious what your workflow is like given just how much you know about automation. Never had a thought of writing a blog? :slight_smile:

I would certainly read it.

Hey Nikita,

Easily fixed – search the script for “new”.

-Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/01/06 19:04
# dMod: 2017/03/06 17:40
# Appl: Safari
# Task: Serially switch between tabs having the same domain (or other filter text in the URL) in the front window.
#     : 2017/03/06 – Now opens a new tab if the urlFilterText isn't found.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Serially, @Switch, @Tabs, @Domain, @Filter, @Front, @Window
# Vers: 2.0
------------------------------------------------------------------------------

set urlFilterText to "forum.keyboardmaestro.com"
set urlToFetch to "https://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 filteredTabIndexList = {} then -- New
         
         set tabURL to URL of current tab
         
         try

            tabURL -- will be undefined if the page is blank and throw an error

            # If the above line does NOT error:
            make new tab at end of tabs with properties {URL:urlToFetch}
            set current tab to last tab

         on error

            # Tab is blank, so we'll just use it.
            set its current tab's URL to urlToFetch

         end try
         
         return
         
      end if
      
      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
------------------------------------------------------------------------------
2 Likes

This would save me so much time. Thank you a lot Chris. How about that blog? :slight_smile:

Yes. I just need the time and oomph.

-Chris

1 Like

Isn’t that what we’re all doing here? Saving time. :slight_smile:

Starting getting this error every time I try call the macro :

Is the macro :

Never got it before and not sure what to do.

Thank you for any help.

I have the same error on Safari 10.1
Its still working ok on Safari 10.0.3

Has anyone solved this “Can’t convert types” error? I’d really miss this Plugin, it worked great when it worked.

Also, is it limited to 10 or does it continue with the newer versions of Safari.

Thanks

There is another pretty easy way to reload Safari tabs

You just need to specify a tab position 1,2,3...

The script will refresh a specified tab in the background

Hey Kirill,

You can also do something like this.

This script reloads every tab in the front Safari window, but it can easily be adapted to work with a specific tab in a specific window.

-Chris

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2011/08/09 20:33
# dMod: 2015/11/14 16:46
# Appl: Safari
# Task: Reload Every Tab in front window.
# Libs: ELb
# Osax: None
# Tags: @Applescript, @Safari, @Reload, @Tabs, @Front, @Window
----------------------------------------------------------------

tell application "Safari"
   
   set tabList to tabs of front window
   
   repeat with theTab in tabList
      tell theTab to do JavaScript "location.reload(true)"
   end repeat
   
end tell

----------------------------------------------------------------
1 Like

Agreed. Still works for Chrome, but not safari. And I'm on 12.0. A huge bummer, as I loved this macro!

Great plugin/macro! Unfortunately, I'm having the same problem with this, a simpler version I tried to make for myself, as well as @JMichaelTX 's from here.

In all of these, the correct tab is selected but the chrome window is not set to frontmost. In other words, if I have only one chrome window open everything works perfectly but if I have 2 chrome windows open and the opposite window is currently focused, "AXRaise" is not working at all to switch to the window with the desired tab.

Mac version: 10.14
Chrome version: 72.0.3626.121
KM Version: 8.2.4

EDIT: taking a closer look the following Applescript works fine for me with "Safari", but not at all with "Google Chrome"

tell application "System Events"
	tell application process "Google Chrome"
		tell (window 2) to perform action "AXRaise"
	end tell
end tell

Re the "can't convert types" error that stops this working with safari: I think I have fixed it, but I don't really understand the fix :slight_smile:
Replacing the line:
var result = _app.windows.whose(_not: [{document: null}]});
with:
var result = _app.windows
removes the error. It may have been required on an older version of safari? I've tested it with multiple safari windows, blank tabs, etc, and it seems robust.

I've attached my patched version here.

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

2 Likes

THANK YOU FOR THIS! So great for bringing my CRM web app to the front.

Hey! Thanks for this awesome script! I use Microsoft Edge. How do I add it in the macro?

I tried to Delete the Macro from the Kayboard Maestro and now I'm getting this error. Can anyone please help?image