FastCode library - boosting speed of coding

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
MostWanted
Posts: 11
Joined: 03 Jan 2021, 07:46
Contact:

FastCode library - boosting speed of coding

Post by MostWanted » 08 Aug 2022, 02:39

Hey, everyone!
I've decided to share some workaround, that I was working with for quite a while.
The idea was to simplify and speed-up a coding process. In order to make it as simpler as possible and as fast as possible as well.
I've ended up with creating simple Library, that contains functions with the most commands of AHK that I was using all these years, automating different apps in Windows OS.

Github link - https://github.com/TripleSence/FastCode.AHK

Version 1.5 update:
1. FastCode library;
- added some more functions with commands, comments for each function to describe a logic
- created classes for the same type of functions that handle similar variables (class Window, class Button). To make it more easier to understand and read.
- updated initial description of file in the comments at the beginning of code
2. Replace.AHK:
- updated list commands in GUI:
- added descriptions for commands
- for the code, that posted via hotstrings, added comment to describe each line of code (example below).
- updated initial description of file in the comments at the beginning of code
3. updated list of global variables, that used in FastCode.AHK library and Replace.AHK script
Image

Usage:
1. Required AHK v1.1.31+
2. File Replace.AHK that contains Hotstrings to create functions/code in any editor via simple few simple shortcuts/hotstrings.
3. File FastCode.AHK - simple library that contains functions with most used commands of AHK.
Both files can be amended as per your requirements.

Example:

Task:
Let's say you want to program command, that will wait for specific window with specific visible button inside and then proceed further.

So, before FastCode i was doing something like that:

Code: Select all

WinWaitActive, Snipping Tool ahk_class Microsoft-Windows-SnipperToolbar ;wait for window
Sleep, 200
Loop {
ControlGet bEnabled, Visible,, Button3, Snipping Tool ahk_class Microsoft-Windows-SnipperToolbar
If bEnabled
Break
}
}
Minuses:
- it took some time to code
- AHK file at the end contains plenty of lines of code.
- hard to read a code even with comments

How I do it with FastCode:
1. I run Replace.AHK file, that contains Hotstrings with needed function lines.
2. I type in code editor Hotstring, for example "wwa."
Script Replace.AHK that running in the background replaces the Hostring with next line of code:

Code: Select all

;wait for specific window START
winname := "WindowName" ;window name/class
btnname := "ButtonName" ;button class/text
Window.wait() ;function that waits for specific window and then specific button inside of it
;wait for specific window FINISH
So, again, "wwa." is my key-phrase for "WinWaitActive" command that encased in FastCode.AHK function.

3. After that I only need to replace "WindowName" and "ButtonName" parameter as per data from WindowSpy.AHK. So, for "WindowName" I can use title of the window or class and for "ButtonName" I can use button class or button text.
As a result I have next final code:

Code: Select all

;wait for specific window START
winname := "Snipping Tool" ;window name/class
btnname := "Try Snip & Sketch" ;button class/text
Window.wait() ;function that waits for specific window and then specific button inside of it
;wait for specific window FINISH
instead of:

Code: Select all

WinWaitActive, Snipping Tool ahk_class Microsoft-Windows-SnipperToolbar ;wait for window
Sleep, 200
Loop {
ControlGet bEnabled, Visible,, Button3, Snipping Tool ahk_class Microsoft-Windows-SnipperToolbar
If bEnabled
Break
}
}
So, now it's much easier for me to read a code and find needed line to amend a code if needed. And also it helps other developers to read my code.

Minuses:
1. You need some time to code needed commands in the FastCode.AHK file as per your requirements. I have some prepared, but if you use others, than you just would need to amend it.
2. You need some time to code needed Hotstrings for Replace.AHK file, so that you can write code faster.

Benefits:
1. Speed coding
2. Smooth code
3. FastCode.AHK can be used independently from Replace.AHK, since Replace.AHK is only kind of tricky file as and addition to the main idea.

Source code of FastCode.AHK library:

Code: Select all

