[Tool] My Functions v1.0.2

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

[Tool] My Functions v1.0.2

08 Sep 2021, 14:58

This script is a sort of lib / dir for functions / classes / etc.

Simply save a function (code) and recall that code easily in the future.
To make things even easier, there is a built in search filter, you just type out a few chars of your function and the list will get filtered to match.

Current GUI:
.
20210908151640.png
20210908151640.png (43.99 KiB) Viewed 1057 times
.

LEGEND:

1. Press this button to toggle the window from full height down to a small strip and back.

2. A DDL containing every saved function.

3. The list filter. Type here to see a filtered function list.

4. These buttons clipboard data / open function files / save the current function.

5. Edit locks. Toggle these checkboxes to block / allow input in the edit controls.

6. The name of the current function. This is also the name of the file for the functions (+Appendage).

7. This is where you add an example CALL of your function. Having this makes life a bit easier. Just clip it and paste it.

8. This is where you type / paste / drop-file your code. You can also add comments here, the notes section is for extra notes, not just simple code comments.

9. This is where you type / paste / drop-file notes about your code. The notes are for more than simple comments that you would normally have with your code. If you don't get it, don't use it.

10. Type out some code here and test your function on the fly.

11. *TRY* to run the code you wrote (includes your function code)


GETTING STARTED
-----------------------------------

1. Run the script
2. Fill out the edits and save.
3. goto step 2

TIPS
---------------------------------------

1. You can drag and drop files into the FUNCTION EDIT and the NOTES EDIT



CODE
----------------------------------------

Code: Select all

/*
Written By: Hellbent
Script Name: My Functions Tool 
Current Version: v1.0.2 (July 10th, 2023)
Date Started: Sept 6th, 2021
Last Edit: July 10th, 2023
Notes: 
; Added a clear all button to clear all of the edit controls.
; Can now right click on the menu to have it be converted from the main window to a small tab on the left side of the screen which can in turn be right clicked on to toggle back to the main window.
; Moved the save button.
*/

#SingleInstance, Force
SetBatchLines, -1
#NoEnv
DetectHiddenWindows, On

Main.SetUp()
OnMessage( 0x201 , Func( "ClickEvent" ).Bind( Main ) )

return
GuiClose:
	ExitApp

GuiContextMenu:	
	Gui.Hide( 1 )
	Gui, 11:New, +AlwaysOnTop -Caption +ToolWindow
	Gui, 11:Color, 4499ff
	Gui, 11:Show, x0 y300 w10 h30
	return

11GuiContextMenu:
	Gui.Show()
	Gui, 11:Destroy
	return

GuiDropFiles:
	Main.DropFile( A_GuiEvent )
	return

ClickEvent( Main , w , l , msg , hwnd ){
	if( Main.FilterActive &&  hwnd != Main.FilterListBox && hwnd != Main.FunctionListFilterEdit ){
		Gui.Hide( 2 )
		Main.FilterActive := 0
		Gui.Set( Main.FunctionListFilterEdit , "" )
	}
}

;*********************************************************************************************************************************************************************************

class Main	{
	
