Reload all chrome tabs of a certain page with AppleScript - applescript

I'm trying to reload all tabs of Chrome (i could have many windows open) of a certain page. This is what I did:
tell application "Google Chrome"
set {ids} to every window
repeat with id in ids
reload (tabs of window id whose URL contains "mypage.com")
end repeat
end tell
But I get Google Chrome got an error: Can’t make item 1 of window id 1 into type integer.
How do I do this?

The answer submitted by #wch1zpink won't work if there are any Chrome windows open that don't contain at least one tab with a mypage.com URL. Unfortunately, you need to iterate through the windows:
tell application "Google Chrome" to repeat with W in windows
reload (every tab in W whose URL contains "mypage.com")
end repeat

This works for me using the latest version of macOS Mojave and Chrome.
tell application "Google Chrome" to set allTabs to a reference to (tabs of windows whose URL contains "mypage.com")
tell application "Google Chrome" to reload allTabs

Related

AppleScript that reads active application name, title and URL

I need AppleScript that, when lunched from command line, will return three things:
current active application name; ex. "Google Chrome", "Safari",
"Steam", "iTunes", "Atom" ect.
current active application title, if there is one
if current active application is a browser, I want the current
active tab URL
examples:
Google Chrome; AppleScript: How to get URL of Safari tab which failed
to load? - Stack Overflow; https://stackoverflow.com/
iTunes; iTunes
iTerm2; 1. node test.js (sleep)
Safari, GitHub - rails/rails: Ruby on Rails; https://github.com/rails/rails
I know there are some similar questions w. answers here on stackoverflow, but I am not able to pice them together to make all of this work. Would appreciate help much.
I wanted to make a script that does exactly this running in the background so that I can better keep track of my time. Here's what I came up with:
tell application "System Events"
set frontmostProcess to first process where it is frontmost
set appName to name of frontmostProcess
end tell
tell application appName
set windowName to the name of the front window
end tell
if appName is equal to "Safari" then
tell application "Safari"
set theURL to the URL of the current tab of the front window
end tell
else if appName is equal to "Google Chrome" then
tell application "Google Chrome"
set theURL to the URL of the active tab of the front window
end tell
else
set theURL to ""
end if
return (appName, windowName, theURL)
Credit goes to https://apple.stackexchange.com/a/171738 for getting me on the right track.

Getting Focused Browser Tabs in OSX

I am trying to get active browser tab from browsers. I can do that for Chrome with this code:
tell application "Google Chrome"
set myURL to the URL of the active tab of the front window
end tell
For Safari, I am changing Chrome to Safari but it is not working at all
Is there anyone to help me about that?
This code finds Gmail tab among all windows and all tabs and makes focus to it.
set tabTitle to "Gmail"
tell application "Google Chrome"
activate
repeat with w in (windows)
set tabIndex to 0
repeat with t in (tabs of w)
set tabIndex to tabIndex + 1
if title of t contains tabTitle then
set (active tab index of w) to tabIndex
set index of w to 1
return
end if
end repeat
end repeat
end tell
For Safari it's:
tell application "Safari"
set myURL to the URL of the current tab of the front window
end tell
You also do not need to be so verbose, dropping all occurrences of the, e.g.:
set myURL to URL of current tab of front window
Does the same thing.
In Script Editor, look in the Library for Safari:
Script Editor > Window > Library > Safari
This is the answer i tried.. #user3439894

How to keep applescript running and redirecting urls?

