Refreshing all Safari Tabs

I was just trying to throw together a quick AppleScript to refresh all of the open tabs in a Safari window.

Here's my code, could someone direct me to a solution to what I thought would be an easy solution.

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

Try this:

tell application "Safari"
	repeat with eachTab in tabs of front window
		set URL of eachTab to URL of eachTab
	end repeat
end tell

I'm getting the same problem as with my code, it just doesn't seem to register the command with each tab.

The above is the equivalent of activating the address bar and hitting Return in each tab. If you open up a new window, open sites in two new tabs, scroll to the bottom of every page and then run the script you should see the loading bar in the active tab and then when you switch to the other tabs they should be back at the top of their page again (tested in Ventura/Safari 16.1).

If you're seeing something else -- what? If you're seeing that but expecting something else -- what?

This works for me on Mojave.

--------------------------------------------------------
# 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: None
# 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

--------------------------------------------------------
4 Likes

Hello, the script is exactly what I was looking for. Thank you very much for it.

But it didn't work in Firefox. What can I do about it?

Either:

a) Don't use Firefox -- it isn't AppleScriptable so there's lot's you can't do either directly or via KM compared to Safari and (some) Chromium-based browsers

or

b) Install the Firefox "Reload All Tabs" add-on which, I think, can be invoked in a macro by KM's "Press a Button" action.

1 Like

Thanks for the reply

You can also install chrome and put an autorefresh extension on it which offers bulk tab refresh option, it would be very easy.

Is there a way to run this script without bringing the browser to the front?

It worked for me without bringing the browser to the front.

Or even sometimes on other (than Safari) Webkit-based browsers. :slightly_smiling_face: The new discussions here made me wonder whether all tabs in a window could be easily reloaded in Orion. The answer turned out to be that it could be done very easily, since Orion has the menu item View > Reload all Tabs in Window, plus the corresponding hotkey -R.

I think I need to explain what I'm trying to do more clearly. I'm refreshing three different pages from three different browsers at 2-second intervals. The script above does exactly what I want without any problems. However, I'm running a different macro at the same time, using menu commands. The browsers come to the foreground, albeit briefly, and the menus change, which causes problems with my other macro. I think if I can refresh the browsers without bringing them to the foreground with a script or other command, it won't interfere with the other macro's operation.
I hope I explained it well.

If you use menu actions, then that will cause the frontmost app to change. If your other macros are affected by the change in menu items, then that's the fault of the macros which used the menu items, not the macro which used AppleScript to refresh the pages without affecting the windows.

You can refresh the browsers without bringing them to the foreground, so I'm not sure why you think this is causing any problems. If the menu changes are causing you problems, then that's the fault of the macros which are causing the menus to change.

Perhaps it would help a lot if you upload the macros that are using the menus, and also upload the macros that are getting problems because the menus are changing. You need to share with us the macros that are interfering with each other.

how can I do that?

Browsers reflesh.kmmacros (28.1 KB)

And how can I make the browsers run in the background while running this macro?

You start by using the code provided to you above by ccstone. That code worked for me. You have completely changed the code, (why did you change it?) and you are using your code on a bunch of different browsers that I don't have so I can't test for you. So your question is actually a completely different question that this thread is about. This thread is about Safari, and none of the browsers you want to control are Safari. Perhaps you should start a new thread, because the title of this thread will tell people this topic is for Safari only.

1 Like

You're right, sorry for that. As you said, I need to start experimenting with the code using the first detail. Thank you very much for your help.

1 Like