MattHiggs Posted July 13, 2017 Posted July 13, 2017 (edited) Hello autoit scripters, So I spend a lot of time looking through these forums, as well as other scripting forums, looking for interesting/useful/cool udfs/example scripts that I might need to use for a current project or is something that I am interested in learning more about and/or how to do it myself. Unfortunately, however, many of the udfs/scripts provided on aforementioned scripting forums only contain the text which makes up the source code for that particular udf/script rather than attaching the script file to the forum post. I can understand why this is done, but that understanding does little to reduce the time consuming process of creating a new script file, opening it, copying the udf/script source code and (optionally) name of udf/script, pasting the source into the newly created file and (optionally) pasting the name as the new file name, saving, and then finally closing. Tired of this monotonous, hair-pulling (if I had any ), time-consuming process, I wrote this script to create a simple GUI which will significantly speed up this process for three different script file extensions: you simply input the name you want to name the script file in the input box (with or without extension). If the file name you provided does not have one the recognized script extensions (ps1, au3, vbs), then it will allow you to select the radio button associated with the file extension for the script you want to create. If the file name does have one of the recognized extensions, the script will automatically check the radio button associated with the detected extension and disable the others. Then paste the source code into the edit control and click "Create". Now this is where it gets interesting. If you already have a file with the same name in the script directory (configured upon first launch), then it will determine if the content in the already existing file is the same as the content in the edit control by comparing the text from the edit control to the text in the already existing file after stripping out the trailing and leading white space for each line and any empty lines. I am fully aware that commented lines/sections could cause this text comparison functionality to not work, but it does currently eliminate the default commented lines that come standard whenever an AutoIT script is created from the context menu (eg all lines leading up to "; Script Start - Add your code below here") from the text compare process, and I will get around to removing all comments from the text compare process if/when I feel like it. If the script determines the text content is the same, it will overwrite the already existing script with the content from the edit control. If it determines the contents are not the same, it will open the udf/script file in the default text editor associated to that particular file's extension, and prompts you with the options to either overwrite the contents, rename the new file so there is no conflict, or cancel the operation. Rinse and repeat. Hope you all enjoy! Updated: If the text in the file name input box has spaces, they will be replaced with underscores in the actual file name (because who likes spaces in regards to scripting?) Updated again (7/13/17): I forgot to account for the file encoding properties for powershell and visual basic scripts. The correct file encoding has now been specified and script is working as it should. expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_SaveSource=y #AutoIt3Wrapper_Res_Language=1033 #AutoIt3Wrapper_Res_requestedExecutionLevel=highestAvailable #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; *** Start added by AutoIt3Wrapper *** #include <AutoItConstants.au3> #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <StringConstants.au3> ; *** End added by AutoIt3Wrapper *** #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GuiEdit.au3> #include <Constants.au3> #include <Array.au3> If Not FileExists ( @AppDataDir & "\createscript\settings.ini" ) Then If Not FileExists ( @AppDataDir & "\createscript" ) Then DirCreate ( @AppDataDir & "\createscript" ) EndIf $folder = FileSelectFolder ( "Select the folder where you want to store saved scripts.", @MyDocumentsDir ) If $folder = "" Then Exit Else IniWrite ( @AppDataDir & "\createscript\settings.ini", "Settings", "Script folder", $folder ) EndIf EndIf #Region ### START Koda GUI section ### Form=c:\users\whiggs\onedrive\always script\form\script creator.kxf $Form1 = GUICreate("Form1", 410, 375, 192, 124, BitOR($GUI_SS_DEFAULT_GUI, $DS_SETFOREGROUND), BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE)) $Label1 = GUICtrlCreateLabel("File name", 160, 0, 89, 29) GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif") $Input1 = GUICtrlCreateInput("", 38, 40, 337, 21) GUIStartGroup() $Radio1 = GUICtrlCreateRadio("powershell", 56, 80, 81, 17) $Radio2 = GUICtrlCreateRadio("autoit", 152, 80, 57, 17) $Radio3 = GUICtrlCreateRadio("Visual Basic Script", 232, 80, 113, 17) GUIStartGroup() $Label2 = GUICtrlCreateLabel("Script Content", 140, 112, 128, 29) GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif") $Edit1 = GUICtrlCreateEdit("", 32, 152, 329, 169, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL)) $Button1 = GUICtrlCreateButton("Create file", 152, 328, 105, 33, $BS_NOTIFY) GUICtrlSetState(-1, $GUI_DISABLE) GUICtrlSetCursor(-1, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### $aRect = _GUICtrlEdit_GetRECT($Edit1) $aRect[0] += 10 $aRect[1] += 10 $aRect[2] -= 10 $aRect[3] -= 10 _GUICtrlEdit_SetRECT($Edit1, $aRect) While 1 If _GUICtrlEdit_GetTextLen($Input1) > 0 And _GUICtrlEdit_GetTextLen($Edit1) > 0 And BitAND(GUICtrlGetState($Button1), 128) Then GUICtrlSetState($Button1, $GUI_ENABLE) EndIf If (_GUICtrlEdit_GetTextLen($Input1) = 0 Or _GUICtrlEdit_GetTextLen($Edit1) = 0) And BitAND(GUICtrlGetState($Button1), 64) Then GUICtrlSetState($Button1, $GUI_DISABLE) EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Input1 If StringRight(GUICtrlRead($Input1), 4) = ".ps1" Then GUICtrlSetState($Radio2, $GUI_UNCHECKED) GUICtrlSetState($Radio3, $GUI_UNCHECKED) GUICtrlSetState($Radio1, $GUI_ENABLE) GUICtrlSetState($Radio1, $GUI_CHECKED) GUICtrlSetState($Radio2, $GUI_DISABLE) GUICtrlSetState($Radio3, $GUI_DISABLE) ElseIf StringRight(GUICtrlRead($Input1), 4) = ".au3" Then GUICtrlSetState($Radio1, $GUI_UNCHECKED) GUICtrlSetState($Radio3, $GUI_UNCHECKED) GUICtrlSetState($Radio2, $GUI_ENABLE) GUICtrlSetState($Radio2, $GUI_CHECKED) GUICtrlSetState($Radio1, $GUI_DISABLE) GUICtrlSetState($Radio3, $GUI_DISABLE) ElseIf StringRight(GUICtrlRead($Input1), 4) = ".vbs" Then GUICtrlSetState($Radio1, $GUI_UNCHECKED) GUICtrlSetState($Radio2, $GUI_UNCHECKED) GUICtrlSetState($Radio3, $GUI_ENABLE) GUICtrlSetState($Radio3, $GUI_CHECKED) GUICtrlSetState($Radio1, $GUI_DISABLE) GUICtrlSetState($Radio2, $GUI_DISABLE) Else If BitAND(GUICtrlGetState($Radio1), 128) Or BitAND(GUICtrlGetState($Radio2), 128) Or BitAND(GUICtrlGetState($Radio3), 128) Then GUICtrlSetState($Radio1, $GUI_ENABLE) GUICtrlSetState($Radio1, $GUI_UNCHECKED) GUICtrlSetState($Radio2, $GUI_ENABLE) GUICtrlSetState($Radio2, $GUI_UNCHECKED) GUICtrlSetState($Radio3, $GUI_ENABLE) GUICtrlSetState($Radio3, $GUI_UNCHECKED) EndIf EndIf Case $Edit1 Case $Button1 If GUICtrlRead($Radio1) = 4 And GUICtrlRead($Radio2) = 4 And GUICtrlRead($Radio3) = 4 Then If Not IsDeclared("sToolTipAnswer") Then Local $sToolTipAnswer $sToolTipAnswer = ToolTip("You need to select one of the script types item.", Default, Default, "Make a selection", 0, 0) Sleep(3000) ToolTip("") Else If GUICtrlRead($Radio1) = 1 Then If StringRight(GUICtrlRead($Input1), 4) = ".ps1" Then $file = IniRead ( @AppDataDir & "\createscript\settings.ini", "Settings", "Script folder", "Error" ) & "\" & GUICtrlRead($Input1) Else $file = IniRead ( @AppDataDir & "\createscript\settings.ini", "Settings", "Script folder", "Error" ) & "\" & GUICtrlRead($Input1) & ".ps1" EndIf ElseIf GUICtrlRead($Radio2) = 1 Then If StringRight(GUICtrlRead($Input1), 4) = ".au3" Then $file = IniRead ( @AppDataDir & "\createscript\settings.ini", "Settings", "Script folder", "Error" ) & "\" & GUICtrlRead($Input1) Else $file = IniRead ( @AppDataDir & "\createscript\settings.ini", "Settings", "Script folder", "Error" ) & "\" & GUICtrlRead($Input1) & ".au3" EndIf ElseIf GUICtrlRead($Radio3) = 1 Then If StringRight(GUICtrlRead($Input1), 4) = ".vbs" Then $file = IniRead ( @AppDataDir & "\createscript\settings.ini", "Settings", "Script folder", "Error" ) & "\" & GUICtrlRead($Input1) Else $file = IniRead ( @AppDataDir & "\createscript\settings.ini", "Settings", "Script folder", "Error" ) & "\" & GUICtrlRead($Input1) & ".vbs" EndIf Else SetError(3) EndIf If @error Then SetError(0) Else $hand = Null If FileExists($file) And Not filetextcompare(_GUICtrlEdit_GetText($Edit1), $file) Then ShellExecute($file, "", IniRead ( @AppDataDir & "\createscript\settings.ini", "Settings", "Script folder", "Error" ), $SHEX_OPEN) #Region --- CodeWizard generated code Start --- ;MsgBox features: Title=Yes, Text=Yes, Buttons=Yes, No, and Cancel, Icon=Warning If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer $iMsgBoxAnswer = MsgBox($MB_YESNOCANCEL + $MB_ICONEXCLAMATION, "File Exists", 'The file already exists, but it appears the contents are different. Would you like to replace the existing file? Select "no" to rename.') Select Case $iMsgBoxAnswer = $IDYES If StringRight($file, 3) = "au3" Then $hand = FileOpen($file, $FO_OVERWRITE) Else $hand = FileOpen($file, 514) EndIf FileWrite($hand, _GUICtrlEdit_GetText($Edit1)) FileClose($hand) Case $iMsgBoxAnswer = $IDNO $b = 0 Do $b += 1 Until Not FileExists(_GetFilenameDrive($file) & _GetFilenamePath($file) & _GetFilename($file) & "(" & $b & ")." & _GetFilenameExt($file)) If StringRight($file, 3) = "au3" Then $hand = FileOpen(_GetFilenameDrive($file) & _GetFilenamePath($file) & _GetFilename($file) & "(" & $b & ")." & _GetFilenameExt($file), $FO_OVERWRITE) Else $hand = FileOpen(_GetFilenameDrive($file) & _GetFilenamePath($file) & _GetFilename($file) & "(" & $b & ")." & _GetFilenameExt($file), 514) EndIf FileWrite($hand, _GUICtrlEdit_GetText($Edit1)) FileClose($hand) Case $iMsgBoxAnswer = $IDCANCEL EndSelect #EndRegion --- CodeWizard generated code Start --- Else If StringRight($file, 3) = "au3" Then $hand = FileOpen($file, $FO_OVERWRITE) Else $hand = FileOpen($file, 514) EndIf FileWrite($hand, _GUICtrlEdit_GetText($Edit1)) FileClose($hand) EndIf _GUICtrlEdit_SetText($Edit1, "") GUICtrlSetData($Input1, "") EndIf EndIf EndSwitch WEnd Func filetextcompare($text1, $two) $same = True $arr = StringSplit($text1, @CRLF, $STR_NOCOUNT) $ext2 = _GetFilenameExt($two) $arr2 = FileReadToArray($two) $search = _ArraySearch($arr2, "; Script Start - Add your code below here") If @error Then SetError(0) Else _ArrayDelete($arr2, "0-" & $search) EndIf $hol = "" $hol2 = "" For $i = 0 To UBound($arr) - 1 Step 1 If StringIsSpace($arr[$i]) Then $hol = $hol & $i & ";" ContinueLoop Else $arr[$i] = StringStripWS($arr[$i], 3) ContinueLoop EndIf Next _ArrayDelete($arr, StringTrimRight($hol, 1)) For $i = 0 To UBound($arr2) - 1 Step 1 If StringIsSpace($arr2[$i]) Then $hol2 = $hol2 & $i & ";" ContinueLoop Else $arr2[$i] = StringStripWS($arr2[$i], 3) ContinueLoop EndIf Next _ArrayDelete($arr2, StringTrimRight($hol2, 1)) If UBound($arr) <> UBound($arr2) Then $same = False Else $same2 = True $where = 0 $val1 = "" $val2 = "" For $i = 0 To UBound($arr2) - 1 Step 1 If $arr[$i] <> $arr2[$i] Then $same2 = False $where = $i $val1 = $arr[$i] $val2 = $arr2[$i] ExitLoop Else ContinueLoop EndIf Next If $same2 Then $same = True Else $same = False EndIf EndIf Return $same EndFunc ;==>filetextcompare Func _GetFilename($sFilePath) Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2") Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'") If IsObj($oColFiles) Then For $oObjectFile In $oColFiles Return $oObjectFile.FileName Next EndIf Return SetError(1, 1, 0) EndFunc ;==>_GetFilename Func _GetFilenameExt($sFilePath) Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2") Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'") If IsObj($oColFiles) Then For $oObjectFile In $oColFiles Return $oObjectFile.Extension Next EndIf Return SetError(1, 1, 0) EndFunc ;==>_GetFilenameExt Func _GetFilenameInt($sFilePath) Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2") Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'") If IsObj($oColFiles) Then For $oObjectFile In $oColFiles Return $oObjectFile.Name Next EndIf Return SetError(1, 1, 0) EndFunc ;==>_GetFilenameInt Func _GetFilenameDrive($sFilePath) Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2") Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'") If IsObj($oColFiles) Then For $oObjectFile In $oColFiles Return StringUpper($oObjectFile.Drive) Next EndIf Return SetError(1, 1, 0) EndFunc ;==>_GetFilenameDrive Func _GetFilenamePath($sFilePath) Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2") Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'") If IsObj($oColFiles) Then For $oObjectFile In $oColFiles Return $oObjectFile.Path Next EndIf Return SetError(1, 1, 0) EndFunc ;==>_GetFilenamePath Edited July 13, 2017 by MattHiggs
nikink Posted October 5, 2017 Posted October 5, 2017 Very neat. I get an error about $SHEX_OPEN when I compile - I assume there's a missing include? I've simply replaced it with "Open" for now.
MattHiggs Posted October 6, 2017 Author Posted October 6, 2017 7 hours ago, nikink said: Very neat. I get an error about $SHEX_OPEN when I compile - I assume there's a missing include? I've simply replaced it with "Open" for now. No... Compiles a ok for me. Make sure you have the latest version of AutoIT installed. The scripts that I upload are written, and will compile successfully, using the latest version of Autoit. https://1drv.ms/u/s!AndRKxKyXo6AgYcq5Gm3EEoQNEmoww
nikink Posted October 10, 2017 Posted October 10, 2017 Aha! ok, my version is 3.3.12 - I'd missed the new release. Thanks.
MattHiggs Posted October 12, 2017 Author Posted October 12, 2017 On 10/10/2017 at 1:13 AM, nikink said: Aha! ok, my version is 3.3.12 - I'd missed the new release. Thanks. My pleasure
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now