Google Chrome doesn't open URL with AppleScript - macos

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.

Related

Is there a way to pause a YouTube video in Google Chrome using AppleScript?

What I'm trying is to type tell application "Google Chrome" to pause and make audio or video in Google Chrome pause. It wouldn't work, since it thinks pause is a variable.
Is there any way to either pause audio or video in all applications or just Google Chrome?
From comments:
The following example AppleScript code will play/pause a YouTube video in the active tab of the front window of Google Chrome: tell application "Google Chrome" to tell active tab of front window to execute javascript "document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"
#user3439894 this one works perfectly, since I only want to pause the video in the active tab. Is there any way to check if the video is paused so it doesn't unpause a video? Also could you write this comment as an answer so I can accept it as the correct answer?
There may be an easier way using JavaScript to ascertain if the active tab of the front window in Google Chrome has a YouTube video playing, however, as I do not know the code for it, here is a way that works for me.
The following example AppleScript code, shown below, was tested in Script Editor under macOS Catalina 10.15.7 and macOS Big Sur 11.2.3 using Google Chrome (Version 90.0.4430.93 (Official Build) (x86_64)) with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Example AppleScript code:
if not running of ¬
application "Google Chrome" then return
tell application "Google Chrome"
if (window count) = 0 then return
set atx to the active tab index of its front window
end tell
tell application "System Events"
-- # macOS Big Sur System Events bug issue.
-- # If running macOS Big Sur, uncomment the next
-- # two lines if System Events reports an error.
-- run
-- delay 0.5
tell application process "Chrome"
if (the value of ¬
attribute "AXTitle" of ¬
radio button atx of ¬
tab group 1 of ¬
group 1 of ¬
the front window) ¬
does not end with "Audio playing" then return
end tell
end tell
tell application "Google Chrome" to ¬
tell the active tab of its front window to ¬
execute javascript ¬
"document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"
Notes:
As coded, the execute javascript only happens if there is a YouTube video playing in the active tab of the front window of Google Chrome, thus pausing it. The code, as coded, cannot cause a paused video to play, regardless of the active tab or any tab of any window.
The -- # macOS Big Sur System Events bug issue. section in the example AppleScript code is an attempted workaround to a System Events issue in macOS Big Sur, in that the exact same code without it runs without issue in macOS Catalina and may not work or needs adjusting. I am still trying to troubleshoot the issue in general. In my experience the issue has not been reproducible at will and as such alternate methods may have to be used if issue continues and cannot be diagnosed.
Use of other than exact versions tested under may be partially to blame for some errors, especially any that involve a change in the hierarchical UI element structure in any UI Scripting scenario using System Events.
Is there any way to either pause audio or video in all applications or just Google Chrome?
Typically pressing the Play/Pause key on the keyboard should pause a video in Google Chrome, and most other apps as well.
It wouldn't work, since it thinks pause is a variable.
pause is not part of the AppleScript dictionary in Google Chrome and why it shows as a variable.
In Google Chrome, running AppleScript code containing JavaScript needs the Allow JavaScript from Apple Events menu item checked under: View > Developer -- Note that this used to be the default, however, at some time is was changed. For more information: https://support.google.com/chrome/?p=applescript
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.
This following AppleScript code will pause or play any instance of a YouTube video in any tab of any window in Google Chrome (whether visible or not).
pauseOrPlayYoutubeInChrome("ytp-play-button ytp-button", 0)
to pauseOrPlayYoutubeInChrome(theClassName, elementnum)
tell application "Google Chrome"
set youtubeTabsRef to a reference to (tabs of windows whose URL contains "youtube")
repeat with youtubeTabs in youtubeTabsRef
execute youtubeTabs javascript "document.getElementsByClassName('" & ¬
theClassName & "')[" & elementnum & "].click();"
end repeat
end tell
end pauseOrPlayYoutubeInChrome
My solution consists of running 2 AppleScript in a specific environment.
It works for me using 1 window of Brave Browser(I think it is the same for google chrome) with 2 tabs on youtube.
First script force opening Brave browser, second run keystroke "K"
tell application "Brave Browser"
if it is not running then launch
activate
end tell
tell application "System Events"
keystroke "k"
end tell
I have created a Quick Action on Automator and then I have followed this answer: https://apple.stackexchange.com/a/401262
And when trying to test it as a Keyboard Shortcut when using another Application, I was receiving an error like this "com.automator.runner.xpc is not allowed to send keystrokes", so I realised that I need to give accessibility privileges(on Security and Privacy) to all applications I want the script to work.
And After that, it works, but sometimes it gives the same error and it works again after some minutes(and never come back again) so I think this can help someone too.
This will pause or play the youtube video on the tab of the Brave Browser that is active.

How to find a specific tab while multiple Google Chrome windows are open in AppleScript

