ControlSend with Slack

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
GoGades
Posts: 3
Joined: 15 May 2019, 11:55

ControlSend with Slack

15 May 2019, 12:00

I've been trying to figure out how to use ControlSend to send keystrokes to Slack - I'd like to avoid activating the window if I can (which works fine, btw, with plain ol' Send)

I think the problem is that Slack is one of those annoying Electron-based apps which is just really a webapp in html/js rendered via an embedded chrome instance. I have tried various combinations of the ControlSend invocation, to no avail.

Code: Select all

ControlSend, Chrome_WidgetWin_1, this is a test, Slack
ControlSend, , this is a test, slack.exe
ControlSend, Chrome_WidgetWin_1, this is a test, slack.exe
No matter which way I try - nothing appears in the slack window.
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: ControlSend with Slack

15 May 2019, 12:21

Code: Select all

SetTitleMatchMode 2
ControlSend, Chrome_WidgetWin_1, this is a test, Slack
ControlSend, , this is a test, ahk_exe slack.exe
ControlSend, Chrome_WidgetWin_1, this is a test, ahk_exe slack.exe
I have never used Slack, but I would try SetTitleMatchMode 2; and the prefix ahk_exe if you use the name of the exe-file.
Slack also seems to have all kinds of official API interfaces for automation, which could help in this case (or not)... or be total overkill: https://api.slack.com/ https://api.slack.com/rtm
GoGades
Posts: 3
Joined: 15 May 2019, 11:55

Re: ControlSend with Slack

15 May 2019, 12:53

Thanks for taking the time to reply, I appreciate it. :thumbup:

I did try both SetTitleMatchMode (both setting to 2 and also Fast/Slow), as well as using the ahk_exe prefix in the WinTitle field - I was using the ControlSend page as reference (those examples work fine) and it omitted ahk_exe in its cmd.exe example.

That being said, no joy, that did not resolve it.

As far as the API goes, I investigated briefly and it would be massive overkill. Also since this is a work slack, I'm 100% sure they would not grant me access to the API (via OAuth). I can barely go to the washroom without some signed form ;)

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

Re: ControlSend with Slack

15 May 2019, 13:18

Yeah, I see...

But I think I had sometimes success with focusing the control first, when Controlsend alone didn't work. Something like this...

Code: Select all

SetTitleMatchMode 2
ControlFocus, Chrome_WidgetWin_1, ahk_exe slack.exe
ControlSend, Chrome_WidgetWin_1, this is a test, ahk_exe slack.exe
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: ControlSend with Slack

15 May 2019, 14:58

I have tested with Chrome browser, the ControlSend didn't work when Chrome is not activated.

Then I tesed slack webhooks, it worked and very simple.

Code: Select all

curl -k -X POST -H "Content-type: application/json" --data "{'text':'Hello world'}" Your_Webhook_URL

I also tested with Selenium to launch slack, and it worked.

Code: Select all

#Persistent

driver := ComObjCreate("Selenium.ChromeDriver")
driver.setBinary("C:\Users\Administrator\AppData\Local\slack\slack.exe")
driver.Start()
return

F1::driver.SendKeys("test string")
If you get errors from chromedriver.exe, you could try chromedriver v2.33 v2.36.

EDIT: chromedriver v2.33 had issues with element.submit(). v2.36 works.
Last edited by tmplinshi on 17 May 2019, 15:10, edited 1 time in total.
GoGades
Posts: 3
Joined: 15 May 2019, 11:55

Re: ControlSend with Slack

17 May 2019, 11:21

Wow, thanks so much for going thru all that trouble!

Unfortunately I can't get webhooks to our corporate slack, and I normally use the slack app (the electron-based wrapper around their site), so selenium is also not an option.

Oh well, regular Send with the window activated works so I guess I'll have to stick with that.

Thanks again for your efforts! :thumbup:
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: ControlSend with Slack

17 May 2019, 12:38

You're welcome. I'm happy to found out that Selenium are able to control Chrome based app.

BTW, I'm confused about what you said. Why Selenium isn't an option for you to use with the slack app? My example code was exactly showing doing that. Inside the code you can see driver.setBinary("C:\Users\Administrator\AppData\Local\slack\slack.exe") which tells Selenium to launch the slack app slack.exe, not the Chrome browser that I think you were thought.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: ControlSend with Slack

17 May 2019, 13:20

Here is another Selenium example using ObjRegisterActive together, so that other scripts can connect to existing Selenium COM object.

Run below code first:

Code: Select all

#Persistent

driver := ComObjCreate("Selenium.ChromeDriver")
driver.setBinary("C:\Users\Administrator\AppData\Local\slack\slack.exe")
driver.AddArgument("--silent") ; Not working yet. I want to disable console logging from chromedriver.exe, hope someone can help
driver.Start()

ObjRegisterActive(driver, "{527E30C4-2757-4945-9A7D-EAEFC45639CA}")
return
Keep the above script running, then we can connect to the driver from another script: :)

Code: Select all

driver := ComObjActive("{527E30C4-2757-4945-9A7D-EAEFC45639CA}")

e := driver.FindElementByCss("#msg_input > div.ql-editor")
e.Clear()
e.SendKeys("Message 1")
e.Submit()

Sleep, 500

e.executeScript("this.innerText = arguments[0]", "Message 2")
e.Submit()

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 169 guests