Connecting to specific Chromecasts

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

Connecting to specific Chromecasts

10 Jun 2021, 15:01

Hello,

I am currently working on a script where I need to have three chrome tabs open and connect to specific chromecasts (one per tab). I am able to get it to where I can open up chrome and three tabs and direct myself to the cast setting. However, there are other chromecasts that I see on my network that are not necessary for this project. Is there a way with AHK where I can direct myself to those specific chromecasts without having the script cast to one that wasn't necessary?

Thanks!
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Connecting to specific Chromecasts

10 Jun 2021, 17:34

Well, by using Chrome.ahk (and the chrome debugging protocol) with a Chrome browser running in debug mode, you can initiate simple Tabmirroring to specific Chromecasts, afaik (I have only one Chromecast, but it should work with multiple "sinks").

Let's say, you have Chrome.ahk already running and Page is a reference to your current tab, then you can do this:

Code: Select all

Page.Call("Cast.enable")
; start casting
Page.Call("Cast.startTabMirroring", { sinkName: "NameOfMyChromeCast"})
; stop casting
Page.Call("Cast.stopCasting", { sinkName: "NameOfMyChromeCast"})
Then repeat this for your other two tabs/Chromecasts.

reference to the corresponding domain of the chrome debugging protocol: https://chromedevtools.github.io/devtools-protocol/tot/Cast/

Alternatives to Chrome.ahk: try to click the right buttons by coordinates or Imagesearch/Findtext() or some other visual clues - or perhaps something based on Accessibility libraries.
What are you doing currently to get to the cast settings (afaik, there is no keyboard shortcut) ?


More on automating Chromecasts:
Spoiler
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

Re: Connecting to specific Chromecasts

10 Jun 2021, 19:51

gregster wrote:
10 Jun 2021, 17:34
Well, by using Chrome.ahk (and the chrome debugging protocol) with a Chrome browser running in debug mode, you can initiate simple Tabmirroring to specific Chromecasts, afaik (I have only one Chromecast, but it should work with multiple "sinks").

Let's say, you have Chrome.ahk already running and Page is a reference to your current tab, then you can do this:

Code: Select all

Page.Call("Cast.enable")
; start casting
Page.Call("Cast.startTabMirroring", { sinkName: "NameOfMyChromeCast"})
; stop casting
Page.Call("Cast.stopCasting", { sinkName: "NameOfMyChromeCast"})
Then repeat this for your other two tabs/Chromecasts.

reference to the corresponding domain of the chrome debugging protocol: https chromedevtools.github.io /devtools-protocol/tot/Cast/ Broken Link for safety

Alternatives to Chrome.ahk: try to click the right buttons by coordinates or Imagesearch/Findtext() or some other visual clues - or perhaps something based on Accessibility libraries.
What are you doing currently to get to the cast settings (afaik, there is no keyboard shortcut) ?


More on automating Chromecasts:
Spoiler
When repeating for the other two Chromecasts, do I have to make another page instance or can I add them within the sinkName parameter?
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Connecting to specific Chromecasts

10 Jun 2021, 20:04

The value of sinkname is just the name of the Chromecast you want to connect to, it has nothing to do with a specific tab.

If you have different tabs to mirror, you'll need of course separate page instances for them (use GetPage() or GettPageByURL(), ... or something like that from Chrome.ahk to get them) - Page is just a random name in my example, you could also name them Tab1, Tab2, Tab3, or whatever.
Edit: Actually, just overwriting the first page instance doesn't seem to break its ongoing mirroring session. But to start another session from a different tab, you will have to connect to it first.

I think I only tried out once connecting to a Chromecast with Chrome.ahk, before today. ;)
rgoijre23
Posts: 20
Joined: 24 May 2021, 13:43

Re: Connecting to specific Chromecasts

11 Jun 2021, 18:42

gregster wrote:
10 Jun 2021, 20:04
The value of sinkname is just the name of the Chromecast you want to connect to, it has nothing to do with a specific tab.

If you have different tabs to mirror, you'll need of course separate page instances for them (use GetPage() or GettPageByURL(), ... or something like that from Chrome.ahk to get them) - Page is just a random name in my example, you could also name them Tab1, Tab2, Tab3, or whatever.
Edit: Actually, just overwriting the first page instance doesn't seem to break its ongoing mirroring session. But to start another session from a different tab, you will have to connect to it first.

I think I only tried out once connecting to a Chromecast with Chrome.ahk, before today. ;)
So this is what I have so far for my program. I am understanding it way more, but starting from a different tab is having some issues. I see where it overrides because I just ran this code and TabOne doesn't "happen." How would I go about connecting to it first like you said in your response so it can open the different URL in a new tab?

Code: Select all

#NoEnv
SetBatchLines, -1

#Include ../Chrome.ahk

; Create an instance of the Chrome
ChromeInst := new Chrome()

; Connect to the page
if !(PageInst := ChromeInst.GetPage())
{
    MsgBox, Could not retrieve page!
    ChromeInst.Kill()
}
else
{
    PageInst.Call("Page.navigate", {"url": "NameOfToUrl1"})
    PageInst.WaitForLoad()
    TabOne := ChromeInst.GetPageByUrl("NameOfToUrl1")

    PageInst.Call("Page.navigate", {"url": "NameOfToUrl2"})
    PageInst.WaitForLoad()
    TabTwo := ChromeInst.GetPageByUrl("NameOfToUrl2")

    ; Start casting
    TabOne.Call("Cast.enable")
    TabOne.Call("Cast.startTabMirroring", { sinkName: "NameOfChromeCast1" })

    TabTwo.Call("Cast.enable")
    TabTwo.Call("Cast.startTabMirroring", { sinkName: "NameOfChromeCast2" })

    ExitApp
}
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Connecting to specific Chromecasts

13 Jun 2021, 23:35

It seems you are navigating away from URLs that you want to cast.

If you want to open different tabs programmatically, and control them separately, you should call Target.createTarget.
Something like this:

Code: Select all

#NoEnv											
SetBatchLines, -1
SetTitleMatchMode 2

#include chrome.ahk

url1 := "https://www.google.com/"
url2 := "http://example.com/"
url3 := "https://www.youtube.com/watch?v=B9OdCjvudgk"

; --- Create a new Chrome instance ---
ChromeInst := new Chrome()			
winwait, - Google Chrome 	

; --- Connect to the page ---
Page1 := ChromeInst.GetPage( )
Page1.Call("Page.navigate", {"url": url1})			
Page1.Call("Cast.enable")

;  create a second tab
Page1.Call("Target.createTarget", {url : url2 })
Page2 := ChromeInst.GetPageByURL(url2)
Page2.Call("Cast.enable")

; create a third tab
Page1.Call("Target.createTarget", {url : url3 })
Page3 := ChromeInst.GetPageByURL(url3)		
Page3.Call("Cast.enable")

; Wait for all three pages to load
Page1.WaitForLoad()
Page2.WaitForLoad()
Page3.WaitForLoad()

; start casting
Page1.Call("Cast.startTabMirroring", { sinkName: "ChromeCast1"})
Page2.Call("Cast.startTabMirroring", { sinkName: "RomeCast"})
Page3.Call("Cast.startTabMirroring", { sinkName: "RoamFast"})
;...

Esc::ExitApp
Of course, you could already start casting on the first tab, before the second and third tabs are loaded (or even created). This is just an example with three different URLs on three different tabs, casting to three different Chromecasts. This way, you could also stop each casting session individually, or navigate elsewhere...

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: joedf, OrangeCat, scriptor2016, supplementfacts and 138 guests