;/*
;===========================================
;  FastCode - simplifying tool for coders
;  https://www.autohotkey.com/boards/viewtopic.php?f=6&t=107165
;  https://github.com/TripleSence/FastCode.AHK  
;
;  Author  : MostWanted (TripleSence)
;  Version : 1.5
;  Date    : 2022-08-14
;
;  Usage:  (required AHK v1.1.31+)
;  1. Contains functions with the most used commands, so that coders can just use functions to make simple AHK coding. 
;  2. Zip file also comes with an AHK file called "Replace" that allows you to simplify the input of functions to speed-up coding.
;  3. When test is successful, you may copy the code
;  4. To start using FastCode, don't forget to include the library into the code with the "#Include" command.
;  5. Global variables related to functions, that need to be created in scripts: winname, btnname, timeifwin, messagetext, text_keys, menu1, submenu1. Example of usage: put "Global winname, btnname, timeifwin, messagetext, text_keys, menu1, submenu1, sleeptime" in the beginning of the script, that you create
;===========================================
;*/

;Functions list START
;Class that contains functions that work with Windows of apps START
class Window {
;function will wait for window and some visible button inside
wait() {
WinWaitActive, %winname% ;wait for window
Sleep, 200
Loop {
ControlGet bEnabled, Visible,, %btnname%, %winname%
If bEnabled
Break
}
}
;end of function

;function will wait for possible window and will click on it
possible() {
Sleep, %timeifwin%
If WinActive(winname) ;server connection window
{
Loop {
ControlGet bEnabled, Visible,, %btnname%, %winname%
If bEnabled
Break
}
Sleep, 400
ControlClick, %btnname%, %winname%
}
}
;end of function

;function will activate specific window from background and some visible button inside
activate() {
WinActivate, %winname% ;activate window
WinWaitActive, %winname% ;wait for window
Sleep, 200
Loop {
ControlGet bEnabled, Visible,, %btnname%, %winname%
If bEnabled
Break
}
}
;end of function

;function will close specific window
close() {
Sleep, 300
WinClose, %winname%
}
;end of function
}
;Class that contains functions that work with Windows of apps FINISH


;function holds a script performance for some specific time in miliseconds
Wait() {
Sleep, %sleeptime%
}
;end of function


;Class that contains functions that work with Buttons of apps START
class Button {
;function will wait for some visible button inside of specific winow
wait_visible() {
Sleep, 300 ;hold for 0.3 sec 
Loop {
ControlGet bEnabled, Visible,, %btnname%, %winname%
If bEnabled
Break
}
}
;end of function

;function will wait for some enabled (active) button inside of specific winow
wait_active() {
Sleep, 300 ;hold for 0.3 sec 
Loop {
ControlGet bEnabled, Enabled,, %btnname%, %winname%
If bEnabled
Break
}
}
;end of function

;function will click on specific button of specific window
click() {
Sleep, 300 ;hold for 0.3 sec 
ControlClick, %btnname%, %winname%
}
;end of function

;function will focus on specific button of specific window
focus() {
Sleep, 300 ;hold for 0.3 sec 
ControlFocus, %btnname%, %winname%
}
;end of function
}
;Class that contains functions that work with Buttons of apps FINISH


;function will select needed menu in the window
menu_select() {
Sleep, 300 ;hold for 0.3 sec 
WinMenuSelectItem, %winname%, , %menu1%, %submenu1%
}
;end of function

;function will send needed text or keys combination
Send() {
Sleep, 300 ;hold for 0.3 sec 
Send %text_keys%
}
;end of function

;function will send needed text or keys combination
ShowMessage() {
Sleep, 300 ;hold for 0.3 sec 
Msgbox, %messagetext%
}
;end of function
;Functions list FINISH
Source code of Replace.AHK file with Hotstrings:

Code: Select all

;/*
;===========================================
;  Replace - Hotstring commands set-up for FastCode AHK Library.
;  https://www.autohotkey.com/boards/viewtopic.php?f=6&t=107165
;  https://github.com/TripleSence/FastCode.AHK  
;
;  Author  : MostWanted (TripleSence)
;  Version : 1.5
;  Date    : 2022-08-14
;
;  Usage:  (required AHK v1.1.31+)
;  1. Contains functions with the most used commands, so that coders can just use functions to make simple AHK coding. 
;  2. Zip file also comes with an AHK file called "Replace" that allows you to simplify the input of functions to speed-up coding.
;  3. When test is successful, you may copy the code
;  4. To start using FastCode, don't forget to include the library into the code with the "#Include" command.
;  5. Global variables related to functions, that need to be created in scripts: winname, btnname, timeifwin, messagetext, text_keys, menu1, submenu1. Example of usage: put "Global winname, btnname, timeifwin, messagetext, text_keys, menu1, submenu1, sleeptime" in the beginning of the script, that you create
;===========================================
;*/

