v2 Command Line Generator

Post your working scripts, libraries and tools.
MarcusK35
Posts: 24
Joined: 30 Mar 2015, 15:06

v2 Command Line Generator

Post by MarcusK35 » 26 May 2022, 13:24

I came up with this script to help convert v1 command lines into v2 code and to generate v2 code from a standard command line. To convert a v1 command line, enter any declared variables into the "Variable" field and then copy and paste the command line into the 2nd field. The logic should detect all the AHK built-in variables (I think) so there is no need to include them in the "Variables" field. When generating v2 command lines from scratch, just type in the command in the 2nd field and only include quotes that will be explicitly used in the command.

Hopefully this description is clear and the script is helpful, and if not, just respond. Cheers!

Code: Select all

#Warn ;Enable warnings to assist with detecting common errors.
#SingleInstance Force
#NoTrayIcon

MainGUI := Gui(, "v2 - Command Line Generator")
MainGUI.SetFont("s9 w400", "Verdana")
MainGUI.Add("Text", "x20 y20 w50", "Variables:")
VariableField := MainGUI.Add("Edit", "R2 x100 y10 w520")
VariableField.OnEvent("Change", GenerateCmdLine)
MainGUI.SetFont("s10 w400", "Verdana")
RunRadio := MainGUI.Add("Radio", "x20 y60 w50", "Run")
RunRadio.OnEvent("Click", GenerateCmdLine)
RunWaitRadio := MainGUI.Add("Radio", "x20 y90 w50", "RunWait")
RunWaitRadio.OnEvent("Click", GenerateCmdLine)
CmdLineField := MainGUI.Add("Edit", "R4 x100 y52 w520")
CmdLineField.OnEvent("Change", GenerateCmdLine)
MainGUI.SetFont("s10 w400", "Verdana")
MaxCheckbox := MainGUI.Add("Checkbox", "x630 y55 w50", "Max")
MaxCheckbox.OnEvent("Click", MaxOption)
MinCheckbox := MainGUI.Add("Checkbox", "x630 y75 w50", "Min")
MinCheckbox.OnEvent("Click", MinOption)
HideCheckbox := MainGUI.Add("Checkbox", "x630 y95 w50", "Hide")
HideCheckbox.OnEvent("Click", HideOption)
MainGUI.Add("Edit", "R4 ReadOnly x100 y132 w520")
CopyCmdLineBtn := MainGUI.Add("Button", "x630 y135 w70 h66", "Copy")
CopyCmdLineBtn.OnEvent("Click", CopyCmdLine)
CopyCmdLine(*)
{
	A_ClipBoard := ControlGetText("Edit3", "v2 - Command Line Generator")
	
	If FileExist(A_Temp "\CmdLineGen.txt")
		FileDelete A_Temp "\CmdLineGen.txt"
		
	Backup := (ControlGetText("Edit1") "`n" ControlGetText("Edit2") "`n" ControlGetChecked("Button1") "`n" ControlGetChecked("Button2") "`n" ControlGetChecked("Button3") "`n" ControlGetChecked("Button4") "`n" ControlGetChecked("Button5"))
		
	FileAppend Backup, A_Temp "\CmdLineGen.txt"
}
MainGUI.SetFont("s9 w400", "Verdana")
SB := MainGUI.Add("StatusBar")
MainGUI.Opt("-MinimizeBox -MaximizeBox +AlwaysOnTop")
MainGUI.Show("h240")

ControlSetChecked 1, "Button1", "v2 - Command Line Generator"

If FileExist(A_Temp "\CmdLineGen.txt")
{
	Loop Read, A_Temp "\CmdLineGen.txt"
	{
		If (A_Index = 1 Or A_Index = 2)
			ControlSetText(A_LoopReadLine, "Edit" A_Index, "v2 - Command Line Generator")
		Else If (A_Index = 3)
			ControlSetChecked A_LoopReadLine, "Button1", "v2 - Command Line Generator"
		Else If (A_Index = 4)
			ControlSetChecked A_LoopReadLine, "Button2", "v2 - Command Line Generator"
		Else If (A_Index = 5)
			ControlSetChecked A_LoopReadLine, "Button3", "v2 - Command Line Generator"
		Else If (A_Index = 6)
			ControlSetChecked A_LoopReadLine, "Button4", "v2 - Command Line Generator"
		Else If (A_Index = 7)
			ControlSetChecked A_LoopReadLine, "Button5", "v2 - Command Line Generator"
	}	
}

CmdLine := ControlGetText("Edit2", "v2 - Command Line Generator")

If (CmdLine != "")
	GenerateCmdLine

Loop
{
	If !WinExist("v2 - Command Line Generator")
		Break
}

If FileExist(A_Temp "\CmdLineGen.txt")
	FileDelete A_Temp "\CmdLineGen.txt"
	
FileAppend CustomVarsBU "`n" AHKCmdLineBU "`n" RunCheckedBU "`n" RunWaitCheckedBU "`n" MaxCheckedBU "`n" MinCheckedBU "`n" HideCheckedBU, A_Temp "\CmdLineGen.txt"

ExitApp

MaxOption(*)
{
	If MaxChecked := ControlGetChecked("Button3", "v2 - Command Line Generator") = 1
	{
		ControlSetChecked 0, "Button4", "v2 - Command Line Generator"
		ControlSetChecked 0, "Button5", "v2 - Command Line Generator"
	}
	
	GenerateCmdLine
}

MinOption(*)
{
	If MinChecked := ControlGetChecked("Button4", "v2 - Command Line Generator") = 1
	{
		ControlSetChecked 0, "Button3", "v2 - Command Line Generator"
		ControlSetChecked 0, "Button5", "v2 - Command Line Generator"
	}
	
	GenerateCmdLine
}

HideOption(*)
{
	If MinChecked := ControlGetChecked("Button5", "v2 - Command Line Generator") = 1
	{
		ControlSetChecked 0, "Button3", "v2 - Command Line Generator"
		ControlSetChecked 0, "Button4", "v2 - Command Line Generator"
	}
	
	GenerateCmdLine
}

