How to combine multiple scripts in one?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Stark78
Posts: 61
Joined: 03 Oct 2013, 03:17

How to combine multiple scripts in one?

30 Dec 2013, 10:20

I have many simple scripts like key remapping and program launching. Is it possible to combine them in one script without this to cause conflict between them?
Because of these scripts i have lots of autohotkey.exe processes running in Task Manager, so i am wondering if i can reduce their number and probably reduce the ram usage.
User avatar
AfterLemon
Posts: 85
Joined: 30 Sep 2013, 14:27
Location: Ohio, USA

Re: How to combine multiple scripts in one?

30 Dec 2013, 10:59

I'm not sure what to tell you here. Read some of the forum "Scripts" and see how they do it, test it yourself, give examples, and most of all... test test test. If something you're trying to combine does not work, try to figure out why and ask for help fixing it. Good luck!
HOME: Windows 11 Pro | AMD Ryzen 7 5800X 8-Core @ 4.50GHZ | 64GB RAM
MOBILE: Samsung Galaxy Note 10+
User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: How to combine multiple scripts in one?

30 Dec 2013, 14:21

Generally where you get in trouble combining scripts is with the Auto-Execute section.

http://docs.ahkscript.org/Scripts.htm

Simple scripts are often just hotkeys without an Auto-Execute section. These can often be combined as long as you don't run into the second major problem which is Variables with the same name.

If you combine hotkeys what have variables with the same names you are probably going to have problems.

If you just want to eliminate tray clutter you can add #NoTrayIcon to the beginning of each script.

You can also use the script in my signature to launch multiply scripts and combine them into one tray icon.

These two methods will still have multiply processes running so will do nothing for ram usage but in my experience ram usage with AutoHotkey is insignificant even with a dozen scripts running compared to what other common processes are using.

Each Autohotkey script has about a 2 meg overhead. My Internet Explorer is using about 200 meg. I am currently using 2.61 gig total memory just with typical stuff open. So AutoHotkey's memory usage is just a drop in the bucket as far as I am concerned.

But if the scripts really are just a bunch of simple hotkeys I would want to combine them just for convenience of editing and running if nothing else.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Wade Hatler
Posts: 60
Joined: 03 Oct 2013, 19:49
Location: Seattle Area
Contact:

Re: How to combine multiple scripts in one?

30 Dec 2013, 15:01

I think the last post was correct. Memory usage is trivial, but it still is a lot nicer to have everything in one script. I have one script with a couple thousand lines that I keep running all the time, and I like that better than having a lot of small ones.

I would basically start with the biggest script you have, and then start merging the other small ones into. In practice it's usually easier than it sounds. Here's what you have to worry about:
  1. The AutoExec section is everything before the first HotKey, HotString or function definition. This code is executed when the script starts, and then if there are any HotKeys, HotStrings the script stays resident.
  2. All of the AutoExec sections from all of your scripts have to be merged together.
  3. Any #include's in all of the scripts will have to be merged together. You can usually (but not always) put them in almost any order, and repeating the same one several times doesn't really heard anything so you don't have to be too fanatical about it.
  4. Any #If or #IfWinActive commands are effective until the end of the file. In practice this means that you will have to take all 'Global' HotKeys and put them at the top before the first #If… statement or at the bottom of a #If… Section, you can put a naked #If to restore global scope.
  5. Any global variables in the AutoExec section are super-global, meaning that they will automatically appear in any functions that have a variable with the same name. The vast majority of variables are just local variables and there is no real conflict. If you declare a variable in a function or HotKey section, most of the time you won't really have any problem. It's only globals that give you problems.
What I would do is take the biggest file you have, possibly rename it to something indicated that it will be bigger, and then start adding your smaller scripts to it. Simply separate a file into the following sections:
  1. #Includes
  2. AutoExec Section (including any global variables)
  3. Global Hotkeys/strings
  4. Hotkeys/strings with #If…
And merge them into the right spots in the main file. It's pretty easy in practice once you get to it.
Stark78
Posts: 61
Joined: 03 Oct 2013, 03:17

Re: How to combine multiple scripts in one?

30 Dec 2013, 15:21