;OPTIMIZATIONS START
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
;Process, Priority, , A
;ZGVkaWNhdGVkIHRvIG15IHdpZmUgTGl6YSBCYXNlbmtv
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1	
SendMode Input
SetTitleMatchMode, 2
;OPTIMIZATIONS END

;User interface START
GuiRight := SizeRight - 300
Button_Right := SizeRight - 200
Button_Bottom := SizeBottom - 10
Gui, 1: Color, cFFFFFF
;List of commands in user interface START
Gui, Add, ListView, w300 ReplaceListView r15 Grid, Hotstring|Description  
LV_Add("", "wwa.", "ads function of WAIT for Window")
LV_Add("", "ifwa.", "ads function of WAIT for POSSIBLE Window")
LV_Add("", "wba.", "ads function of Waiting for ENABLED Button")
LV_Add("", "wbv.", "ads function of Waiting for VISSIBLE Button")
LV_Add("", "wa.", "ads function of ACTIVATE Window")
LV_Add("", "", "")
LV_Add("", "click.", "ads function of CLICK on Button")
LV_Add("", "close.", "ads function of CLOSE a Window")
LV_Add("", "focus.", "ads function of FOCUS on Button")
LV_Add("", "", "")
LV_Add("", "wms.", "ads function of SELECT Menu")
LV_Add("", "sn.", "ads function of SEND some Text/Keys")
LV_Add("", "sl.", "ads function of HOLD for some period of time")
LV_Add("", "msg.", "ads function of pop-up some MESSAGE box")
LV_Add("", "", "")
Loop % LV_GetCount()
{
   LV_Modify(A_Index, "AutoHdr")  ; Select each row whose first field contains the filter-text.
}
;List of commands in user interface FINISH

YPos += 50
GuiBottom := SizeBottom - YPos - 1
TitlePos := YPos - 21
ButtonPos := YPos - 25
Gui, 1: Font, w700 c000000
Gui, 1: Font, c000000
Gui, 1: Add, Button, w60 h30, &Exit app
Gui, 1: +AlwaysOnTop -Sysmenu +Caption +ToolWindow +Border
Gui, 1: Show, x1300 y150, Replace Script 1.5 - by TripleSence
return



2Button+:
Gui, 2: Destroy
Gui, 1: Show, NoActivate x1300 y300, Replace Script 1.5 - by TripleSence
return

ButtonExitapp:
2ButtonX:
ExitApp
;User interface FINISH

;Hotstrings START

;Hotstring that prepares code for "activate specific window" function START
:*:wa.::
(
;activate specific window START
winname := "WindowName" ;window name/class ;window name/class
btnname := "ButtonName" ;button class/text ;button class/text
Window.activate() ;function that activates specific window and then specific button
;activate specific window FINISH


)
;Hotstring that prepares code for "activate specific window" function FINISH


;Hotstring that prepares code for "waiting for visible button" function START
:*:wbv.::
(
;waiting for visible button START
winname := "WindowName" ;window name/class
btnname := "ButtonName" ;button class/text
Button.wait_visible() ; function that waits for specific visible button
;waiting for visible button FINISH


)
;Hotstring that prepares code for "waiting for visible button" function FINISH


;Hotstring that prepares code for "waiting for active button" function START
:*:wba.::
(
;waiting for active button START
winname := "WindowName" ;window name/class
btnname := "ButtonName" ;button class/text
Button.wait_active() ;function that waits for specific active button
;waiting for active button FINISH


)
;Hotstring that prepares code for "waiting for active button" function FINISH


