Condense a part of a script to be referred to later as a shortcut?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
VV710
Posts: 8
Joined: 17 Feb 2017, 15:12

Condense a part of a script to be referred to later as a shortcut?

10 Jan 2019, 14:27

I feel like i just cant figure out the right words to search for this.

But basically i want to figure out how to select a big chunk of my script and call it like a shortcut name so the rest of the script can just refer to that section?

Let me know if i need to clarify anymore
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Condense a part of a script to be referred to later as a shortcut?

10 Jan 2019, 14:41

Are you referring to a Label?

Code: Select all

#SingleInstance, Force

NumCount := 0

Loop, 10 {
	GoSub, Label1
	NumCount++
}

Label1:
	MsgBox, % NumCount
return
Or Function?

Code: Select all

#SingleInstance, Force

NumCount := 0

Loop, 10 {
	Function1(5)
}

Function1(Num) {
	Global NumCount
	NumCount := NumCount + Num
	MsgBox, % NumCount
}
VV710
Posts: 8
Joined: 17 Feb 2017, 15:12

Re: Condense a part of a script to be referred to later as a shortcut?

10 Jan 2019, 15:34

I mean something like the below, needing to be written out once in a script, but i could target this section again later on without having to copy it lower creating a ridiculously long script. Its a lot longer but basically how the same step is at the top and bottom of what ive posted, is there a way to call the first chunk something and just use that name in place of the same chunk again at the bottom?

F10::
Loop,
{
Suspend
;Copy ID from Excel
WinActivate, Script Data.xlsx - Excel
Sleep 1000
Send, {Down}
Sleep 500
Clipboard := ""
Send, ^c
ClipWait
if (Clipboard = "`r`n")
break
Sleep 2000


WinActivate, Google Chrome
Sleep 1000
Send, ^f
Sleep 1000
SendRaw, Status
Sleep 1000
Send, {Escape}
Sleep 1000
Send, {Tab}
Sleep 1000
Send, {Up 2}
Sleep 1000
Send, ^f
Sleep 1000
SendRaw, Search by:
Sleep 1000
Send, {Escape}
Sleep 1000
Send, {Tab}
Sleep 1000
Send, ^v
Sleep 3000
Send, {Tab 4}
Sleep 2000
Send, {Enter}
Sleep 3000
Send, ^f
Sleep 1500
SendRaw, Manage
Sleep 1000
Send, {Escape}
Sleep 1000
Send, {Enter}
Sleep 5000

;Copy Number from Excel
WinActivate, Script Data.xlsx - Excel
Sleep 1000
Send, {Right}
Clipboard := ""
send, ^c
ClipWait
if (Clipboard = "`r`n")
break
Sleep 1000
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Condense a part of a script to be referred to later as a shortcut?

10 Jan 2019, 16:11

Code: Select all

#SingleInstance, Force

F10::
Loop {
	Suspend
	GoSub, Chunk1
	Sleep 2000
	WinActivate, Google Chrome
	Sleep 1000
	Send, ^f
	Sleep 1000
	SendRaw, Status
	Sleep 1000
	Send, {Escape}
	Sleep 1000
	Send, {Tab}
	Sleep 1000
	Send, {Up 2}
	Sleep 1000
	Send, ^f
	Sleep 1000
	SendRaw, Search by:
	Sleep 1000
	Send, {Escape}
	Sleep 1000
	Send, {Tab}
	Sleep 1000
	Send, ^v
	Sleep 3000
	Send, {Tab 4}
	Sleep 2000
	Send, {Enter}
	Sleep 3000
	Send, ^f
	Sleep 1500
	SendRaw, Manage
	Sleep 1000
	Send, {Escape}
	Sleep 1000
	Send, {Enter}
	Sleep 5000
	GoSub, Chunk1
	Sleep 1000
}
return

Chunk1:
	;Copy ID from Excel
	WinActivate, Script Data.xlsx - Excel
	Sleep 1000
	Send, {Down}
	Sleep 500
	Clipboard := ""
	Send, ^c
	ClipWait
	if (Clipboard = "`r`n")
	break
return
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: Condense a part of a script to be referred to later as a shortcut?

13 Jan 2019, 18:49

I think you are just trying to make a simple function https://autohotkey.com/docs/Functions.htm. Try this:

Code: Select all

F10::
Loop,
{
Suspend
CopyIDfromExcel()

WinActivate, Google Chrome
Sleep 1000
Send, ^f
Sleep 1000
SendRaw, Status
Sleep 1000
Send, {Escape}
Sleep 1000
Send, {Tab}
Sleep 1000
Send, {Up 2}
Sleep 1000
Send, ^f
Sleep 1000
SendRaw, Search by:
Sleep 1000
Send, {Escape}
Sleep 1000
Send, {Tab}
Sleep 1000
Send, ^v
Sleep 3000
Send, {Tab 4}
Sleep 2000
Send, {Enter}
Sleep 3000
Send, ^f
Sleep 1500
SendRaw, Manage 
Sleep 1000
Send, {Escape}
Sleep 1000
Send, {Enter}
Sleep 5000

CopyIDfromExcel() 
}

; Function (I usually put my custom functions at the bottom of my scripts):

CopyIDfromExcel() {
WinActivate, Script Data.xlsx - Excel
Sleep 1000
Send, {Right}
Clipboard := ""
send, ^c
ClipWait
if (Clipboard = "`r`n")
break
Sleep 1000
}
or you can take the whole chunk to a function like this:

Code: Select all

F10::
Loop, 
{
Suspend
CopyIDEntireProcess() 
}

; Function

CopyIDEntireProcess() {
WinActivate, Script Data.xlsx - Excel
Sleep 1000
Send, {Down}
Sleep 500
Clipboard := ""
Send, ^c
ClipWait
if (Clipboard = "`r`n")
break
Sleep 2000


WinActivate, Google Chrome
Sleep 1000
Send, ^f
Sleep 1000
SendRaw, Status
Sleep 1000
Send, {Escape}
Sleep 1000
Send, {Tab}
Sleep 1000
Send, {Up 2}
Sleep 1000
Send, ^f
Sleep 1000
SendRaw, Search by:
Sleep 1000
Send, {Escape}
Sleep 1000
Send, {Tab}
Sleep 1000
Send, ^v
Sleep 3000
Send, {Tab 4}
Sleep 2000
Send, {Enter}
Sleep 3000
Send, ^f
Sleep 1500
SendRaw, Manage 
Sleep 1000
Send, {Escape}
Sleep 1000
Send, {Enter}
Sleep 5000

WinActivate, Script Data.xlsx - Excel
Sleep 1000
Send, {Right}
Clipboard := ""
send, ^c
ClipWait
if (Clipboard = "`r`n")
break
Sleep 1000
}
Also, I would highly recommend using COM to automate Excel as opposed to sending keystrokes, it is much faster and more reliable and can run in the background invisibly if needed. I started out trying to automate excel in the way you are doing (and I think a lot of people do) and it was very unreliable and required a lot Sleep and Send commands that aren't very reliable. PM me and I'd be happy to give you more info on automating Excel via COM. You can also do this to automate web scraping, there are tons of tutorials on it, but I'm not that experienced with it yet so you'll have to put in the work yourself :beer:
-TL

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: effel, rc76 and 197 guests