GenerateCmdLine(*)
{
	SB.SetText("")

	BuiltInVariables := ("A_Args,A_WorkingDir,A_InitialWorkingDir,A_ScriptDir,A_ScriptName,A_ScriptFullPath,A_ScriptHwnd,A_LineNumber,A_LineFile,A_ThisFunc,A_AhkVersion,A_AhkPath,A_IsCompiled,A_YYYY,A_MM,A_DD,A_MMMM,A_MMM,A_DDDD,A_DDD,A_WDay,A_YDay,A_YWeek,A_Hour,A_Min,A_Sec,A_MSec,A_Now,A_NowUTC,A_TickCount,A_IsSuspended,A_IsPaused,A_IsCritical,A_ListLines,A_TitleMatchMode,A_TitleMatchModeSpeed,A_DetectHiddenWindows,A_DetectHiddenText,A_FileEncoding,A_SendMode,A_SendLevel,A_StoreCapsLockMode,A_KeyDelay,A_KeyDuration,A_KeyDelayPlay,A_KeyDurationPlay,A_WinDelay,A_ControlDelay,A_MenuMaskKey,A_MouseDelay,A_MouseDelayPlay,A_DefaultMouseSpeed,A_CoordModeToolTip,A_CoordModePixel,A_CoordModeMouse,A_CoordModeCaret,A_CoordModeMenu,A_RegView,A_TrayMenu,A_AllowMainWindow,A_IconHidden,A_IconTip,A_IconFile,A_IconNumber,A_TimeIdle,A_TimeIdlePhysical,A_TimeIdleKeyboard,A_TimeIdleMouse,A_ThisHotkey,A_PriorHotkey,A_PriorKey,A_TimeSinceThisHotkey,A_TimeSincePriorHotkey,A_EndChar,A_MaxHotkeysPerInterval,A_HotkeyInterval,A_HotkeyModifierTimeout,A_ComSpec,A_Temp,A_OSVersion,A_Is64bitOS,A_PtrSize,A_Language,A_ComputerName,A_UserName,A_WinDir,A_ProgramFiles,A_AppData,A_AppDataCommon,A_Desktop,A_DesktopCommon,A_StartMenu,A_StartMenuCommon,A_Programs,A_ProgramsCommon,A_Startup,A_StartupCommon,A_MyDocuments,A_IsAdmin,A_ScreenWidth,A_ScreenHeight,A_ScreenDPI,A_Clipboard,A_Cursor,A_EventInfo,A_LastError,A_Index,A_LoopFileName,A_LoopFileExt,A_LoopFilePath,A_LoopFileFullPath,A_LoopFileShortPath,A_LoopFileShortName,A_LoopFileDir,A_LoopFileTimeModified,A_LoopFileTimeCreated,A_LoopFileTimeAccessed,A_LoopFileAttrib,A_LoopFileSize,A_LoopFileSizeKB,A_LoopFileSizeMB,A_LoopRegName,A_LoopRegType,A_LoopRegKey,A_LoopRegTimeModified,A_LoopReadLine,A_LoopField")
		
	CustomVars := ControlGetText("Edit1", "v2 - Command Line Generator")

	If (CustomVars != "")
	{
		IllegalCharsFound := 0
		
		IllegalChars := "``~!@#$%^&*()=+{}[]\|`;`:`'`"`,`.<>/?1234567890 " ;"
		
		Loop Parse, IllegalChars
		{
			If FoundPos := InStr(CustomVars, A_LoopField) = 1
			{
				SB.SetText("Illegal first character `"" A_LoopField "`" in Variables field - remove to continue.")
				
				IllegalCharsFound := 1
				
				Break
			}
		}
		
		If (IllegalCharsFound = 1)
		{
			Global CustomVarsBU := CustomVars
			
			Return
		}
		
		IllegalChars := "``~!@#$%^&*()=+{}[]\|`;`:`'`"<>/? " ;"
		CharsFound := ""

		Loop Parse, IllegalChars
		{
			If FoundPos := InStr(CustomVars, A_LoopField) > 0
			{
				CharsFound := (CharsFound "," A_LoopField)
				
				IllegalCharsFound := 1
			}
		}
		
		If (IllegalCharsFound = 1)
		{
			CharsFound := SubStr(CharsFound, 2)
			
			SB.SetText("Illegal character(s) `"" CharsFound "`" in Variables field - remove to continue.")
			
			Global CustomVarsBU := CustomVars
			
			Return
		}
		
		If FoundPos := InStr(CustomVars, ",,") > 0
		{
			SB.SetText("Consecutive commas found in Variables field - remove to continue.")
			
			Global CustomVarsBU := CustomVars
			
			Return
		}
	}
	
	Global CustomVarsBU := CustomVars
	
	If (FoundPos := InStr(CustomVars, ",", , , -1) = Length := StrLen(CustomVars))
	{
		Global CustomVarsBU := CustomVars
		
		CustomVars := SubStr(CustomVars, 1, Length - 1)
	}	

	Loop Parse, BuiltInVariables, ","
	{
		If VarCheck := InStr(CustomVars, A_LoopField) > 0
		{
			CustomVars := StrReplace(CustomVars, A_LoopField ",")
			CustomVars := StrReplace(CustomVars, "," A_LoopField)
			CustomVars := StrReplace(CustomVars, A_LoopField)
		}
	}
		
	If CustomVars != ""
		VariableList := (BuiltInVariables . "," CustomVars)
	Else
		VariableList := BuiltInVariables
	
	Global AHKCmdLineBU := ControlGetText("Edit2", "v2 - Command Line Generator")
	AHKCmdLine := ControlGetText("Edit2", "v2 - Command Line Generator")
	AHKCmdLine := StrReplace(AHKCmdLine, "%comspec%", "A_ComSpec")
	AHKCmdLine := StrReplace(AHKCmdLine, "``")
	
	FoundVariables := ""
		
	Loop Parse, VariableList, ","
	{
		If VariableExist := InStr(AHKCmdLine, A_LoopField) > 0
			FoundVariables := (FoundVariables A_LoopField ",")
	}

	StrLength := StrLen(FoundVariables)
	FoundVariables := SubStr(FoundVariables, 1, StrLength - 1)
	
	StartingPos := 1
	
	Loop
	{
		QuotePos := InStr(AHKCmdLine, "`"", , StartingPos) ;"
		
		If (QuotePos = 0)
			Break
		
		BeforeQuote := SubStr(AHKCmdLine, 1, QuotePos - 1)
		AfterQuote := SubStr(AHKCmdLine, QuotePos + 1)
		BeforeQuote := (BeforeQuote . "```"") ;"

		StartingPos := (QuotePos + 2)
		
		AHKCmdLine := (BeforeQuote AfterQuote)
	}

	Loop Parse, FoundVariables, ","
	{
		Variable := A_LoopField
		
		If FoundPos := InStr(AHKCmdLine, "%" A_LoopField "%") > 0
			AHKCmdLine := StrReplace(AHKCmdLine, "%" A_LoopField "%", "`" " A_LoopField " `"")
		Else
			AHKCmdLine := StrReplace(AHKCmdLine, A_LoopField, "`" " A_LoopField " `"")
	}
	
	VariableFirst := 0
	
	Loop Parse, FoundVariables, ","
	{
		If VarFirstCheck := InStr(AHKCmdLine, "`" " A_LoopField) = 1 ;"
		{
			AHKCmdLine := StrReplace(AHKCmdLine, "`" " A_LoopField, A_LoopField, , , 1) ;"
			
			VariableFirst := 1
			
			Break
		}
	}
	
	VariableLast := 0
	
	Loop Parse, FoundVariables, ","
	{
		CmdLineLength := StrLen(AHKCmdLine)
		VariableStr := (A_LoopField " `"") ;"
		VariableLength := StrLen(VariableStr) ;"
		VarLastCheck := InStr(AHKCmdLine, A_LoopField " `"", , , -1) ;"
		
		If (CmdLineLength = VarLastCheck + VariableLength - 1)
		{
			AHKCmdLine := SubStr(AHKCmdLine, 1, CmdLineLength - 2)
			
			VariableLast := 1
		
			Break
		}
	}

	Loop Parse, FoundVariables, ","
		AHKCmdLine := StrReplace(AHKCmdLine, " ```" " A_LoopField " ```" ", "```" " A_LoopField " ```"")

	Loop Parse, FoundVariables, ","
		AHKCmdLine := StrReplace(AHKCmdLine, A_LoopField " ```" ", A_LoopField " ```"") ;"
		
	Loop Parse, FoundVariables, ","
		AHKCmdLine := StrReplace(AHKCmdLine, " ```" " A_LoopField, "```" " A_LoopField) ;"
		
	If FoundPos := InStr(AHKCmdLine, "A_ComSpec `" /c") = 0 ;"
	{
		AHKCmdLine := ("A_ComSpec `" /c " AHKCmdLine) ;"
		
		VariableFirst := 1
	}

	If (VariableFirst = 1 And VariableLast = 0)
		AHKCmdLine := (AHKCmdLine "`"") ;"
	Else If (VariableFirst = 0 And VariableLast = 1)
		AHKCmdLine := ("`"" AHKCmdLine) ;"
	Else If (VariableFirst = 0 And VariableLast = 0 And AHKCmdLine != "")
		AHKCmdLine := ("`"" AHKCmdLine "`"") ;"
		
	If RunChecked := ControlGetChecked("Button1", "v2 - Command Line Generator") = 1
	{
		If (AHKCmdLine != "")
			AHKCmdLine := ("Run " AHKCmdLine)
		
		Global RunCheckedBU := 1
		Global RunWaitCheckedBU := 0
	}
	Else If RunWaitChecked := ControlGetChecked("Button2", "v2 - Command Line Generator") = 1
	{
		If (AHKCmdLine != "")
			AHKCmdLine := ("RunWait " AHKCmdLine)
	
		Global RunCheckedBU := 0
		Global RunWaitCheckedBU := 1
	}
		
	If MaxChecked := ControlGetChecked("Button3", "v2 - Command Line Generator") = 1
	{
		If (AHKCmdLine != "")
			AHKCmdLine := (AHKCmdLine ", `"Max`"")
		
		Global MaxCheckedBU := 1
		Global MinCheckedBU := 0
		Global HideCheckedBU := 0
	}
	Else If MinChecked := ControlGetChecked("Button4", "v2 - Command Line Generator") = 1
	{
		If (AHKCmdLine != "")
			AHKCmdLine := (AHKCmdLine ", `"Min`"")
		
		Global MaxCheckedBU := 0
		Global MinCheckedBU := 1
		Global HideCheckedBU := 0
	}
	Else If HideChecked := ControlGetChecked("Button5", "v2 - Command Line Generator") = 1
	{
		If (AHKCmdLine != "")
			AHKCmdLine := (AHKCmdLine ", `"Hide`"")
		
		Global MaxCheckedBU := 0
		Global MinCheckedBU := 0
		Global HideCheckedBU := 1
	}
	Else
	{
		AHKCmdLine := StrReplace(AHKCmdLine, ", `"Max`"")
		AHKCmdLine := StrReplace(AHKCmdLine, ", `"Min`"")
		AHKCmdLine := StrReplace(AHKCmdLine, ", `"Hide`"")
		
		Global MaxCheckedBU := 0
		Global MinCheckedBU := 0
		Global HideCheckedBU := 0
	}

	ControlSetText(AHKCmdLine, "Edit3", "v2 - Command Line Generator")
}

Esc::ExitApp
Attachments
image.png
image.png (21.34 KiB) Viewed 1408 times
Last edited by gregster on 26 May 2022, 16:39, edited 1 time in total.
Reason: Topic moved from 'AutoHotkey v2 Development'.

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: v2 Command Line Generator

Post by AHK_user » 26 May 2022, 15:29

@MarcusK35 I would advice you to check if the v2 script convertor is usefull for you.
https://github.com/mmikeww/AHK-v2-script-converter

This can convert most of the v1 code to v2, and seems to work perfectly for the run command.

2022-05-26 22_19_44-Quick Convertor V2.png
2022-05-26 22_19_44-Quick Convertor V2.png (20.23 KiB) Viewed 1392 times

MarcusK35
Posts: 24
Joined: 30 Mar 2015, 15:06

Re: v2 Command Line Generator

Post by MarcusK35 » 26 May 2022, 17:48

Interesting... I'll check it out. The one thing I will say is that mine will create v2 code from just a command line, which was the original plan for the script. I don't think the converter will do that. Keeping track of all the quotes drives me a bit nutty so I automated it.

MarcusK35
Posts: 24
Joined: 30 Mar 2015, 15:06

Re: v2 Command Line Generator

Post by MarcusK35 » 21 Jun 2022, 21:13

This is an updated, more robust version of the original that formats text/command lines from v1 to v2 and will automatically create command line code for v2. Enjoy!

Code: Select all

#Warn ;Enable warnings to assist with detecting common errors.
#SingleInstance Off
#NoTrayIcon

;Workflow buttons
;Button7 - Previous
;Button8 - Debug
;Button9 - Next
;Button10 - Stop
;Button11 - Clear

If WinExist("v2 - Command Line Generator")
{
	BackupSettings

	WinGetPos &X, &Y, , , "v2 - Command Line Generator"
	
	If (X < 0 And Y < 0)
	{
		Global X := 1
		Global Y := 1
	}

	PID := WinGetPID("v2 - Command Line Generator")
	
	ProcessClose PID
}
Else
{
	Global X := 1
	Global Y := 1
}

Global DebugClicked := 0
Global NextClicked := 0
Global PreviousClicked := 0
Global StopClicked := 0

MainGUI := Gui(, "v2 - Command Line Generator")
MainGUI.SetFont("s9 w400", "Verdana")
MainGUI.Add("Text", "x10 y20 w50", "Variables:")
VariableField := MainGUI.Add("Edit", "R2 x120 y10 w520")
VariableField.OnEvent("Change", GenerateCmdLine)
MainGUI.SetFont("s10 w400", "Verdana")
RunCheckbox := MainGUI.Add("Checkbox", "x10 y60 w50", "Run")
RunCheckbox.OnEvent("Click", RunSelect)
RunSelect(*)
{
	If ((IsChecked := ControlGetChecked("Button1", "v2 - Command Line Generator")) = 1)
	{
			Sleep 100
			
			ControlSetChecked(0, "Button2", "v2 - Command Line Generator")
	}
	
	GenerateCmdLine
}
RunWaitCheckbox := MainGUI.Add("Checkbox", "x10 y90 w50", "RunWait")
RunWaitCheckbox.OnEvent("Click", RunWaitSelect)
RunWaitSelect(*)
{
	If ((IsChecked := ControlGetChecked("Button2", "v2 - Command Line Generator")) = 1)
	{
			Sleep 100
			
			ControlSetChecked(0, "Button1", "v2 - Command Line Generator")
	}
	
	GenerateCmdLine
}
CmdLineField := MainGUI.Add("Edit", "R4 x120 y52 w520")
CmdLineField.OnEvent("Change", GenerateCmdLine)
MainGUI.SetFont("s10 w400", "Verdana")
MaxCheckbox := MainGUI.Add("Checkbox", "x650 y55 w50", "Max")
MaxCheckbox.OnEvent("Click", MaxOption)
MinCheckbox := MainGUI.Add("Checkbox", "x650 y75 w50", "Min")
MinCheckbox.OnEvent("Click", MinOption)
HideCheckbox := MainGUI.Add("Checkbox", "x650 y95 w50", "Hide")
HideCheckbox.OnEvent("Click", HideOption)
MainGUI.Add("Text", "x10 y140", "Working Dir/PID:")
WorkingDirField := MainGUI.Add("Edit", "R2 x120 y130 w520")
WorkingDirField.OnEvent("Change", GenerateCmdLine)
PIDField := MainGUI.Add("Edit", "R2 x650 y130 w70")
PIDField.OnEvent("Change", GenerateCmdLine)
MainGUI.Add("Text", "x10 y200", "Command Line:")
MainGUI.Add("Edit", "R4 ReadOnly x120 y177 w520")
CopyCmdLineBtn := MainGUI.Add("Button", "x650 y180 w70 h66", "Copy")
CopyCmdLineBtn.OnEvent("Click", CopyCmdLine)
CopyCmdLine(*)
{
	A_ClipBoard := ControlGetText("Edit5", "v2 - Command Line Generator")
}
MainGUI.Add("Text", "x10 y285", "Debug Line:")
MainGUI.Add("Edit", "R5 ReadOnly x120 y256 w520")
PreviousButton := MainGUI.Add("Button", "x260 y355 w70", "Previous")
PreviousButton.OnEvent("Click", PreviousButtonClicked)
PreviousButtonClicked(*)
{
	Global PreviousClicked := 1
}
DebugButton := MainGUI.Add("Button", "x340 y355 w70", "Debug")
DebugButton.OnEvent("Click", DebugButtonClicked)
DebugButtonClicked(*)
{
	Global DebugClicked := 1
	
	Loop 4
		ControlSetEnabled(0, "Edit" A_Index, "v2 - Command Line Generator")
		
	Loop 5
		ControlSetEnabled(0, "Button" A_Index, "v2 - Command Line Generator")

	ControlHide("Button8", "v2 - Command Line Generator")
	ControlShow("Button9", "v2 - Command Line Generator")
	ControlHide("Button11", "v2 - Command Line Generator")
	ControlShow("Button10", "v2 - Command Line Generator")
	ControlSetEnabled(1, "Button10", "v2 - Command Line Generator")
	
	
	GenerateCmdLine
}
NextButton := MainGUI.Add("Button", "x340 y355 w70", "Next")
NextButton.OnEvent("Click", NextButtonClicked)
NextButtonClicked(*)
{
	Global NextClicked := 1
}
StopButton := MainGUI.Add("Button", "x420 y355 w70", "Stop")
StopButton.OnEvent("Click", StopButtonClicked)
StopButtonClicked(*)
{
	Global StopClicked := 1
	Global DebugClicked := 0
	
	Loop 4
		ControlSetEnabled(1, "Edit" A_Index, "v2 - Command Line Generator")
		
	Loop 5
		ControlSetEnabled(1, "Button" A_Index, "v2 - Command Line Generator")

	ControlHide("Button9", "v2 - Command Line Generator")
	ControlShow("Button8", "v2 - Command Line Generator")
	ControlHide("Button10", "v2 - Command Line Generator")
	ControlShow("Button11", "v2 - Command Line Generator")
	ControlSetEnabled(0, "Button7", "v2 - Command Line Generator")
	ControlSetEnabled(0, "Button10", "v2 - Command Line Generator")
	ControlSetEnabled(1, "Button11", "v2 - Command Line Generator")
}
ClearButton := MainGUI.Add("Button", "x420 y355 w70", "Clear")
ClearButton.OnEvent("Click", ClearButtonClicked)
ClearButtonClicked(*)
{
	ControlSetText("", "Edit6", "v2 - Command Line Generator")
	
	ControlSetEnabled(0, "Button11", "v2 - Command Line Generator")
}
MainGUI.SetFont("s9 w400", "Verdana")
SB := MainGUI.Add("StatusBar")
MainGUI.Opt("-MaximizeBox +AlwaysOnTop")

If (X = 1 And Y = 1)
	MainGUI.Show("h420")
Else
	MainGUI.Show("x" X " y" Y " h420")


If FileExist(A_Temp "\CmdLineGen.txt")
{
	Loop Read, A_Temp "\CmdLineGen.txt"
	{
		If (A_Index <= 4)
			ControlSetText(A_LoopReadLine, "Edit" A_Index, "v2 - Command Line Generator")
		Else If (A_Index = 5)
			ControlSetChecked A_LoopReadLine, "Button1", "v2 - Command Line Generator"
		Else If (A_Index = 6)
			ControlSetChecked A_LoopReadLine, "Button2", "v2 - Command Line Generator"
		Else If (A_Index = 7)
			ControlSetChecked A_LoopReadLine, "Button3", "v2 - Command Line Generator"
		Else If (A_Index = 8)
			ControlSetChecked A_LoopReadLine, "Button4", "v2 - Command Line Generator"
		Else If (A_Index = 9)
			ControlSetChecked A_LoopReadLine, "Button5", "v2 - Command Line Generator"
	}	
}

ControlHide("Button9", "v2 - Command Line Generator")
ControlHide("Button11", "v2 - Command Line Generator")
ControlSetEnabled(0, "Button7", "v2 - Command Line Generator")
ControlSetEnabled(0, "Button10", "v2 - Command Line Generator")
ControlSetEnabled(0, "Button11", "v2 - Command Line Generator")

CmdLine := ControlGetText("Edit2", "v2 - Command Line Generator")

If (CmdLine != "")
	GenerateCmdLine

Loop
{
	If !WinExist("v2 - Command Line Generator")
		Break
}

BackupSettings

ExitApp

MaxOption(*)
{
	If (MaxChecked := ControlGetChecked("Button3", "v2 - Command Line Generator") = 1)
	{
		Sleep 100

		ControlSetChecked(0, "Button4", "v2 - Command Line Generator")
		ControlSetChecked(0, "Button5", "v2 - Command Line Generator")
	}
	
	GenerateCmdLine
}

MinOption(*)
{
	If (MinChecked := ControlGetChecked("Button4", "v2 - Command Line Generator") = 1)
	{
		Sleep 100

		ControlSetChecked(0, "Button3", "v2 - Command Line Generator")
		ControlSetChecked(0, "Button5", "v2 - Command Line Generator")
	}
	
	GenerateCmdLine
}

HideOption(*)
{
	If (MinChecked := ControlGetChecked("Button5", "v2 - Command Line Generator") = 1)
	{
		Sleep 100

		ControlSetChecked(0, "Button3", "v2 - Command Line Generator")
		ControlSetChecked(0, "Button4", "v2 - Command Line Generator")
	}
	
	GenerateCmdLine
}

GenerateCmdLine(*)
{
	SB.SetText("")
	AHKCmdLine := ""

	BuiltInVariables := ("A_Args,A_WorkingDir,A_InitialWorkingDir,A_ScriptDir,A_ScriptName,A_ScriptFullPath,A_ScriptHwnd,A_LineNumber,A_LineFile,A_ThisFunc,A_AhkVersion,A_AhkPath,A_IsCompiled,A_YYYY,A_MM,A_DD,A_MMMM,A_MMM,A_DDDD,A_DDD,A_WDay,A_YDay,A_YWeek,A_Hour,A_Min,A_Sec,A_MSec,A_Now,A_NowUTC,A_TickCount,A_IsSuspended,A_IsPaused,A_IsCritical,A_ListLines,A_TitleMatchMode,A_TitleMatchModeSpeed,A_DetectHiddenWindows,A_DetectHiddenText,A_FileEncoding,A_SendMode,A_SendLevel,A_StoreCapsLockMode,A_KeyDelay,A_KeyDuration,A_KeyDelayPlay,A_KeyDurationPlay,A_WinDelay,A_ControlDelay,A_MenuMaskKey,A_MouseDelay,A_MouseDelayPlay,A_DefaultMouseSpeed,A_CoordModeToolTip,A_CoordModePixel,A_CoordModeMouse,A_CoordModeCaret,A_CoordModeMenu,A_RegView,A_TrayMenu,A_AllowMainWindow,A_IconHidden,A_IconTip,A_IconFile,A_IconNumber,A_TimeIdle,A_TimeIdlePhysical,A_TimeIdleKeyboard,A_TimeIdleMouse,A_ThisHotkey,A_PriorHotkey,A_PriorKey,A_TimeSinceThisHotkey,A_TimeSincePriorHotkey,A_EndChar,A_MaxHotkeysPerInterval,A_HotkeyInterval,A_HotkeyModifierTimeout,A_ComSpec,A_Temp,A_OSVersion,A_Is64bitOS,A_PtrSize,A_Language,A_ComputerName,A_UserName,A_WinDir,A_ProgramFiles,A_AppData,A_AppDataCommon,A_Desktop,A_DesktopCommon,A_StartMenu,A_StartMenuCommon,A_Programs,A_ProgramsCommon,A_Startup,A_StartupCommon,A_MyDocuments,A_IsAdmin,A_ScreenWidth,A_ScreenHeight,A_ScreenDPI,A_Clipboard,A_Cursor,A_EventInfo,A_LastError,A_Index,A_LoopFileName,A_LoopFileExt,A_LoopFilePath,A_LoopFileFullPath,A_LoopFileShortPath,A_LoopFileShortName,A_LoopFileDir,A_LoopFileTimeModified,A_LoopFileTimeCreated,A_LoopFileTimeAccessed,A_LoopFileAttrib,A_LoopFileSize,A_LoopFileSizeKB,A_LoopFileSizeMB,A_LoopRegName,A_LoopRegType,A_LoopRegKey,A_LoopRegTimeModified,A_LoopReadLine,A_LoopField")
		
	CustomVars := ControlGetText("Edit1", "v2 - Command Line Generator")

	If (CustomVars != "")
	{
		IllegalCharsFound := 0
		
		IllegalChars := "``~!@#$%^&*()=+{}[]\|`;`:`'`"`,`.<>/?1234567890 " ;"
		
		Loop Parse, IllegalChars
		{
			If FoundPos := InStr(CustomVars, A_LoopField) = 1
			{
				SB.SetText("Illegal first character `"" A_LoopField "`" in Variables field - remove to continue.")
				
				IllegalCharsFound := 1
				
				Break
			}
		}
		
		If (IllegalCharsFound = 1)
			Return
		
		IllegalChars := "``~!@#$%^&*()=+{}[]\|`;`:`'`"<>/? " ;"
		CharsFound := ""

		Loop Parse, IllegalChars
		{
			If FoundPos := InStr(CustomVars, A_LoopField) > 0
			{
				CharsFound := (CharsFound "," A_LoopField)
				
				IllegalCharsFound := 1
			}
		}
		
		If (IllegalCharsFound = 1)
		{
			CharsFound := SubStr(CharsFound, 2)
			
			SB.SetText("Illegal character(s) `"" CharsFound "`" in Variables field - remove to continue.")

			Return
		}
		
		If FoundPos := InStr(CustomVars, ",,") > 0
		{
			SB.SetText("Consecutive commas found in Variables field - remove to continue.")

			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Modifies carriage returns and comma code.
	
	AHKCmdLine := ControlGetText("Edit2", "v2 - Command Line Generator")
	AHKCmdLine := StrReplace(AHKCmdLine, "``,", ",") ;Replaces all '`,' with ','
	AHKCmdLine := StrReplace(AHKCmdLine, "``n", "``n``r") ;Replaces all '``n' with '``n``r'
	
	WorkingDir := ControlGetText("Edit3", "v2 - Command Line Generator")
	
	LogicEnd := (A_LineNumber - 2)
	
	If (DebugClicked = 1)
	{
		Global DebugStep := 1
		Global PreviousDebugStep := 1
		
		DebugWorkingDir := ""
		
		If (WorkingDir =! "")
			DebugWorkingDir := (", " WorkingDir)

		If (AHKCmdLine != "")
		{
			Global DebugLines := (AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText(AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}
		Else
		{
			Global DebugLines := ("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}

		Loop
		{
			If (NextClicked = 1)
			{
				ControlSetEnabled(1, "Button7", "v2 - Command Line Generator")
		
				Global NextClicked := 0
				
				Break
			}
			
			If (StopClicked = 1)
			{
				Global StopClicked := 0
				
				Return
			}
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Finds all custom variables in AHKCmdLine and WorkingDir encapsulated by '%'
	
	PercentVariables := ""

	Loop 2
	{
		If (A_Index = 1)
			Haystack := AHKCmdLine
		Else
		{
			If (WorkingDir != "")
				Haystack := WorkingDir
			Else
				Break
		}
			
		StartingPos := 1
		
		Loop
		{
			VariableFound := 0
			FirstPercent := InStr(Haystack, "%", , StartingPos)

			If (FirstPercent = 0)
				Break
			Else
			{
				SecondPercent := InStr(Haystack, "%", , FirstPercent + 1)

				If (SecondPercent = 0)
					Break
				Else If (SecondPercent = (FirstPercent + 1))
				{
					StartingPos := (SecondPercent + 1)

					Continue
				}

				If (SecondPercent < (Length := StrLen(Haystack)))
					StartingPos := (SecondPercent + 1)
				
				Variable := SubStr(Haystack, FirstPercent + 1, SecondPercent - FirstPercent - 1)

				Loop Parse, BuiltInVariables, ","
				{
					If (A_LoopField = Variable)
					{
						VariableFound := 1
					
						Break
					}
					Else
						VariableFound := 0
				}

				If (VariableFound = 0)
					PercentVariables := (PercentVariables "," Variable)
					
				If (SecondPercent = (Length := StrLen(Haystack)))
					Break
			}
		}
	}
	
	If (PercentVariables != "")
		VariableList := (BuiltInVariables PercentVariables)
	Else	
		VariableList := BuiltInVariables
	
	LogicEnd := (A_LineNumber - 2)
	
	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)
		
		DebugPercentVariables := SubStr(PercentVariables, 2)
	
		Global DebugLines := (DebugLines "Percent Variables: " DebugPercentVariables "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
		ControlSetText("Percent Variables: " DebugPercentVariables "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		
		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}

	LogicStart := (A_LineNumber + 2) ;Finds all variables in AHKCmdLine and WorkingDir
	
	If (FoundPos := InStr(CustomVars, ",", , , -1) = (Length := StrLen(CustomVars)))
		CustomVars := SubStr(CustomVars, 1, Length - 1)

	Loop Parse, VariableList, ","
	{
		If (VarCheck := InStr(CustomVars, A_LoopField) > 0)
		{
			CustomVars := StrReplace(CustomVars, A_LoopField ",")
			CustomVars := StrReplace(CustomVars, "," A_LoopField)
			CustomVars := StrReplace(CustomVars, A_LoopField)
		}
	}
		
	If (CustomVars != "")
		VariableList := (VariableList . "," CustomVars)
	
	FoundVariables := ""

	Loop 2
	{
		If A_Index = 1
			Haystack := AHKCmdLine
		Else
		{
			If (WorkingDir != "")
				Haystack := WorkingDir
			Else
				Break
		}
			
		Loop Parse, VariableList, ","
		{
			If ((InHaystack := InStr(Haystack, A_LoopField) > 0) And (InFoundVariables := InStr(FoundVariables, A_LoopField) = 0))
				FoundVariables := (FoundVariables A_LoopField ",")
		}
	}

	StrLength := StrLen(FoundVariables)
	FoundVariables := SubStr(FoundVariables, 1, StrLength - 1)
	
	LogicEnd := (A_LineNumber - 2)
	
	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)
		
		ControlSetEnabled(1, "Button7", "v2 - Command Line Generator")
		ControlSetEnabled(1, "Button10", "v2 - Command Line Generator")
	
		Global DebugLines := (DebugLines "Found Variables: " FoundVariables "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
		ControlSetText("Found Variables: " FoundVariables "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ; Modifies all explicit quotes

	Loop 2
	{
		If A_Index = 1
			Haystack := AHKCmdLine
		Else
		{
			If (WorkingDir != "")
				Haystack := WorkingDir
			Else
				Break
		}
			
		StartingPos := 1
	
		Loop
		{
			QuotePos := InStr(Haystack, "`"", , StartingPos) ;"
			
			If (QuotePos = 0)
				Break
			
			BeforeQuote := SubStr(Haystack, 1, QuotePos - 1)
			AfterQuote := SubStr(Haystack, QuotePos + 1)
			BeforeQuote := (BeforeQuote . "```"") ;"

			StartingPos := (QuotePos + 2)
			
			Haystack := (BeforeQuote AfterQuote)
		}
		
		If (A_Index = 1)
			AHKCmdLine := Haystack
		Else
		{
			If (WorkingDir != "")
				WorkingDir := Haystack
		}
	}

	LogicEnd := (A_LineNumber - 2)

	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)

		If (WorkingDir != "")
			DebugWorkingDir := (", " WorkingDir)

		If (AHKCmdLine != "")
		{
			Global DebugLines := (DebugLines AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText(AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}
		Else
		{
			Global DebugLines := (DebugLines "No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Removes all '%' from variables
	
	Loop 2
	{
		If A_Index = 1
			Haystack := AHKCmdLine
		Else
		{
			If (WorkingDir != "")
				Haystack := WorkingDir
			Else
				Break
		}
		
		Loop Parse, FoundVariables, ","
			Haystack := StrReplace(Haystack, "%" A_LoopField "%", A_LoopField) ;"
			
		If (A_Index = 1)
			AHKCmdLine := Haystack
		Else
		{
			If (WorkingDir != "")
				WorkingDir := Haystack
		}
	}
	
	LogicEnd := (A_LineNumber - 2)
	
	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)

		If (WorkingDir != "")
			DebugWorkingDir := (", " WorkingDir)

		If (AHKCmdLine != "")
		{
			Global DebugLines := (DebugLines AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText(AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}
		Else
		{
			Global DebugLines := (DebugLines "No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Determines if a variable is first or last in string
	
	ACLVariableFirst := 0
	WDVariableFirst := 0
	ACLVariableLast := 0
	WDVariableLast := 0
	ACLFirstVariable := ""
	WDFirstVariable := ""
	ACLLastVariable := ""
	WDLastVariable := ""
	
	Loop 2
	{
		If A_Index = 1
			Haystack := AHKCmdLine
		Else
		{
			If (WorkingDir != "")
				Haystack := WorkingDir
			Else
				Break
		}
		
		Loop Parse, FoundVariables, ","
		{
			HaystackLength := StrLen(Haystack)
			Variable := A_LoopField
			VariableLength := StrLen(Variable)
			StartingPos := 1
			
			Loop
			{
				FoundPos := InStr(Haystack, Variable, , StartingPos)
				
				If (FoundPos = 0)
					Break

				If (FoundPos = 1)
				{
					If (Haystack = AHKCmdLine)
					{
						ACLVariableFirst := 1
						ACLFirstVariable := Variable
					}
					Else If (Haystack = WorkingDir)
					{
						WDVariableFirst := 1
						WDFirstVariable := Variable
					}
					
					StartingPos := (StartingPos + VariableLength)
				}
				Else
				{
					If (HaystackLength = FoundPos + VariableLength - 1)
					{			
						If (Haystack = AHKCmdLine)
						{
							ACLVariableLast := 1
							ACLLastVariable := Variable
						}
						Else If (Haystack = WorkingDir)
						{
							WDVariableLast := 1
							WDLastVariable := Variable
						}
						
						Break
					}
					Else
						StartingPos := (StartingPos + VariableLength)
				}
						
			}
		}
	}
	
	Loop 2
	{
		If (A_Index = 1 And ACLVariableFirst = 1)
		{
			Variable := ACLFirstVariable
			VariableLength := StrLen(Variable)
			Haystack := AHKCmdLine
			HSLength := StrLen(Haystack)
			
			FoundPos := InStr(Haystack, Variable, , , -1)

			If (ACLVariableLast = 0 And (VariableLength = HSLength) And FoundPos = 1)
			{
				ACLVariableLast := 1
				ACLLastVariable := Variable
			}
		}
		
		If (A_Index = 2 And WDVariableFirst = 1)
		{
			If (WorkingDir != "")
			{
				Variable := WDFirstVariable
				VariableLength := StrLen(Variable)
				Haystack := WorkingDir
				HSLength := StrLen(Haystack)
				
				FoundPos := InStr(Haystack, Variable, , , -1)
				
				If (A_Index = 2 And WDVariableLast = 0 And (VariableLength = HSLength) And FoundPos = 1)
				{
					WDVariableLast := 1
					WDLastVariable := Variable
				}
			}
			Else
				Break
		}
	}
	
	LogicEnd := (A_LineNumber - 2)

	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)

		Global DebugLines := (DebugLines "ACLFirstVariable: " ACLFirstVariable "`r`nACLLastVariableLast: " ACLLastVariable "`r`nWDFirstVariable: " WDFirstVariable "`r`nWDLastVariable: " WDLastVariable "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
		ControlSetText("ACLFirstVariable: " ACLFirstVariable "`r`nACLLastVariableLast: " ACLLastVariable "`r`nWDFirstVariable: " WDFirstVariable "`r`nWDLastVariable: " WDLastVariable "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Places quotes around all variables.

	Loop 2
	{
		If A_Index = 1
			Haystack := AHKCmdLine
		Else
		{
			If (WorkingDir != "")
				Haystack := WorkingDir
			Else
				Break
		}

		Loop Parse, FoundVariables, ","
			Haystack := StrReplace(Haystack, A_LoopField, "`" " A_LoopField " `"")

		If (A_Index = 1)
			AHKCmdLine := Haystack
		Else
			WorkingDir := Haystack
	}
	
	LogicEnd := (A_LineNumber - 2)

	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)

		If (WorkingDir != "")
			DebugWorkingDir := (", " WorkingDir)

		If (AHKCmdLine != "")
		{
			Global DebugLines := (DebugLines AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText(AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}
		Else
		{
			Global DebugLines := (DebugLines "No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Removes quotes between two variables
	
	Loop 2
	{
		If A_Index = 1
			Haystack := AHKCmdLine
		Else
		{
			If (WorkingDir != "")
				Haystack := WorkingDir
			Else
				Break
		}

		Loop Parse, FoundVariables, ","
			Haystack := StrReplace(Haystack, A_LoopField " `"`" ", A_LoopField " ")
			
		If (A_Index = 1)
			AHKCmdLine := Haystack
		Else
			WorkingDir := Haystack
	}
	
	LogicEnd := (A_LineNumber - 2)
		
	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)

		If (WorkingDir != "")
			DebugWorkingDir := (", " WorkingDir)

		If (AHKCmdLine != "")
		{
			Global DebugLines := (DebugLines AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText(AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}
		Else
		{
			Global DebugLines := (DebugLines "No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Removes quotes from start and/or end of string if variable exist there
	
	Loop 2
	{
		If (A_Index = 1)
			Haystack := AHKCmdLine
		Else
		{
			If (WorkingDir != "")
				Haystack := WorkingDir
			Else
				Break
		}
	
		If (A_Index = 1)
		{
			If (ACLVariableFirst = 1)
				Haystack := SubStr(Haystack, 3)
				
			If (ACLVariableLast = 1)
			{
				Length := StrLen(Haystack)
				Haystack := SubStr(Haystack, 1, Length - 2)
			}
			
			If (ACLVariableFirst = 0 And ACLVariableLast = 0)
				HayStack := ("`"" Haystack "`"")
			Else If (ACLVariableFirst = 1 And ACLVariableLast = 0)
				HayStack := (Haystack "`"") ;"
			Else If (ACLVariableFirst = 0 And ACLVariableLast = 1)
				HayStack := ("`"" Haystack) ;"
		}
		Else
		{
			If (WDVariableFirst = 1)
				Haystack := SubStr(Haystack, 3)
				
			If (WDVariableLast = 1)
			{
				Length := StrLen(Haystack)
				Haystack := SubStr(Haystack, 1, Length - 2)
			}
			
			If (WDVariableFirst = 0 And WDVariableLast = 0)
				Haystack := ("`"" Haystack "`"")
			Else If (WDVariableFirst = 1 And WDVariableLast = 0)
				Haystack := (Haystack "`"") ;"
			Else If (WDVariableFirst = 0 And WDVariableLast = 1)
				Haystack := ("`"" Haystack) ;"
		}
		
		If (A_Index = 1)
			AHKCmdLine := Haystack
		Else
			WorkingDir := Haystack
	}
	
	If (WorkingDir != "")
		WorkingDir := (", " WorkingDir)

	LogicEnd := (A_LineNumber - 2)

	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)

		If (AHKCmdLine != "")
		{
			Global DebugLines := (DebugLines AHKCmdLine WorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText(AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}
		Else
		{
			Global DebugLines := (DebugLines "No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Adds Run/RunWait to AHKCmdLine
	
	If (RunChecked := ControlGetChecked("Button1", "v2 - Command Line Generator") = 1)
	{
		If (AHKCmdLine != "")
		{
			If (ACLVariableFirst = 1)
				AHKCmdLine := ("Run A_ComSpec `" /c `" " AHKCmdLine)			
			Else
			{
				AHKCmdLine := StrReplace(AHKCmdLine, "`"", , , , 1)
				AHKCmdLine := ("Run A_ComSpec `" /c " AHKCmdLine)
			}
		}
	}
	
	If (RunWaitChecked := ControlGetChecked("Button2", "v2 - Command Line Generator") = 1)
	{
		If (AHKCmdLine != "")
		{
			If (ACLVariableFirst = 1)
				AHKCmdLine := ("RunWait A_ComSpec `" /c `" " AHKCmdLine)			
			Else
			{
				AHKCmdLine := StrReplace(AHKCmdLine, "`"", , , , 1)
				AHKCmdLine := ("RunWait A_ComSpec `" /c " AHKCmdLine) ;"
			}
		}
	}
	
	LogicEnd := (A_LineNumber - 2)

	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)

		If (AHKCmdLine != "")
		{
			Global DebugLines := (DebugLines AHKCmdLine WorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText(AHKCmdLine DebugWorkingDir "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}
		Else
		{
			Global DebugLines := (DebugLines "No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Adds Options to command line

	Options := ""
	
	If (AHKCmdLine != "")
	{
		If (MaxChecked := ControlGetChecked("Button3", "v2 - Command Line Generator") = 1)
			Options := (", `"Max`"")
		Else If (MinChecked := ControlGetChecked("Button4", "v2 - Command Line Generator") = 1)
			Options := (", `"Min`"")
		Else If (HideChecked := ControlGetChecked("Button5", "v2 - Command Line Generator") = 1)
			Options := (", `"Hide`"")
		
		If (Options != "" And WorkingDir = "")
			WorkingDir := ", "
	}
	
	LogicEnd := (A_LineNumber - 2)

	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)

		If (AHKCmdLine != "")
		{
			Global DebugLines := (DebugLines AHKCmdLine WorkingDir Options "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText(AHKCmdLine WorkingDir Options "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}
		Else
		{
			Global DebugLines := (DebugLines "No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	LogicStart := (A_LineNumber + 2) ;Adds OutputVarPID to command line

	OutputVarPID := ControlGetText("Edit4", "v2 - Command Line Generator")
	
	If (OutputVarPID != "")
	{
		OutputVarPID := (", &" OutputVarPID)
	
		If (Options = "")
			Options := ", "
		
		If (WorkingDir = "")
			WorkingDir := ", "
	}

	LogicEnd := (A_LineNumber - 2)

	If (DebugClicked = 1)
	{
		Global DebugStep := (DebugStep + 1)
		Global PreviousDebugStep := (PreviousDebugStep + 1)

		If (AHKCmdLine != "")
		{
			Global DebugLines := (DebugLines AHKCmdLine WorkingDir Options OutputVarPID "`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText(AHKCmdLine WorkingDir Options OutputVarPID "`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}
		Else
		{
			Global DebugLines := (DebugLines "No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd "|")
			ControlSetText("No Command Line available.`r`n`r`nLines # " LogicStart " - " LogicEnd, "Edit6", "v2 - Command Line Generator")
		}

		NextPrevStop
		
		If (StopClicked = 1)
		{
			Global StopClicked := 0
			
			Return
		}
	}
	
	ControlSetText(AHKCmdLine WorkingDir Options OutputVarPID, "Edit5", "v2 - Command Line Generator")
	
	If (DebugClicked = 1)
	{
		Global DebugClicked := 0

		Loop 4
			ControlSetEnabled(1, "Edit" A_Index, "v2 - Command Line Generator")
			
		Loop 5
			ControlSetEnabled(1, "Button" A_Index, "v2 - Command Line Generator")

		ControlHide("Button9", "v2 - Command Line Generator")
		ControlShow("Button8", "v2 - Command Line Generator")
		ControlHide("Button10", "v2 - Command Line Generator")
		ControlShow("Button11", "v2 - Command Line Generator")
		ControlSetEnabled(0, "Button7", "v2 - Command Line Generator")
		ControlSetEnabled(1, "Button11", "v2 - Command Line Generator")
	}
	
	Global Backup := (ControlGetText("Edit1", "v2 - Command Line Generator") "`n" ControlGetText("Edit2", "v2 - Command Line Generator") "`n" ControlGetText("Edit3", "v2 - Command Line Generator") "`n" ControlGetText("Edit4", "v2 - Command Line Generator") "`n" ControlGetChecked("Button1", "v2 - Command Line Generator") "`n" ControlGetChecked("Button2", "v2 - Command Line Generator") "`n" ControlGetChecked("Button3", "v2 - Command Line Generator") "`n" ControlGetChecked("Button4", "v2 - Command Line Generator") "`n" ControlGetChecked("Button5", "v2 - Command Line Generator"))
}

NextPrevStop(*)
{
	Loop
	{
		If (NextClicked = 1)
		{
			If (DebugStep = PreviousDebugStep)
			{
				Global NextClicked := 0
				
				Break
			}
			Else
			{
				If (PreviousDebugStep = 1)
					ControlSetEnabled(1, "Button7", "v2 - Command Line Generator")

				Global PreviousDebugStep := (PreviousDebugStep + 1)

				Loop Parse, DebugLines, "|"
				{
					If (A_Index = PreviousDebugStep)
					{
						ControlSetText(A_LoopField, "Edit6", "v2 - Command Line Generator")
						
						Global NextClicked := 0
						
						Break
					}
				}
			}
		}

		If (PreviousClicked = 1)
		{
			Global PreviousDebugStep := (PreviousDebugStep - 1)
			
			If (PreviousDebugStep = 1)
				ControlSetEnabled(0, "Button7", "v2 - Command Line Generator")
			
			Loop Parse, DebugLines, "|"
			{
				If (A_Index = PreviousDebugStep)
				{
					ControlSetText(A_LoopField, "Edit6", "v2 - Command Line Generator")
					
					Break
				}
			}
			
			Global PreviousClicked := 0
		}
	
		If (StopClicked = 1)
			Break
	}
}

BackupSettings(*)
{
	If FileExist(A_Temp "\CmdLineGen.txt")
		FileDelete A_Temp "\CmdLineGen.txt"
		
	Sleep 200
		
	If WinExist("v2 - Command Line Generator")
	{
		Global Backup := (ControlGetText("Edit1", "v2 - Command Line Generator") "`n" ControlGetText("Edit2", "v2 - Command Line Generator") "`n" ControlGetText("Edit3", "v2 - Command Line Generator") "`n" ControlGetText("Edit4", "v2 - Command Line Generator") "`n" ControlGetChecked("Button1", "v2 - Command Line Generator") "`n" ControlGetChecked("Button2", "v2 - Command Line Generator") "`n" ControlGetChecked("Button3", "v2 - Command Line Generator") "`n" ControlGetChecked("Button4", "v2 - Command Line Generator") "`n" ControlGetChecked("Button5", "v2 - Command Line Generator"))
	}
		
	FileAppend Backup, A_Temp "\CmdLineGen.txt"
}

Esc::ExitApp

Post Reply

Return to “Scripts and Functions (v2)”