;Hotstring that prepares code for "wait for possible window" function START
:*:ifwa.::
(
;wait for possible window START
winname := "WindowName" ;window name/class
btnname := "ButtonName" ;button class/text
timeifwin := "SLEEPTIME" ;how much time to wait for possible window, for example "300" (0.3 miliseconds)
Window.possible() ;function that waits for possible window and button inside and click on it
;wait for possible window FINISH


)
;Hotstring that prepares code for "wait for possible window" function FINISH


;Hotstring that prepares code for "wait for specific window" function START
:*:wwa.::
(
;wait for specific window START
winname := "WindowName" ;window name/class
btnname := "ButtonName" ;button class/text
Window.wait() ;function that waits for specific window and then specific button inside of it
;wait for specific window FINISH


)
;Hotstring that prepares code for "wait for specific window" function FINISH


;Hotstring that prepares code for "hold for sometime" function START
:*:sl.::
(
;hold for sometime START
sleeptime := "SLEEPTIME" ;time to wait. put a number, for example "300" (0.3 miliseconds)
Wait() ;function that holds script performance for special period of time
;hold for sometime FINISH


)
;Hotstring that prepares code for "hold for sometime" function FINISH


;Hotstring that prepares code for "click on specific button" function START
:*:click.::
(
;click on specific button START;
winname := "WindowName" ;window name/class
btnname := "ButtonName" ;button class/text
Button.click() ;function that clicks on specific button in specific window
;click on specific button FINISH;


)
;Hotstring that prepares code for "click on specific button" function FINISH


;Hotstring that prepares code for "close specific window" function START
:*:close.::
(
;close specific window START
winname := "WindowName" ;window name/class
Window.close() ;function that closes specific window 
;close specific window FINISH


)
;Hotstring that prepares code for "close specific window" function FINISH


;Hotstring that prepares code for "focus on specific button" function START
:*:focus.::
(
;focus on specific button START
winname := "WindowName" ;window name/class
btnname := "ButtonName" ;button class/text
Button.focus() ;function that focuses cursor on specific button in specific window
;focus on specific button FINISH


)
;Hotstring that prepares code for "focus on specific button" function FINISH


;Hotstring that prepares code for "select specific menu" function START
:*:wms.::
(
;select specific menu START
winname := "WindowName" ;window name/class
menu1 := "MenuIteamName" ;menu item name, for example "File"
submenu1 := "SubMenuItemName" ;submenu item name, for example "New"
menu_select() ;finction that selects specific item of menu of the specific window
;select specific menu FINISH


)
;Hotstring that prepares code for "select specific menu" function FINISH


;Hotstring that prepares code for "send some text or keys combination" function START
:*:sn.::
(
;send some text or keys combination START
text_keys := "SomeText" ;text or keys compination to send, for example "Hi!" or "^m" (Ctr+M combination)
Send() ;function that sends some text or keys combination
;send some text or keys combination FINISH


)
;Hotstring that prepares code for "send some text or keys combination" function FINISH


;Hotstring that prepares code for "Show message box with text" function START
:*:msg.::
(
;Show message box with text START
messagetext := "TextHere" ;text to show in message box
ShowMessage() ;function that shows message box with specific text inside
;Show message box with text FINISH


)
;Hotstring that prepares code for "Show message box with text" function FINISH
;Hotstrings FINISH
Attachments
FastCode AHK Library 1.5.zip
Zip file contains FastCode library, Replace.AHK file and Example file
(4.44 KiB) Downloaded 22 times
Replace GUI.JPG
Screenshot of Replace.AHK Gui
Replace GUI.JPG (34.45 KiB) Viewed 523 times
Last edited by MostWanted on 15 Aug 2022, 02:25, edited 11 times in total.

kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: FastCode library - boosting speed of coding

Post by kazhafeizhale » 08 Aug 2022, 11:32

you can try this, a snippet management tool
https://github.com/kazhafeizhale/super-command

User avatar
MostWanted
Posts: 11
Joined: 03 Jan 2021, 07:46
Contact:

Re: FastCode library - boosting speed of coding

Post by MostWanted » 09 Aug 2022, 07:02

kazhafeizhale wrote:
08 Aug 2022, 11:32
you can try this, a snippet management tool
https://github.com/kazhafeizhale/super-command
Hey, thx for comment. FastCode is something else, from what you shared. It's similar on some point, but general idea is different.

Post Reply

Return to “Scripts and Functions (v1)”