I have this code I can keep running inside Script Editor. But it causes my laptops fans to scream! So I am not sure is this right way to do this?
repeat until application "Google Chrome" is not running
if application "Google Chrome" is running then
tell application "Google Chrome"
set (URL of every tab of every window where URL is equal to "facebook.com") to "twitter.com"
end tell
end if
end repeat
Other question is that why my code isn't working? I would like to use this for redirecting from spesific urls to another. But even this implementation doesn't work.. If I open facebook.com it doesn't redirect to twitter.com.
Any help is appreciated.
NOTE: YES, I want to use applescript for this so focus on it, pls :)
When an application appears to work, but doesn’t seem to be matching its conditions, it is always helpful to look at the actual values being checked. In this case, open the URL you think should be matched, and then get that URL to look at.
tell application "Google Chrome"
get URL of tab 1 of window 1
end tell
When I entered “Facebook.com” in a new window in Chrome, and then ran the above script, what I found was https://www.facebook.com/?_rdr=p.
When I replaced “Facebook.com” in your code sample with https://www.facebook.com/?_rdr=p, the script now had an affect; it redirected to Chrome’s about page.
Taking a cue from the necessity of using the full URL in the condition, I switched “twitter.com” to “http://twitter.com/”.
The script now performs as I understand you intended it to: every tab in every window that matches https://www.facebook.com/?_rdr=p gets redirected to twitter.com.
repeat until application "Google Chrome" is not running
if application "Google Chrome" is running then
tell application "Google Chrome"
set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
end tell
end if
end repeat
Applescript and Google Chrome are matching your conditional’s text against the full URL, so your conditional has to use the full URL as well.
#vadian’s note about using an idle handler is a very good one. See this Hourly Pop-up Alert question for ideas on using an idle handler. Something like this should work:
on idle
if application "Google Chrome" is running then
tell application "Google Chrome"
set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
end tell
end if
--number of seconds to wait for the next check
return 1
end idle
Save as application, stay open after run handler.

Google Chrome doesn't open URL with AppleScript

What I have set up is a AppleScript that asks you what website you want to open with a couple options, to make this simple, I only list one option, Google. This is the current code:
if the_button = "Google" then
site = "http://www.google.com"
tell application "Google Chrome"
launch
tell window 1
make new tab with properties {URL:site}
repeat while loading of active tab
delay 0.1
end repeat
end tell
activate
end tell
It opens a new tab in Google Chrome, but doesn't put anything in the URL bar or reload/load or anything. I've tried this: Script Safari 5.1 to open a tab But, nothing's working. I'm using Mac OS X Lion 10.7.4 and Google Chrome version 21.0.1180.79. Please help!
It works for me, but not when Chrome doesn't have open windows or if all windows are minimized. You could add a reopen command to open a new default window or unminimize a window in those cases.
set site to "http://www.google.com"
tell application "Google Chrome"
reopen
activate
tell window 1
make new tab with properties {URL:site}
end tell
end tell
Another option might be to use something like do shell script "open " & quoted form of "http://google.com" & " -a Google\ Chrome". It's often closer to the default behavior of browsers.
Can't you just do this?
tell application "Google Chrome"
activate
set theSite to "http://www.google.com/"
open location theSite
end tell
The “open location” command is part of Standard Additions and is available to all Mac apps. It’s the default way to open a website in any Mac app. You might notice that Safari’s AppleScript dictionary doesn’t even include a way to open a website — it simply uses the standard “open location” command which you can find in the Standard Additions dictionary.

Mac: reloading document in Chrome or Firefox?

How can I tell Chrome or Firefox to reload the document in the top window? Here's what I'm using for Safari:
osascript -e '
tell application "Safari"
activate
do JavaScript "history.go(0)" in document 1
end tell
'
Here's the code for Chrome:
tell application "Google Chrome"
tell the active tab of its first window
reload
end tell
end tell
Or more concisely:
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
I do not think Firefox or Chrome have special Applescript support, but you can send the keystrokes (Cmd-R) to refresh the page:
tell application "Firefox"
activate
tell application "System Events" to keystroke "r" using command down
end tell
Here's another way to do it in Safari without using JavaScript:
tell application "Safari"
tell its first document
set its URL to (get its URL)
end tell
end tell
Automator
Open Automator and choose a New Document
Choose Service
Set Service receives to no input
Choose Run AppleScript action from the action list.
Paste the following code in the script:
 
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
Save the the service, for example, using the name Chrome Refresh
System Preferences
Open System Preferences > Keyboard
In the Shortcuts tab, choose Services
Assign a new shortcut
The following answers above work well but using them result in DevTools refreshing in a new tab if it was the last tab/window in focus. I don't want DevTools to refresh in a new tab, I just want the first tab to refresh regardless of last focus/active and this worked well for me. Leaving for someone searching for this use case as well.
tell application "Google Chrome"
activate
tell application "System Events"
tell process "Google Chrome"
keystroke "r" using {command down, shift down}
end tell
end tell
end tell

Resources