[Outdated] WhatsApp with AHK

Post your working scripts, libraries and tools for AHK v1.1 and older
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

[Outdated] WhatsApp with AHK

31 May 2015, 14:52

This is just a random idea I had. I just wanted to have a standalone WhatsApp window with a WhatsApp icon on my computer that I could minimize to the tray bar.
At the moment it simply notifies you (sound and traytip) when you get a new message and the traybar icon also indicates the new message count.
If you have a good idea what else I could implement, leave a post. ... I was thinking about adding Cleverbot to it to troll people. :lol: Idk.. if anyone likes the idea, I might give it a try.

v1.2 (Changelog)

Code: Select all

;<< Tray Menu
Menu, Tray, NoStandard
Menu, Tray, Add, Minimize to tray, GuiMinimizeToTray
Menu, Tray, Add, Exit, GuiClose
Menu, Tray, Default, Minimize to tray
Menu, Tray, Click, 2
Menu, Tray, Tip, WhatsApp AHK`nDouble-click to minimize
;>>
;<< Icon
If (!FileExist("WhatsApp_Gui.ico")) {
    If FileExist("WhatsApp_Gui.png") {
        FileDelete, WhatsApp_Gui.png
        Sleep, 100
    }
    imgUrl := WhatsApp_GetFavIcon(0,2)
    UrlDownloadToFile, % imgUrl, WhatsApp_Gui.png
    MakeIco(["WhatsApp_Gui.png"])
    Sleep, 10
    FileDelete, WhatsApp_Gui.png
}
Menu, Tray, Icon, WhatsApp_Gui.ico
;>>
;<< Gui
Gui, 1: Add, ActiveX, x0 y0 vWB -HScroll, Shell.Explorer
Gui, Add, Button, Hidden Default gGUI_SendButton, Send
Gui, 1: +HwndMyGuiHwnd +Resize
Gui, Show, w800 h500, WhatsApp
;>>
;<< WB
ComObjConnect(WB, WB_events)
WB.silent := True
WB.Navigate("about:<!DOCTYPE html><head><meta http-equiv='X-UA-Compatible' content='IE=edge'/></head>")
WB.Navigate("https://web.whatsapp.com/",0,0,0,"User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0")
Return
;>>

Class WB_events{
    NewWindow3(ppDisp, cancel, dwFlags, bstrUrlContext, bstrUrl) {
        NumPut(True, ComObjValue(cancel))
    }
    TitleChange(newTitle) {
        Static oldMsgCount := 0
        If (InStr(newTitle,"WhatsApp")) {
            newTitle := StrReplace(newTitle, " Web", " AHK")
            Gui, +LastFound
            WinGetTitle, oldTitle
            WinSetTitle, % newTitle
            If (oldTitle != newTitle) {
                RegExMatch(newTitle, "\(([^\)]+)\) WhatsApp", match)
                msgCount := match1
                FileDelete, WhatsApp_Tray.png
                Sleep, 10
                imgUrl := WhatsApp_GetFavIcon(msgCount,1)
                UrlDownloadToFile, % imgUrl, WhatsApp_Tray.png
                Sleep, 10
                MakeIco(["WhatsApp_Tray.png"])
                FileDelete, WhatsApp_Tray.png
                Sleep, 10
                Menu, Tray, Icon, WhatsApp_Tray.ico,,1
                If (msgCount > oldMsgCount)
                    TrayTip, % newTitle, % "New message" (msgCount>1?"s":"") "!"
                oldMsgCount := msgCount
            }
        }
    }
}

GUI_SendButton() {
    Global WB
    inputs := WB.Document.getElementsByClassName("input")
    Loop % inputs.length {
        curInput := inputs[A_Index-1]
        If (InStr(curInput.outerHTML, "placeholder=""type a message""")) {
            If (curInput.innerHTML = "<br>" || curInput.innerHTML = "<br>&nbsp;") {
                curInput.innerHTML := ""
            }
            If (curInput.innerHTML != "") {
                curInput.innerHTML := curInput.innerHTML
                curInput.blur()
                curInput.focus()
                SendInput, {Space}{BS}
                WB.Document.getElementsByClassName("send-container")[0].Click()
            }
        }
    }
        
        
    ;}
}
GuiSize(guiHwnd, eventInfo, width, height) {
    Global WB
    If (eventInfo = 1)
        GuiMinimizeToTray()
    Else
        GuiControl, Move, WB, h%height% w%width%
}
GuiMinimizeToTray() {
    Gui +lastfound
    WinHide
    Menu, Tray, Delete, Minimize to tray
    Menu, Tray, Add, Restore, Restore
    Menu, Tray, Default, Restore
    Menu, Tray, Tip, WhatsApp AHK`nDouble-click to restore
    Menu, Tray, Delete, Exit
    Menu, Tray, Add, Exit, GuiClose
    ;TrayTip, WhatsApp AHK, I'm still around and will keep running in the background.
}
Restore() {
    Gui +lastfound
    WinShow
    WinRestore
    WinActivate
    Menu, Tray, Delete, Restore
    Menu, Tray, Add, Minimize to tray, GuiMinimizeToTray
    Menu, Tray, Default, Minimize to tray
    Menu, Tray, Tip, WhatsApp AHK`nDouble-click to minimize
    Menu, Tray, Delete, Exit
    Menu, Tray, Add, Exit, GuiClose
}
GuiClose() {
    ExitApp
}

WhatsApp_GetFavIcon(msgCount:=0,icoSize:=1) {
    Static sizeArray := ["1x","2x"]
    If (msgCount > 0)
        Return "https://web.whatsapp.com/img/favicon/" sizeArray[icoSize] "/f" (msgCount<10 ? "0" msgCount : (msgCount > 99 ? "00" : msgCount)) ".png"
    Else If (!msgCount)
        Return "https://web.whatsapp.com/img/favicon/" sizeArray[icoSize] "/favicon.png"
    Else If (msgCount < 0)
        Return "https://web.whatsapp.com/img/favicon/" sizeArray[icoSize] "/favicon-error2.png"
}
MakeIco(sourceImages, icon:="") { ;Credits to kon - http://ahkscript.org/boards/viewtopic.php?p=14093#p14093
    If (!icon) {
        SplitPath, % sourceImages[1],, si_dir,, si_name
        If (si_dir)
            icon := si_dir "\" si_name ".ico"
        Else
            icon := si_name ".ico"
    }
	ICO_ContainerSize := 16
	O := 0
	Total_PotentialSize := 6
    
	for i, img in sourceImages {
		FileGetSize, imgSize, % img
		if (imgSize)
			Total_PotentialSize += ICO_ContainerSize + imgSize
	}
    
	VarSetCapacity(ICO_Data, Total_PotentialSize, 0)
	NumPut(0, ICO_Data, O, "UShort"), O += 2 ;reserved - always 0
	NumPut(1, ICO_Data, O, "UShort"), O += 2 ;ico = 1, cur = 2
	NumPut(0, ICO_Data, O, "UShort"), O += 2 ;number of images in file (will be updated below)
    
	For i, img in sourceImages {
        dim := ImgGetDimensions(img)
        width := (dim.width=256 ? 0 : dim.width)
        height := (dim.height=256 ? 0 : dim.height)
		If (width <= 255 && height <= 255 && File := FileOpen(img, "r")) {
			NumPut(width, ICO_Data, O, "UChar"),      O += 1 ;image width (256 = 0)
			NumPut(height, ICO_Data, O, "UChar"),     O += 1 ;image height (256 = 0)
			NumPut(0, ICO_Data, O, "UChar"),          O += 1 ;color palatte: 0 if not used
			NumPut(0, ICO_Data, O, "UChar"),          O += 1 ;reserved - always 0
			NumPut(1, ICO_Data, O, "UShort"),         O += 2 ;ico - color planes (0/1), cur - horizontal coordinates of the hotspot in pixels from left
			NumPut(32, ICO_Data, O, "UShort"),        O += 2 ;ico - bits per pixel, cur - vertical coordinates of hotspot in pixels from top
			NumPut(File.Length, ICO_Data, O, "UInt"), O += 4 ;size of image data in bytes
			NumPut(O + 4, ICO_Data, O, "UInt"),       O += 4 ;offset of image data from begining of ico/cur file
			File.RawRead(&ICO_Data + O, File.Length), O += File.Length ;actual image data
			NumPut(NumGet(ICO_Data, 4, "UShort") + 1, ICO_Data, 4, "UShort") ;adds one to the total number of images
			File.Close()
		}
	}
 
	If (O > 6){
		File := FileOpen(icon, "w")
		File.RawWrite(&ICO_Data, O)
		File.Close()
		VarSetCapacity(ICO_Data, O, 0)
		VarSetCapacity(ICO_Data, 0)
        Return True
	}
}
ImgGetDimensions(fileFullPath) {
    dimensions := {}
    RegexMatch(FileGetProperty(fileFullPath, "Height"), "(\d+)", match)
    dimensions.height := match1
    RegexMatch(FileGetProperty(fileFullPath, "Width"), "(\d+)", match)
    dimensions.width := match1
    Return dimensions
}
FileGetProperty(FilePath, Property) { ;Credits to kon - http://www.autohotkey.com/boards/viewtopic.php?f=6&t=3806
    static PropTable
    if (!PropTable) {
        PropTable := {Name: {}, Num: {}}, Gap := 0
        oShell := ComObjCreate("Shell.Application")
        oFolder := oShell.NameSpace(0)
        while (Gap < 11)
            if (PropName := oFolder.GetDetailsOf(0, A_Index - 1)) {
                PropTable.Name[PropName] := A_Index - 1
                PropTable.Num[A_Index - 1] := PropName
                Gap := 0
            }
            else
                Gap++
    }
    If (!InStr(FilePath,":") && !InStr(FilePath,"\\"))
        FilePath := A_ScriptDir "\" FilePath
    if ((PropNum := PropTable.Name[Property] != "" ? PropTable.Name[Property]
    : PropTable.Num[Property] ? Property : "") != "") {
        SplitPath, FilePath, FileName, DirPath
        oShell := ComObjCreate("Shell.Application")
        oFolder := oShell.NameSpace(DirPath)
        oFolderItem := oFolder.ParseName(FileName)
        if (PropVal := oFolder.GetDetailsOf(oFolderItem, PropNum))
            return PropVal
        return 0
    }
    return -1
}
Old versions
Spoiler
Image

Note:
This script heavily relies on the installed version of your Internet Explorer. If your Internet Explorer version is 8 or below, this script will likely not work properly.
I am testing this script on:
AutoHotkey: 1.1.22.00, 32 Bit, Unicode
OS: Windows 8.1 Pro, x64
IE: 11.0.9600.17801
Last edited by Bruttosozialprodukt on 22 Nov 2016, 06:47, edited 5 times in total.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: WhatsApp with AHK

31 May 2015, 17:41

Nice, I like it :thumbup:.

Three thoughts:
• Perhaps it looks better to change or clear the tray text from the default script name.
• Double clicking the tray produces an error message for me: 076: Menu,Tray,Delete,Minimize to tray or 088: Menu,Tray,Delete,Restore.
• Could you implement a 'Press enter to send' function, perhaps by sending a mouse click on the send button? (There's no built-in option for that, is there?)
User avatar
joedf
Posts: 8986
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: WhatsApp with AHK

31 May 2015, 21:18

Nice, interesting script
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
coolykoen
Posts: 61
Joined: 01 Jun 2015, 06:10

Re: WhatsApp with AHK

01 Jun 2015, 06:27

it gives the error that whatsapp recomments using chrome/firefox etc
so could you update/change the useragent or try any other fix?

otherwise great idea :)

screenshot:
https://drive.google.com/file/d/0Bz7e6p ... sp=sharing
alternative link (same screenshot):
http://imgur.com/WD6lRdu
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: WhatsApp with AHK

01 Jun 2015, 06:54

That's odd, that's already being done with this line: WB.Navigate("https://web.whatsapp.com/",0,0,0,"User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"). Perhaps you're using some anti-virus or privacy software that's stripping that header from the request?
coolykoen
Posts: 61
Joined: 01 Jun 2015, 06:10

Re: WhatsApp with AHK

01 Jun 2015, 08:57

Nextron wrote:That's odd, that's already being done with this line: WB.Navigate("https://web.whatsapp.com/",0,0,0,"User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"). Perhaps you're using some anti-virus or privacy software that's stripping that header from the request?
well.... now i get this...
Image

i have a feeling im doing something wrong, cause i dont think your code is wrong -.- (no i didnt edit your code :P )
Last edited by coolykoen on 01 Jun 2015, 08:59, edited 1 time in total.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: WhatsApp with AHK

01 Jun 2015, 08:58

Tell us your AHK Version. Maybe outdated / old.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
coolykoen
Posts: 61
Joined: 01 Jun 2015, 06:10

Re: WhatsApp with AHK

01 Jun 2015, 09:01

jNizM wrote:Tell us your AHK Version. Maybe outdated / old.
1.1.20.03 (downloading newest right now)
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: WhatsApp with AHK

01 Jun 2015, 09:09

Yes the update will fix this

SplitPath, % sourceImages[1],, si_dir,, si_name
1.1.21.00 - March 28, 2015

Changed InputVar parameters to allow % expression (except with If commands).
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

Re: WhatsApp with AHK

01 Jun 2015, 09:11

Testing environment was:
AutoHotkey: 1.1.22.00, 32 Bit, Unicode
OS: Windows 8.1 Pro, x64
IE: 11.0.9600.17801
coolykoen
Posts: 61
Joined: 01 Jun 2015, 06:10

Re: WhatsApp with AHK

01 Jun 2015, 09:22

Bruttosozialprodukt wrote:Testing environment was:
AutoHotkey: 1.1.22.00, 32 Bit, Unicode
OS: Windows 8.1 Pro, x64
IE: 11.0.9600.17801
its working now thanks :)
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

Re: WhatsApp with AHK

01 Jun 2015, 09:43

1.1.21.00 - March 28, 2015
Changed InputVar parameters to allow % expression (except with If commands).
That's funny, I didn't even know this wasn't a thing before.
• Perhaps it looks better to change or clear the tray text from the default script name.
I don't understand.

I will fix the double clicking (for now only use one click) and I will try to add 'enter to send'.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: WhatsApp with AHK

01 Jun 2015, 10:02

Currently when you hover the cursor over the tray icon you'll get a see a tooltip with the name-of-the-script.ahk. Perhaps change that with Menu, Tray, Tip, WhatsApp AHK`nClick to open or close
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

Re: WhatsApp with AHK

03 Jun 2015, 12:42

Okay here are the discussed changes for v1.1:
-Minimize/restore to/from tray now requires double-clicking
-The tray tip when hovering over the tray icon now shows either WhatsApp AHK`nDouble-click to minimize or WhatsApp AHK`nDouble-click to restore
-Pressing enter to send a message is implemented
User avatar
Joe Glines
Posts: 771
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: WhatsApp with AHK

04 Jun 2015, 07:10

While your script may be interesting, there seems to be a lot of code around getting icons (which doesn't work for me at work) thus means i cannot try it w/o a lot of work. :( I highly recommend just picking from existing windows icons. There are tons to choose from.. Scroll down on this page and it offers a lot https://diymediahome.org/windows-icons- ... ns-images/
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

Re: WhatsApp with AHK

04 Jun 2015, 08:47

Nope, I want the original icons. The nice thing about this is that the prorgam can show how many new messages you got on the tray icon.
Could you please share your error message and tell me which OS you are on? The Internet Explorer version would be helpful too.
Is your Internet access restricted? Firewall? Proxy?
User avatar
Joe Glines
Posts: 771
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: WhatsApp with AHK

04 Jun 2015, 09:21

I'm behind a proxy.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

Re: WhatsApp with AHK

04 Jun 2015, 10:35

Have you configured the proxy in your Internet options?
Is the proxy known to block certain content?

And again, what error messages do you get?

(Just fyi, I highly doubt, that using a system icon would change anything for you. The script doesn't only download the favicons from whatsapp, it also downloads a whole bunch of other images, fonts, css, js and html files.)
User avatar
joedf
Posts: 8986
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: WhatsApp with AHK

04 Jun 2015, 13:08

Base64 ftw!
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Insaid, kaka2 and 92 guests