	Setup(){
		local fn , Index , po , cpo 
		This.FunctionsListPath := A_ScriptDir "\MyFunctions\*.ahk"
		This.FolderPath := A_ScriptDir "\MyFunctions"
		This.FileExt := "_MYFUNC.ahk"
		This.TestPath := A_ScriptDir "\MyFunctions\MYFUNCTIONSTESTSCRIPT.AHK"
		IfNotExist, % This.FolderPath
			File.CreateFolder( This.FolderPath )
		This.Tog := 0
		This.Selected := 1 
		This.SaveKeyList := [ "FunctionName" , "Prototype" , "Function" , "Notes" ] 
		This.FunctionName := "" 
		This.Prototype := "" 
		This.Function := "" 
		This.Notes := "" 
		This.Handles := [] 
		This.GuiHwnd := Gui.New( "+AlwaysOnTop +Resize" )
		Gui.Margin( 10 , 5 )
		This.Handles[ ( This.SizeToggleButton := Gui.Add( "Button" , "xm ym w60" , "+/- Size" ) ) ] := "SizeToggleButton" 
		This._Bind( "_ToggleSize" ,  This.SizeToggleButton )
		This.Handles[ ( This.FunctionListDDL := Gui.Add( "DDL" , "x+10 ym w300 r25" ) ) ] := "FunctionListDDL" 
		This._Bind( "_SelectItem" ,  This.FunctionListDDL )
		Gui.Set( This.FunctionListDDL , This.FunctionList := File.GetFunctionsList( This.FunctionsListPath ) )
		Gui.Set( This.FunctionListDDL , This.Selected , "Choose" )
		This.Handles[ ( This.FunctionListFilterEdit := Gui.Add( "Edit" , "x+10 w300 r1" ) ) ] := "FunctionListFilterEdit" 
		This._Bind( "_Filter" ,  This.FunctionListFilterEdit )
		This.Handles[ ( This.ClipboardFunctionButton := Gui.Add( "Button" , "x+10 r1" , "Clipboard Function" ) ) ] := "ClipboardFunctionButton" 
		This._Bind( "_Clip" ,  This.ClipboardFunctionButton )
		This.Handles[ ( This.ClipboardPrototypeButton := Gui.Add( "Button" , "x+10 r1" , "Clipboard Prototype" ) ) ] := "ClipboardPrototypeButton" 
		This._Bind( "_Clip" ,  This.ClipboardPrototypeButton )
		This.Handles[ ( This.OpenEditButton := Gui.Add( "Button" , "x+10 r1" , "Open in Editor" ) ) ] := "OpenEditButton"
		This._Bind( "_RunEdit" ,  This.OpenEditButton )
		Gui.Show("AutoSize Hide")
		This.TogHeight := Gui.Position( This.GuiHwnd )["H"] - 35
		Gui.Margin( 10 , 10 )
		This.Handles[ ( This.UnlockAllCheckbox := Gui.Add( "Checkbox" , "xm y+30" , "Edit All" ) ) ] := "UnlockAllCheckbox"
		This._Bind( "_UnlockAll" ,  This.UnlockAllCheckbox )
		This.Handles[ ( This.ClearAllButton := Gui.Add( "Button" , "x+10 yp-3 w120 +Disabled" , "Clear All" )  ) ] := "ClearAllButton"
		This._Bind( "_ClearAllEdits" ,  This.ClearAllButton )
		This.Handles[ ( This.SaveButton := Gui.Add( "Button" , "x+10 r1" , "Save" ) ) ] := "SaveButton"
		This._Bind( "_Save" ,  This.SaveButton )
		Gui.Add( "Text" , "xm y+20 w100" , "Function Name:" )
		This.Handles[ ( This.FunctionNameEdit := Gui.Add( "Edit" , "x+10 yp-4 w800 r1 Disabled" ) ) ] := "FunctionNameEdit"
		This.Handles[ ( This.FunctionNameCheckBox := Gui.Add( "CheckBox" , "x+10 yp+4 r1" , "Edit" ) ) ] := "FunctionNameCheckBox"
		This._Bind( "_UnlockEdit" ,  This.FunctionNameCheckBox )
		Gui.Add( "Text" , "xm y+20 w100" , "Call Prototype:" )
		This.Handles[ ( This.PrototypeEdit := Gui.Add( "Edit" , "x+10 yp-4 w800 r1 Disabled" ) ) ] := "PrototypeEdit"
		This.Handles[ ( This.PrototypeCheckBox := Gui.Add( "CheckBox" , "x+10 yp+4 r1" , "Edit" ) ) ] := "PrototypeCheckBox"
		This._Bind( "_UnlockEdit" ,  This.PrototypeCheckBox )
		Gui.Add( "Text" , "xm y+20 w100" , "Function Definition:" )
		This.Handles[ ( This.FunctionEdit := Gui.Add( "Edit" , "xm y+10 w1000 r15 WantTab -Wrap Disabled" ) ) ] := "FunctionEdit"
		This.Handles[ ( This.FunctionCheckBox := Gui.Add( "CheckBox" , "x+10 yp+4 r1" , "Edit" ) ) ] := "FunctionCheckBox"
		This._Bind( "_UnlockEdit" ,  This.FunctionCheckBox )
		Gui.Add( "Text" , "xm  w100" , "Notes:" )
		This.Handles[ ( This.NotesEdit := Gui.Add( "Edit" , "xm y+10 w1000 r6 WantTab -Wrap Disabled" ) ) ] := "NotesEdit"
		This.Handles[ ( This.NotesCheckBox := Gui.Add( "CheckBox" , "x+10 yp+4 r1" , "Edit" ) ) ] := "NotesCheckBox"
		This._Bind( "_UnlockEdit" ,  This.NotesCheckBox )
		This._SelectItem()
		Gui.Add( "Text" , "xm  w100" , "Test Code:" )
		This.Handles[ ( This.CommandEdit := Gui.Add( "Edit" , "xm y+10 w1000 r7 WantTab -Wrap" ) ) ] := "CommandEdit"
		This.Handles[ ( This.CommandButton := Gui.Add( "Button" , "x+10 yp" , "Run" ) ) ] := "CommandButton"
		This._Bind( "_RunTestCode" ,  This.CommandButton )
		Gui.Show( "AutoSize xCenter yCenter" , "MyFunctions" )
		This.FilterGuiHwnd := Gui.New( "+Owner1 -Caption" , 2 )
		Gui.Color( "22262a" , "22262a" , 2 )
		Gui.Margin( 0 , 0 , 2 )
		This.Handles[ ( This.FilterListBox := Gui.Add( "ListBox" , "cWhite xm ym w300 r25 " , "Item 1||item 2|" , 2 )  ) ] := "FilterListBox"
		This._Bind( "_SelectFilterItem" ,  This.FilterListBox , 2 )
	}
	