Thanks, for the information!
User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: How to combine multiple scripts in one?

30 Dec 2013, 16:08

Wade Hatler wrote:If you declare a variable in a function or HotKey section, most of the time you won't really have any problem. It's only globals that give you problems. [/list]
Variables declared in a HotKey section are not local and problems can occur if two hotkeys use the same variable.

Code: Select all

f1::
	a := 5
return

f2::
	MsgBox % a
return
Hotkey f1 will definitely effect the value of "a" in Hotkey f2. Now whether that will cause a problem depends on how that variable is being used but it is something that needs to be taken into consideration.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Wade Hatler
Posts: 60
Joined: 03 Oct 2013, 19:49
Location: Seattle Area
Contact:

Re: How to combine multiple scripts in one?

30 Dec 2013, 18:58

DOH!! You're right ;)
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: How to combine multiple scripts in one?

01 Jan 2014, 10:30

One thing you can do with a compiled script that may sit in the tray and be dormant is to call EmptyWorkingSet() API.

Code: Select all

;call EmptyWorkingSet in AHK
_EmptyWorkingSet()
{
  return DllCall("psapi.dll\EmptyWorkingSet", "UInt", -1)
}
As soon as the script starts being invoked the OS allocates more resources. But if it's waiting for a key you may not press all day it might be worth the call.
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
cws
Posts: 27
Joined: 02 Oct 2017, 18:11

Re: How to combine multiple scripts in one?

17 Oct 2017, 13:00

From the sounds of it in this thread, there is no way to have a "global" variable that can be used in one script that contains multiple hotkeys. I use one script to run a bunch of different hotkeys that are located in the startup folder, and one of the things that I was hoping to do is assign a variable at the top of the script that has my password, then use that variable in multiple hotkeys.
User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: How to combine multiple scripts in one?

17 Oct 2017, 13:39

cws wrote:From the sounds of it in this thread, there is no way to have a "global" variable that can be used in one script that contains multiple hotkeys. I use one script to run a bunch of different hotkeys that are located in the startup folder, and one of the things that I was hoping to do is assign a variable at the top of the script that has my password, then use that variable in multiple hotkeys.
Your terminology is confusing to me.

A script can have multiply hotkeys. Most variables in that script will be global. Meaning that a variable can be used in multiple places within that script.

You can have multiply scripts running (each script is a separate file). Normally two separate scripts do not share any variables or communicate in any way. Now there are ways to make two scripts communicate with each other but two separate scripts are as separate as any other two applications running on a computer.

"Hotkeys" are not located in the startup folder. "Scripts" that might contain "Hotkeys" are located in the startup folder.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
cws
Posts: 27
Joined: 02 Oct 2017, 18:11

Re: How to combine multiple scripts in one?

18 Oct 2017, 13:29

Fair enough, perhaps I can rephrase and add some accuracy to my question.

I have a script that is compiled on the desktop with a shortcut in the startup folder.

That script has quite a few different hotkeys in it that I like to all have available while I am working.

I was hoping to assign a variable at the top of the script for my different passwords that I use for logging in to certain internal sites.

However, when I tried to do that by defining the variable at the top of the script like below (for example):

Code: Select all

corppw = p@assword
and then later in a script do something like this:

Code: Select all

SendRaw, %corppw% 
or maybe just

Code: Select all

Send, %corppw%
User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: How to combine multiple scripts in one?

18 Oct 2017, 15:30

cws wrote:I have a script that is compiled on the desktop with a shortcut in the startup folder.

That script has quite a few different hotkeys in it that I like to all have available while I am working.

I was hoping to assign a variable at the top of the script for my different passwords that I use for logging in to certain internal sites.
It looks like you only have one script and what you seem to be describing should work.

Here is an actual working example:

Code: Select all

global corppw := "p@ssword"

F12::
	Send % corppw
return

F11::
	SendPassword()
return

SendPassword()
{
	SendRaw % corppw
}
The global command is not needed for F12 to work but is needed for F11 as variables created within the function are local by default.

The global corppw := "p@ssword" needs to be near the top of the script before any hotkeys or returns.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], iamMG, morkovka18, ntepa, william_ahk and 178 guests