My code to store tabs in a browser not working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
omar
Posts: 540
Joined: 22 Oct 2015, 17:56

My code to store tabs in a browser not working

Post by omar » 05 Feb 2023, 20:06

I need some help. I made some simple code to cycle through open tabs and store in a text file.

It's 1am and I can't figure out what's wrong.
Hoping someone can point out the flaw.
Code is started by pressing Ctrl + Window + F9.

I get random crazy results. If running from new, it only stores 2 URLs in the file and stops. If I run again, then it store 5 URL's more.

Here's the code:

Code: Select all

#NoEnv ; Recommended setting for new AHK scripts. Makes sure AHK doesn't access empty variables.

#SingleInstance force
;===============================
/*
The code is meant to cycle through open tabs and store URLs in a text file.
The code stops when we come to a URL that we already have stored.

If 2 URLs are open in tabs and are the same URL, then the code won't work. But I can live this!
*/
;===============================
MsgBox, ,, AutoHotKey ##%A_ScriptName%## now loaded and ready to be used, 1.5

; Start coding in the line below.
^#F9::

saving_tabs_array := []

Loop, 40 ;Intentionally I put a fixed number here - to stop looping forever if I get my loop wrong
{
	temp_array_string := ""
	for each, array_element in saving_tabs_array
		temp_array_string .= A_Index . ": " . array_element . "`n"
	
    if (InStr(temp_array_string, Clipboard)) ; Checking to see if we have cycled back to the same URL
	{
        ;MsgBox, URLs: %temp_array_string%
		break
	}
	else
	{
		; Value does not exist in the array of URLs, so we add to the array
		saving_tabs_array.Push(Clipboard)
		SendInput ^{tab} ; Move to next tab
	    SendInput !d ; This sends alt + d
		SendInput ^c ; send ctrl C - we have the current URL on clipboard now
		Sleep 100 ; we sleep for 100 miliseconds - AHK doesn't work properly without sleeps.
	}
}

FileAppend, %temp_array_string%, tabs.txt

Return

^#F10::Reload ;<- Windows Key + F10 causes the script to reload itself

^#F11::

MsgBox, ,, AutoHotKey ##%A_ScriptName%## Code about to close, 2.5
ExitApp ;<- Exit the app

Return
The code should be really simple. I don't know where I'm going wrong!

QUESTION: how do I clear the contents of the text file and write over whatever is there previously?

Thanks

omar
Posts: 540
Joined: 22 Oct 2015, 17:56

Re: My code to store tabs in a browser not working

Post by omar » 07 Feb 2023, 09:07

Anyone?

utvex
Posts: 10
Joined: 05 Feb 2023, 14:52

Re: My code to store tabs in a browser not working

Post by utvex » 07 Feb 2023, 11:42

Try to add sleep after each input. Also increase sleep time e.g. to 250 if you have slow computer

Post Reply

Return to “Ask for Help (v1)”