- Сообщения
- 8,673
- Репутация
- 2,486
AutoIt: 3.36.1
Версия: 1.0
Категория: Командная строка, Окна и диалоги
Описание: Самопальный командный интерпретатор, аналог Command Line (cmd).
Код:
Снимок:

История версий:
Источник: autoit-script.ru
Автор(ы): G.Sandler (CreatoR)
Версия: 1.0
Категория: Командная строка, Окна и диалоги
Описание: Самопальный командный интерпретатор, аналог Command Line (cmd).
Код:
Код:
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <ScrollBarConstants.au3>
#include <WindowsConstants.au3>
#include <GUIEdit.au3>
#include <GUIMenu.au3>
#include <Misc.au3>
#include <File.au3>
#include <WinAPIEx.au3>
_Console_ExpandVars(1)
_WinAPI_SetThemeAppProperties(0)
Global $aSupported_Cmds = _Console_GetCmds()
Global $aCmds_History[1]
Global $iCurr_Cmd_InHistory = 0
Global $sApp_Title = StringTrimRight(@ScriptName, 4)
Global $sConsole_Font = "Courier New"
Global $sConsole_BkColor = 0x000000
Global $sConsole_TxtColor = 0xFFFFFF
Global $sConsole_InptTxtColor = 0xF3B214
Global $iGUI_W = 800
Global $iGUI_H = 500
$hGUI = GUICreate($sApp_Title, $iGUI_W, $iGUI_H)
GUISetIcon("cmd.exe", 0)
$nConsole_Edit = GUICtrlCreateEdit("" , 4, 4, $iGUI_W - 10, $iGUI_H - 25, BitOR($ES_WANTRETURN, $ES_MULTILINE, $ES_READONLY, $ES_AUTOVSCROLL, $WS_VSCROLL), 0)
GUICtrlSetBkColor(-1, $sConsole_BkColor)
GUICtrlSetFont(-1, 9, 400, 4, $sConsole_Font)
GUICtrlSetColor(-1, $sConsole_TxtColor)
$nDummy_Menu = GUICtrlCreateDummy()
$nContext_Menu = GUICtrlCreateContextMenu($nDummy_Menu)
$hContext_Menu = GUICtrlGetHandle($nContext_Menu)
$nCopy_MItem = GUICtrlCreateMenuItem("Copy", $nContext_Menu)
$nPaste_MItem = GUICtrlCreateMenuItem("Paste", $nContext_Menu)
GUICtrlCreateMenuItem("", $nContext_Menu)
$nExit_MItem = GUICtrlCreateMenuItem("Exit", $nContext_Menu)
$nCommand_Input = GUICtrlCreateInput("", 4, $iGUI_H - 20, $iGUI_W - 10, 15, -1, 0)
GUICtrlSetBkColor(-1, $sConsole_BkColor)
GUICtrlSetFont(-1, 9, 400, 4, $sConsole_Font)
GUICtrlSetColor(-1, $sConsole_InptTxtColor)
$nPrevInput_Bttn = GUICtrlCreateButton("Prev", $iGUI_W - 155, $iGUI_H - 20, 50, 15)
GUICtrlSetState(-1, $GUI_HIDE)
$nNextInput_Bttn = GUICtrlCreateButton("Next", $iGUI_W - 105, $iGUI_H - 20, 50, 15)
GUICtrlSetState(-1, $GUI_HIDE)
$nSend_Bttn = GUICtrlCreateButton("Send", $iGUI_W - 55, $iGUI_H - 20, 50, 15)
GUICtrlSetState(-1, $GUI_HIDE)
Dim $aAccelKeys[3][2] = [["{ENTER}", $nSend_Bttn], ["{UP}", $nPrevInput_Bttn], ["{DOWN}", $nNextInput_Bttn]]
GUISetAccelerators($aAccelKeys)
GUISetState(@SW_SHOW)
_Console_SetHeader()
While 1
$aCurInfo = GUIGetCursorInfo($hGUI)
If $aCurInfo[3] = 1 And ($aCurInfo[4] = $nConsole_Edit Or $aCurInfo[4] = $nCommand_Input) Then
If $aCurInfo[4] = $nConsole_Edit Then
GUICtrlSetState($nPaste_MItem, $GUI_DISABLE)
Else
GUICtrlSetState($nPaste_MItem, $GUI_ENABLE)
EndIf
_GUICtrlMenu_TrackPopupMenu($hContext_Menu, $hGUI)
EndIf
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE, $nExit_MItem
GUIDelete($hGUI)
Exit
Case $nCopy_MItem
Local $sSelEditText = ControlCommand($hGUI, "", $nConsole_Edit, "GetSelected")
If Not @error And $sSelEditText <> "" Then ClipPut($sSelEditText)
Case $nPaste_MItem
Local $aCaret_Pos = _GUICtrlEdit_GetSel($nCommand_Input)
_GUICtrlEdit_InsertText($nCommand_Input, ClipGet(), $aCaret_Pos[0])
Case $nPrevInput_Bttn, $nNextInput_Bttn
If _Iif($nMsg = $nPrevInput_Bttn, ($iCurr_Cmd_InHistory > 1), ($iCurr_Cmd_InHistory < $aCmds_History[0])) Then
$iCurr_Cmd_InHistory -= _Iif($nMsg = $nPrevInput_Bttn, 1, -1)
EndIf
If $iCurr_Cmd_InHistory > 0 And $iCurr_Cmd_InHistory <= $aCmds_History[0] And $aCmds_History[$iCurr_Cmd_InHistory] <> "" Then
_Console_ExpandVars(0)
GUICtrlSetData($nCommand_Input, $aCmds_History[$iCurr_Cmd_InHistory])
_Console_ExpandVars(1)
EndIf
Case $nSend_Bttn
_Console_Send()
EndSwitch
WEnd
Func _Console_SetHeader()
Local $sProductName = RegRead('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'ProductName')
Local $sVersion = RegRead('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'CurrentVersion')
_Console_Write( _
$sProductName & ' [Version ' & $sVersion & '.' & @OSBuild & ']' & @CRLF & '(C) Microsoft Corporation, 1985-2012.' & @CRLF & @CRLF & _
"You are logged as: " & @UserName & @CRLF & _
"Computer: " & @ComputerName & @CRLF & _
"IP Address: " & @IPAddress1 & @CRLF & _
"Administrator Rights: " & _Iif(IsAdmin(), "Yes", "No") & @CRLF & _
"Help: Type Help or ? then hit ENTER to send the command to the console" & @CRLF & @CRLF & _
@WorkingDir & '>')
EndFunc
Func _Console_Send()
Local $sInputRead, $sCmd, $aParams, $sParams = "", $sClean_Params = "", $sData = "", $vRet = ""
If GUICtrlGetHandle($nCommand_Input) <> ControlGetHandle($hGUI, '', ControlGetFocus($hGUI)) Then
Return
EndIf
$sInputRead = StringStripWS(GUICtrlRead($nCommand_Input), 3)
If $sInputRead = "" Then
_Console_Write(@CRLF & @WorkingDir & ">")
Return
EndIf
_Console_ExpandVars(0)
$aCmds_History[0] += 1
ReDim $aCmds_History[$aCmds_History[0] + 1]
$aCmds_History[$aCmds_History[0]] = $sInputRead
$iCurr_Cmd_InHistory = $aCmds_History[0] + 1
_Console_ExpandVars(1)
$sCmd = StringRegExpReplace($sInputRead, '^([^\h]+)\h?.*', '\1')
$sClean_Params = StringRegExpReplace($sInputRead, '^[^\h]+\h*(.*)', '\1')
$sParams = $sClean_Params
$aStrs = StringRegExp($sParams, '("[^"]*"|''[^'']*'')', 3)
;Replace all strings (text between "") with temporary unique(?) stamp
For $i = 0 To UBound($aStrs)-1
$sParams = StringReplace($sParams, $aStrs[$i], '~_STR' & $i+1 & '_~', 1, 2)
Next
$aParams = StringSplit($sParams, ',')
$sParams = ''
For $i = 1 To $aParams[0]
$aParams[$i] = StringStripWS($aParams[$i], 3)
If Not StringRegExp($aParams[$i], '(?i)^(~_STR.*|\w+\(~_STR.*|\d+|(0x)?[0-9a-f]+|\?)$') Then
$aParams[$i] = Execute($aParams[$i])
EndIf
$sParams &= $aParams[$i]
If $i < $aParams[0] Then
$sParams &= ','
EndIf
Next
;Restore the strings that we removed (remember) earlier
For $i = 0 To UBound($aStrs)-1
$sParams = StringReplace($sParams, '~_STR' & $i+1 & "_~", '"' & Execute($aStrs[$i]) & '"', 1, 2)
Next
If _Console_CmdIsSupported($sCmd) Then
If $aParams[1] = "?" Then
_Console_ShowHelp($sCmd)
Return
EndIf
$sClean_Params = __StringStripChars($sClean_Params, '"', 3)
Switch $sCmd
Case "Shutdown"
$sData = @CRLF & $sCmd & ': NOT ADDED YET'
Case "Help", "?"
_Console_ShowHelp()
Return
Case "Title"
WinSetTitle($hGUI, "", $sClean_Params)
Case "DIR"
$sData = $sInputRead
If Not StringInStr($sClean_Params, '\') Then
$sClean_Params &= '\'
EndIf
FileChangeDir($sClean_Params)
Case "Clear", "Cls"
_Console_Write("", 0)
Return
Case "Color"
$sConsole_TxtColor = '0x' & StringRegExpReplace($aParams[1], '^(#|0x)', '')
If $aParams[1] = '' Then
$sData = @CRLF & $sCmd & ": Incorrect parameters in command / Wrong number of arguments in command call"
Else
If $aParams[0] > 1 Then
$sConsole_BkColor = '0x' & StringRegExpReplace($aParams[2], '^(#|0x)', '')
EndIf
If __StringColorIsDarkShade($sConsole_TxtColor) And __StringColorIsDarkShade($sConsole_BkColor) Then
$sConsole_TxtColor = 0xFFFFFF
EndIf
If Not __StringColorIsDarkShade($sConsole_TxtColor) And Not __StringColorIsDarkShade($sConsole_BkColor) Then
$sConsole_TxtColor = 0x000000
EndIf
GUICtrlSetColor($nConsole_Edit, $sConsole_TxtColor)
GUICtrlSetBkColor($nConsole_Edit, $sConsole_BkColor)
GUICtrlSetColor($nCommand_Input, _Iif($sConsole_BkColor = $sConsole_InptTxtColor, $sConsole_TxtColor, $sConsole_InptTxtColor))
GUICtrlSetBkColor($nCommand_Input, $sConsole_BkColor)
GUISetBkColor(_Iif(__StringColorIsDarkShade($sConsole_BkColor), 0xFFFFFF, 0x000000))
$sData = @CRLF & $sInputRead
EndIf
Case "Print"
If $sClean_Params = "" Then
$sData = @CRLF & $sCmd & ": Wrong number of arguments in command call"
Else
$sData = $sClean_Params
EndIf
Case "Exit"
Exit
Case Else
$sData = $sInputRead
If $sParams = "" Then
$vRet = Execute($sCmd & '()')
If @error Then
$vRet = Execute('_' & $sCmd & '()')
EndIf
Else
$vRet = Execute($sCmd & '(' & $sParams & ')')
If @error Then
$vRet = Execute('_' & $sCmd & '(' & $sParams & ')')
EndIf
EndIf
If @error Then
$sData = @CRLF & $sCmd & ": Incorrect parameters in command / Wrong number of arguments in command call"
EndIf
EndSwitch
_Console_Write($sData)
If $vRet <> '' Or IsArray($vRet) Then
If IsArray($vRet) Then
$sData = ''
If UBound($vRet, 0) > 1 Then
For $i = 0 To UBound($vRet)-1
For $j = 0 To UBound($vRet, 2)-1
$sData &= __StringAddTabbing($vRet[$i][$j], 50, ' ')
Next
$sData &= @CRLF
Next
Else
For $i = 0 To UBound($vRet)-1
$sData &= $vRet[$i] & @CRLF
Next
EndIf
$vRet = $sData
EndIf
_Console_Write(@CRLF & $vRet)
EndIf
_Console_Write(@CRLF & @CRLF & @WorkingDir & ">")
Return
EndIf
Run($sCmd & ' ' & $sParams)
If @error Then
_Console_Write(@CRLF & $sCmd & ": Not supported command / Programm not found" & @CRLF & @CRLF & @WorkingDir & ">")
Else
_Console_Write(@CRLF & $sInputRead & @CRLF & @CRLF & @WorkingDir & ">")
EndIf
EndFunc
Func _Console_Write($sText, $iAppend = 1)
GUICtrlSetData($nCommand_Input, "")
If GUICtrlRead($nConsole_Edit) = "" Then
$sText = StringStripWS($sText, 1)
EndIf
If $iAppend Then
_GUICtrlEdit_AppendText($nConsole_Edit, $sText)
Else
_GUICtrlEdit_SetText($nConsole_Edit, $sText)
EndIf
;_GUICtrlEdit_Scroll($nConsole_Edit, $SB_SCROLLCARET)
EndFunc
Func _Console_GetCmds()
_Console_ExpandVars(0)
Local $iTotalCmds = 24
Local $aCmds_Arr[$iTotalCmds + 1][2] = _
[ _
[$iTotalCmds], _
['Print', 'Like Echo in CmdLine' & @CRLF & _
@TAB & 'Supported Macros:' & @CRLF & _
@TAB & '@CPUArch@ - Returns "X86" when the CPU is a 32-bit CPU and "X64" when the CPU is 64-bit.' & @CRLF & _
@TAB & '@KBLayout@ - Returns code denoting Keyboard Layout. See Appendix for possible values.' & @CRLF & _
@TAB & '@MUILang@ - Returns code denoting Multi Language if available (Vista is OK by default).' & @CRLF & _
@TAB & '@OSArch@ - Returns one of the following: "X86", "IA64", "X64" - this is the architecture type of the currently running operating system.' & @CRLF & _
@TAB & '@OSLang@ - Returns code denoting OS Language.' & @CRLF & _
@TAB & '@OSType@ - Returns "WIN32_NT" for NT/2000/XP/2003/Vista/2008/Win7/2008R2.' & @CRLF & _
@TAB & '@OSVersion@ - Returns one of the following: "WIN_2008R2", "WIN_7", "WIN_2008", "WIN_VISTA", "WIN_2003", "WIN_XP", "WIN_XPe", "WIN_2000".' & @CRLF & _
@TAB & '@OSBuild@ - Returns the OS build number. For example, Windows 2003 Server returns 3790' & @CRLF & _
@TAB & '@OSServicePack@ - Service pack info in the form of "Service Pack 3" or, for Windows 95, it may return "B"' & @CRLF & _
@TAB & '@ComputerName@ - Computer''s network name.' & @CRLF & _
@TAB & '@UserName@ - ID of the currently logged on user.' & @CRLF & _
@TAB & '@IPAddress1@ - IP address of first network adapter. Tends to return 127.0.0.1 on some computers.' & @CRLF & _
@TAB & '@IPAddress2@ - IP address of second network adapter. Returns 0.0.0.0 if not applicable.' & @CRLF & _
@TAB & '@IPAddress3@ - IP address of third network adapter. Returns 0.0.0.0 if not applicable.' & @CRLF & _
@TAB & '@IPAddress4@ - IP address of fourth network adapter. Returns 0.0.0.0 if not applicable.' & @CRLF & _
@TAB & '@DesktopHeight@ - Height of the desktop screen in pixels. (vertical resolution)' & @CRLF & _
@TAB & '@DesktopWidth@ - Width of the desktop screen in pixels. (horizontal resolution)' & @CRLF & _
@TAB & '@DesktopDepth@ - Depth of the desktop screen in bits per pixel.' & @CRLF & _
@TAB & '@DesktopRefresh@ - Refresh rate of the desktop screen in hertz.' & @CRLF], _
['Help', 'Shows this commands help list'], _
['?', 'Shows this commands help list (same as Help)'], _
['Title', 'Sets the console title, Syntax/Example: Title "My Console"'], _
['DIR', 'Sets current workind dir, Syntax/Example: DIR D:\'], _
['Clear', 'Clears the console'], _
['Cls', 'Clears the console (same as Clear)'], _
['Color', 'Sets console color, Syntax/Example: Color FontColor [, BkColor] ;Parameters accept html colors as well as hex colors'], _
['Beep', 'Plays back a beep noise, Syntax/Example: Beep 500, 1000 ;500 is the Frequency, and 1000 is Duration'], _
['CDTray', 'Opens or closes a cd tray or type all to open or close all trays, Syntax/Example: CDTray C:'], _
['ClipPut', 'Writes text to the clipboard, Syntax/Example: ClipPut Text'], _
['ClipGet', 'Show text (and only text) from the clipboard'], _
['DirCreate', 'Creates a directory/folder, Syntax/Example: DirCreate "DirPath"'], _
['FileGetSize', 'Returns file size in bytes, Syntax/Example: FileGetSize "FilePath"'], _
['FileWrite', 'Writes to a file, Syntax/Example: FileWrite "FilePath", "Text Data"'], _
['ReplaceStringInFile', 'Perform replacing in certain file, Syntax/Example: ReplaceStringInFile "FilePath", "OldText", "NewText" [, Caseness [, Occurance]]'], _
['SoundSetWaveVolume', 'Sets the wave volume 0-100, Syntax/Example: SoundSetWaveVolume 50'], _
['WinSetTitle', 'Sets window title, Syntax/Example: WinSetTitle "Title", "Text", "New Title"'], _
['WinSetState', 'Sets window state, Syntax/Example: WinSetState "Title", "Text", State'], _
['WinSetTrans', 'Sets window transparency 0-255, Syntax/Example: WinSetTrans "Title", "Text", 150'], _
['ProcessList', 'List all processes'], _
['ProcessClose', 'Close a process, Syntax/Example: ProcessClose notepad.exe'], _
['MsgBox', 'Display Message Box, Syntax/Example: MsgBox 64, "@ScriptName@", "Hello World" [, TimeOut [, hWnd]]'], _
['Exit', 'Exits console'] _
]
_Console_ExpandVars(1)
Return $aCmds_Arr
EndFunc
Func _Console_CmdIsSupported($sCmd)
For $i = 1 To UBound($aSupported_Cmds)-1
If $sCmd = $aSupported_Cmds[$i][0] Then
Return True
EndIf
Next
Return False
EndFunc
Func _Console_ShowHelp($sCmd = "")
Local $sHelpStr = ""
_Console_ExpandVars(0)
For $i = 1 To $aSupported_Cmds[0][0]
$sHelpStr &= $aSupported_Cmds[$i][0] & " - " & $aSupported_Cmds[$i][1] & @CRLF
If $sCmd = $aSupported_Cmds[$i][0] Then
$sCmd = $aSupported_Cmds[$i][0] ;To display it as defined in the commands list (with proper case)
$sHelpStr = $aSupported_Cmds[$i][1]
ExitLoop
EndIf
Next
_Console_Write(@CRLF & $sCmd & _Iif($sCmd = "", "", @CRLF & "Usage:" & @CRLF) & @CRLF & $sHelpStr & @CRLF & @CRLF & @WorkingDir & ">")
_Console_ExpandVars(1)
EndFunc
Func _Console_ExpandVars($iExpand)
Opt("ExpandEnvStrings", $iExpand)
Opt("ExpandVarStrings", $iExpand)
EndFunc
Func __StringAddTabbing($sString, $iStrTabLen = 20, $sTab = @TAB)
Local $iStrLen = StringLen($sString)
If $iStrTabLen < $iStrLen Then
$sString = StringLeft($sString, Floor($iStrTabLen / 3)) & "..." & StringRight($sString, Floor($iStrTabLen / 3))
EndIf
While StringLen($sString) < $iStrTabLen
$sString &= $sTab
WEnd
Return $sString
EndFunc
Func __StringStripChars($sString, $sSubString, $iFlag = 0, $iCount = 0, $iGroupChars = 0)
If StringLen($sString) = 0 Then
Return SetError(1, 0, $sString)
EndIf
Local $sGroupChar_a = '(', $sGroupChar_b = ')'
If $iCount < 0 Then
$sGroupChar_a = '['
$sGroupChar_b = ']'
EndIf
$sSubString = StringRegExpReplace($sSubString, '([][{}()|.?+*\\^\$])', '\\\1')
If $iGroupChars = 1 Then
$sSubString = '[' & $sSubString & ']'
EndIf
Local $sPattern = '(?i)' & $sGroupChar_a & $sSubString & $sGroupChar_b
Local $sPattern_Count = '{1,' & $iCount & '}'
If $iCount <= 0 Then
$sPattern_Count = '+'
EndIf
If $iFlag <> 0 Then
$iCount = 0
EndIf
If $iFlag = 1 Then
$sPattern = '(?i)^' & $sGroupChar_a & $sSubString & $sGroupChar_b & $sPattern_Count
EndIf
If $iFlag = 2 Then
$sPattern = '(?i)' & $sGroupChar_a & $sSubString & $sGroupChar_b & $sPattern_Count & '$'
EndIf
If $iFlag = 3 Then
$sPattern = '(?i)^' & $sGroupChar_a & $sSubString & $sGroupChar_b & $sPattern_Count & '|' & $sGroupChar_a & $sSubString & $sGroupChar_b & $sPattern_Count & '$'
EndIf
$sString = StringRegExpReplace($sString, $sPattern, '', $iCount)
Return SetExtended(@extended, $sString)
EndFunc
Func __StringColorIsDarkShade($nColor)
Local $i_Red = BitAND(BitShift($nColor, 16), 0xFF)
Local $i_Green = BitAND(BitShift($nColor, 8), 0xFF)
Local $i_Blue = BitAND($nColor, 0xFF)
Local $iMidle_Color_Val = Int(765 / 2) ;765 is the total of 255 + 255 + 255
Return ($i_Red + $i_Green + $i_Blue) < $iMidle_Color_Val
EndFunc
Снимок:
История версий:
Источник: autoit-script.ru
Автор(ы): G.Sandler (CreatoR)