	_Bind( Method , Control , Name := 1 ){
		local fn := This[ Method ].Bind( This )
		Gui.Set( Control , fn , "+g" , Name )
	}
	
	_ClearAllEdits(){
		for k , v in This	{
			if( instr( k , "Edit" ) && !InStr( k , "Open" ) ){
				Gui.Set( This[ k ] , "" )
			}
		}
	}
	
	_ToggleSize(){
		if( This.Tog := !This.Tog ){
			Gui.Show( "h" This.TogHeight )
		}else{
			Gui.Show( "AutoSize" )
		}
	}
	
	_RunTestCode(){
		File.Write( "OnExit, MYCLEAR`n`r" Gui.Get( This.CommandEdit ) "`n`n`r" This.Function  "`n`rMYCLEAR:`n`tFileDelete, % A_ScriptFullPath`n`texitapp" , This.TestPath )
		try	{
			run, % This.TestPath
		}
	}
	
	DropFile( path ){
		MouseGetPos,,,, ctrl , 2
		if( ctrl = This.FunctionEdit ){
			Gui.Set( This.FunctionEdit , File.Read( path ) )
		}else if( ctrl = This.NotesEdit ){
			Gui.Set( This.NotesEdit , File.Read( path ) )
		}
	}
	
	_SelectFilterItem(){
		This.FunctionName := Gui.Get( This.FilterListBox ,,2)
		Gui.Set( This.FunctionListFilterEdit , "" )
		This._SetDDL()
		This._SelectItem()
		Gui.Hide( 2 )
		This.FilterActive := 0
	}
	
	_Filter(){
		local value , ta , oL := "|"
		value := Gui.Get( This.FunctionListFilterEdit )
		if( value = "" ){
			Gui.Hide( 2 )
			return
		}
		This.FilterActive := 1
		po := Gui.Position( This.GuiHwnd )
		Gui.Show( "x" po.X + 328 + 60 + 10 " y" po.Y + 65 " NA", , 2)
		ta := StrSplit( This.FunctionList , "|" )
		loop, % ta.Length() - 1	{
			if( instr( ta[ A_Index + 1 ] , value ) )
				oL .= ta[ A_Index + 1 ] "|"
		}
		Gui.Set( This.FilterListBox , oL ,, 2 )
	}
	
	_Clip(){
		MouseGetPos,,,, ctrl , 2
		( This.Handles[ ctrl ] = "ClipboardFunctionButton" ) ? ( Clipboard := This.Function ) : ( Clipboard := This.Prototype )
	}
	
	_RunEdit(){
		Try	
			Run, % "edit " This.FolderPath "\" This.FunctionName "_MYFUNC.ahk"
	}
	
	_UnlockAll(){
		Loop, % This.SaveKeyList.Length(){
			Gui.Set( This[ This.SaveKeyList[ A_Index ] "Edit" ] , "" , ( Gui.Get( This.UnlockAllCheckbox ) ) ? ( "Enable" ) : ( "Disable" ) )
			Gui.Set( This[ This.SaveKeyList[ A_Index ] "Checkbox" ] , ( Gui.Get( This.UnlockAllCheckbox ) ) ? ( 1 ) : ( 0 ) )
		}
		Gui.Set( This.ClearAllButton , "" , ( Gui.Get( This.UnlockAllCheckbox ) ) ? ( "Enable" ) : ( "Disable" ) )
	}
	
	_UnlockEdit(){
		MouseGetPos,,,, ctrl , 2 
		Gui.Set( This[ StrReplace( This.Handles[ ctrl ] , "CheckBox" , "Edit" ) ] , "" ,  ( Gui.Get( ctrl ) ) ? ( "Enable" ) : ( "Disable" )  )
	}
	
	_LockAllEdits(){
		Loop, % This.SaveKeyList.Length(){
			Gui.Set( This[ This.SaveKeyList[ A_Index ] "Edit" ] , "" , "Disable" )
			Gui.Set( This[ This.SaveKeyList[ A_Index ] "Checkbox" ] , 0 )
		}
		Gui.Set( This.UnlockAllCheckbox , 0 )
	}
	
	_Save(){
		local path , name
		This._GetEdits()
		if( !This.FunctionName ){
			Gui, 1:+OwnDialogs
			MsgBox, 262160, Missing, Function Name Required
			return
		}
		name := This.FunctionName "_MYFUNC.ahk"
		path := This.FolderPath "\" name
		File.Write( ";‡`n/*`n" This.Notes "`n*/`n`r" , path )
		File.Append( ";‡" This.Prototype "`n`r" , path )
		File.Append( ";‡`n" This.Function "`n`r" , path )
		This._SetDDL()
		This._LockAllEdits()
	}
	