to clickClassName(theClassName, elementnum)
tell application "Google Chrome"
set youtubeTab to (the first tab of window 2 whose URL contains "youtube")
end tell
tell application "Google Chrome"
execute youtubeTab javascript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
end tell
end clickClassName
clickClassName("ytp-play-button ytp-button", 0)
I have two windows open on my MacBook. One on the MacBook itself another one on the external display. I would like to play/pause YouTube videos playing on the external display while taking notes on my MacBook. Above script works only if the windows on the MacBook is activated because it is specifically pointing to the other window via window 2.
I think I need to find all windows and somehow use the repeat operator so that I can play/pause the video regardless of which window is activated. But I am not sure how to approach.
Any help is appreciated!
PS. I have added this script as a Service and assigned a specific key combination. So when I am taking notes on my MacBook I can quickly play/pause the video.
The following example AppleScript code will click the Play/Pause button on a YouTube video in any (every) tab of Google Chrome whose URL contains youtube:
tell application "Google Chrome"
set YouTubeTabs to a reference to ¬
(tabs of windows whose URL contains "youtube")
repeat with aTab in YouTubeTabs
tell aTab to ¬
execute javascript ¬
"document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"
end repeat
end tell
Assuming you only have one tab in Google Chrome that has a YouTube video to play/pause, this will certainly do it for you.

Reload all chrome tabs of a certain page with 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

Applescript/Bashscript to activate incognito google chrome running in background

I know how to open google chrome in incongito mode:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --incognito
Also I found how to close incognito google chrome:
tell application "Google Chrome"
close (every window whose mode is "incognito")
end tell
Also from this link
tell application "Google Chrome"
set incognitoIsRunning to the (count of (get every window whose mode is "incognito")) is greater than 0
end tell
if (incognitoIsRunning) then
say "Shh"
end if
Using above script I tried this:
tell application "Google Chrome"
set incognitoIsRunning to the (count of (get every window whose mode is "incognito")) is greater than 0
end tell
if (incognitoIsRunning) then
tell application "Google Chrome" to activate
end if
But this did not activate the incognito google chrome, it is activating the normal mode google chrome.
I am running both normal and incognito chromes.
(incognito is playing songs, and working on normal chrome).
How can we activate incognito google chrome when also running normal mode google chrome?
Update
Borrowing idea from this link
I got this:
tell application "System Events" to tell process "Google Chrome"
perform action "AXRaise" of window 2
set frontmost to true
end tell
But this kills second google chrome, I need to activate the incognito,
even though there may be multiple normal google chromes running.
Related links:
How to check is Chrome is running in incognito mode using Applescript?
Applescript - Google Chrome activate a certain window
I'm hoping to have interpreted your situation correctly, having understood the following:
You're running a single instance of Google Chrome, which has several open windows, at least one of which is in incognito mode;
You wish to switch application focus to Google Chrome, but the methods you have tried typically bring the normal windows into the foreground, leaving the incognito windows at the back;
You want the switch to Google Chrome, essentially, to do the opposite, which would be to bring the incognito windows to the front, and leave the normal windows remaining open but at the back.
If I've interpreted you correctly, then:
tell application "Google Chrome"
activate
set index of (every window whose mode is "incognito") to 1
end tell
System info: AppleScript version: "2.7", system version: "10.13.6"
The activate command does what you would expect, and brings focus to Google Chrome's normal windows. The next line sets the index of each incognito window to 1, displacing the one before to one place behind. The normal windows, thus, end up at the rear as a natural consequence of this process.
The great thing about it is that, quite surprisingly, Google Chrome executes these two commands in a seamless fashion (under regular conditions). So there's no observable flicker or visible rearranging of windows: the incognito window that ends up frontmost simply appears at the front at the same time as Google Chrome comes into the foreground.
I'm not going to guarantee that will be the case across all systems or with a large number of open windows. But, on my pretty low-spec Macbook 12", the switch was fluid when tested with 4 normal windows plus 4 incognito windows.
I think the order of the incognito windows ends up being the reverse of what it was, but if it's important to preserve relative order, then you can simply execute the set index... command twice, which will reverse the reversed order. This, however, does produce the tiniest of visible transitions as we switch from the front incognito window to the rear one.

Opening a new Safari window with Alfred AppleScript opens 2 windows

When I run this script from Alfred (using the text "nsafari", and Safari is quit (not in the dock), two Safari windows will pop up. When I run it from the Script Editor, it will sometimes open up two windows, but sometimes not. (This also happens with my new Safari window script as well).
if application "Safari" is running then
tell application "Safari"
make new document
activate
end tell
else
tell application "Safari" to activate
end if
Why does it open two windows only from Afred? And how do I make it only create one?
I don't know why, but after two years, the problem I reported above does not seem to happen any more...
(and yes this is not a very satisfying answer)

Resources