	_GetEdits(){
		Loop, % This.SaveKeyList.Length()
			This[ This.SaveKeyList[ A_Index ] ] := Gui.Get( This[ This.SaveKeyList[ A_Index ] "Edit" ] )
	}
	
	_SetEdits(){
		Loop, % This.SaveKeyList.Length()
			Gui.Set( This[ This.SaveKeyList[ A_Index ] "Edit" ] , This[ This.SaveKeyList[ A_Index ] ] )
	}
	
	_SelectItem(){
		local path , ta
		This.FunctionName := Gui.Get( This.FunctionListDDL )
		path := This.FolderPath "\" This.FunctionName This.FileExt 
		ta := StrSplit( File.Read( path ) , "‡" )
		This.Notes := Trim( StrReplace( StrReplace( Trim( ta[ 2 ] , ";" ) , "/*" , "" ) , "*/" , "" ) , "`n`r" )
		This.Prototype := Trim( ta[ 3 ] , "; `n`r" )
		This.Function := Trim( ta[ 4 ] , "`n`r" )
		This._SetEdits()
	}
	
	_SetDDL(){
		Gui.Set( This.FunctionListDDL , This.FunctionList := File.GetFunctionsList( This.FunctionsListPath ) )
		arr := StrSplit( This.FunctionList , "|" )
		Loop, % Arr.Length(){
			if( Arr[ A_Index ] = This.FunctionName ){
				Gui.Set( This.FunctionListDDL , A_Index - 1 , "Choose" )
			}
		}
	}
}
;*********************************************************************************************************************************************************************************
class Gui	{
	
	New( options , Name := 1 ){
		local hwnd
		Gui, % Name ":New", % Options " hwndhwnd"
		return hwnd
	}
	
	Add( Type , Options , Display := "" , Name := 1 ){
		local hwnd
		Gui, % Name ":Add", % Type, % Options " hwndhwnd", % Display
		return hwnd
	}
	
	Margin( x , y , Name := 1 ){
		Gui, % Name ":Margin", % x , % y
	}
	
	Font( Font , Options , Name := 1 ){
		Gui, % Name ":Font", % Options , % Font
	}
	
	Show( Options := "" , Title := "" , Name := 1 ){
		if( IsObject( Options ) ){
			cc := Options
			Options :=  "x" cc.X " y" cc.Y " w" cc.W " h" cc.H
		}
		Gui, % Name ":Show", % Options , % Title
	}
	
	Set( Control , Value := "" , Sub := "" , Name := 1 ){
		GuiControl, % Name ":" Sub , % Control , % Value
	}
	
	Get( Control , Sub := "" , Name := 1 ){
		local output 
		GuiControlGet, output , % Name ":" Sub , % Control
		return output
	}
	Position( hwnd ){
		local x , y , w , h 
		WinGetPos, x , y , w , h , % "ahk_id " hwnd
		return { X: x , Y: y , W: w , H: h }
	}
	
	Color( Color1 , Color2 := "ffffff" , Name := 1 ){
		Gui, % Name ":Color", % Color1 , % Color2
	}
	
	Hide( Name := 1 ){
		Gui, % Name ":Hide"
	}
	Destroy( Name := 1 ){
		Gui, % Name ":Destroy"
	}
}
;*********************************************************************************************************************************************************************************
class File	{
	
	GetFunctionsList( Path ){
		local fList := "|" 
		Loop, Files, % Path
		{
			if( instr( A_LoopFileName , "_MYFUNC" ) )
				fList .= StrReplace( strSplit( A_LoopFileName , "." )[1] , "_MYFUNC" , "" ) "|"
		}
		return fList
	}
	
	Read( path ){
		local output
		FileRead, output , % Path
		return output
	}
	
	Write( value , path ){
		File.Delete( path )
		File.Append( value , path )
	}
	
	Append( value , path ){
		FileAppend, % value , % path
	}
	
	Delete( path ){
		FileDelete, % path
	}
	
	CreateFolder( Path ){
		FileCreateDir, % Path
	}
}
;*********************************************************************************************************************************************************************************
Last edited by Hellbent on 10 Jul 2023, 01:21, edited 3 times in total.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: [Tool] My Functions v1.0.1

10 Jul 2023, 00:52

*Update*
Added a clear all button
Moved the save button.
Can now toggle into a small tab that sits on the right on the main monitor by right clicking on the window and tab. ( the position of the tab can be edited )


.
my functions.gif
my functions.gif (866.4 KiB) Viewed 458 times
.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Lamron750 and 244 guests