Sign in to follow this  
Followers 0
Jettison

m-Run

4 posts in this topic

#1 ·  Posted (edited)

Just a script that I first intended to be for my personal use, and it's tweaked to do that, so a lot of the functionality might be totally unnecessary, but I thought I'd post it in case anyone found it useful. In essence, it acts just like a normal windows run box, but with added stuff. One of the inspirations for this was RunFast. (M by the way stands for minimal)

Thanks to lod3n for his _StartRun UDF, which I'm using for this. I used to use a combination of Slim's _ShellExec and the built-in Run() funcs, but lod3n's UDF really rocks. :rolleyes:

When you first start m-run, it will create an ini called 'm-config.' Everything in it is pretty self-explanatory...There are options for fade in/out (you can toggle it on and off), AutoComplete Speed (specify in a decimal value like '0.2' or '2.0' or '1.5' - default is 1.2, which is neither too fast nor too slow), and width/height, and font properties. It also writes a history file (m-run.hist) so all your favorite apps can be quickly accessed. You can modify it yourself. In m-config you can also specify the maximum number of history items to write (if you make it a large number, and then reduce it, you may lose some history items in the process!).

An addition is it checks for a file named envvars.txt in the same directory as m-run. This is entirely optional, but it's something I use every day. Basically, you enter paths to common folders on your computer separated by lines, and it adds them to the PATH. For example, if you have a folder named 'Foo' and a file named 'bar.txt' inside it, you wouldn't have to do 'X:\Foo\bar.txt' - you could just do 'run.txt'

Since _StartRun for some reason doesn't allow you to omit extensions for file in the cwd (you can omit them for other paths in the environment though), there's a function that checks for that. So you could run m-config instead of m-config.ini and it would work. If you have many files with the same name but different extensions, it would launch the first one.

There are a few prefixes you can add: ` prefixes all DOS commands...for example:

`start foobar.txt

And you may have noticed a key for 'BBFolder' in the ini. This is for the blackbox shell..again..I made this for my own personal use, so most of you can just leave it blank and shouldn't have any problems. All it allows you to do is prefix blackbox bro@ms (broadcast messages) with @, like you normally would do in a blackbox run plugin.

So feedback would be appreciated, and constructive criticism. :rambo:

#Include <GUIConstants.au3>
#Include <GuiCombo.au3>
#Include <File.au3>
#Include <Array.au3>
#NoTrayIcon

Opt("RunErrorsFatal", 0)
Opt("WinTitleMatchMode", 3)

; --- Define Functions ---

Func _StartRun($sCmd, $sFolder = @DesktopDir, $rState = @SW_SHOWNORMAL)
    Local $prevRunFatal = Opt("RunErrorsFatal", 0)
   
    Local $pid = Run($sCmd, $sFolder, $rState)
   
    If $pid = 0 Then
        Local $aRet = DllCall("shell32.dll", "long", "ShellExecute", _
                "hwnd", 0, _
                "string", "", _
                "string", $sCmd, _
                "string", "", _
                "string", $sFolder, _
                "int", $rState)
        Local $errorText = _GetLastErrorMessage()

        If $aRet[0] > 32 Then
            Opt("RunErrorsFatal", $prevRunFatal)
            Return 1
        Else
            Opt("RunErrorsFatal", $prevRunFatal)
            Return $errorText
        EndIf
    Else
        Opt("RunErrorsFatal", $prevRunFatal)
        Return 1
    EndIf
EndFunc

Func _GetLastErrorMessage()
    Local $ret, $s
    Local $p = DllStructCreate("char[4096]")
    Local Const $FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000

    If @error Then Return ""

    $ret = DllCall("Kernel32.dll", "int", "GetLastError")

    $ret = DllCall("kernel32.dll", "int", "FormatMessage", _
            "int", $FORMAT_MESSAGE_FROM_SYSTEM, _
            "ptr", 0, _
            "int", $ret[0], _
            "int", 0, _
            "ptr", DllStructGetPtr($p), _
            "int", 4096, _
            "ptr", 0)
    $s = DllStructGetData($p, 1)
    Return $s
EndFunc

Func _StartRun98($sCmd, $sFolder = "", $rState = @SW_SHOWNORMAL)
    Local $prevRunFatal = Opt("RunErrorsFatal", 0)
    Local $pid = Run($sCmd, $sFolder, $rState)
    Opt("RunErrorsFatal", $prevRunFatal)

    If $pid = 0 Then
        $RetVal = ShellExecute($sCmd, "", $sFolder, "", $rState)
        Return $RetVal
    Else
        Return 1
    EndIf
EndFunc

Func _CheckConfig()
    Global $hist = "m-run.hist"
    $config = "m-config.ini"
    If Not FileExists($config) Then
        $mrun = "BBFolder=C:\Blackbox" & @LF _
        & "Title=mRun" & @LF _
        & "Width=150" & @LF _
        & "Height=20" & @LF _
        & "FontN=Corbel" & @LF _
        & "FontS=8" & @LF _ 
        & "ACSpeed=1.2"
        $transparency = "FadeIn=1" & @LF _ 
        & "inStep=5" & @LF _
        & "inFromTo=0-255" & @LF _
        & "FadeOut=1" & @LF _
        & "outStep=-5" & @LF _
        & "outFromTo=255-0"
        $aliases = "ce=C:\Program Files\CubicExplorer\CubicExplorer.exe"
        $envvars = "BB=C:\Blackbox"
        $history = "MaxHist=50"
        IniWriteSection($config, "mRun", $mrun)
        IniWriteSection($config, "Transparency", $transparency)
        IniWriteSection($config, "History", $history)
        IniWriteSection($config, "Aliases", $aliases)
    EndIf
    Global $bbdir = IniRead($config, "mRun", "BBFolder", "C:\Blackbox")
    Global $title = IniRead($config, "mRun", "Title", "mRun")
    Global $width = IniRead($config, "mRun", "Width", "150")
    Global $height = IniRead($config, "mRun", "Height", "20")
    Global $fontn = IniRead($config, "mRun", "FontN", "Corbel")
    Global $fonts = IniRead($config, "mRun", "FontS", "8")
    Global $acspeed = IniRead($config, "mRun", "ACSpeed", "1.2")
    Global $fadein = IniRead($config, "Transparency", "FadeIn", "1")
    Global $instep = IniRead($config, "Transparency", "inStep", "5")
    Global $inft = IniRead($config, "Transparency", "inFromTo", "0-255")
    Global $fadeout = IniRead($config, "Transparency", "FadeOut", "1")
    Global $outstep = IniRead($config, "Transparency", "outStep", "-5")
    Global $outft = IniRead($config, "Transparency", "outFromTo", "255-0")
    Global $maxhist = IniRead($config, "History", "MaxHist", "50")
    Global $alist = IniReadSection($config, "Aliases")
EndFunc

Func _CheckHist()
    ; Check to seeif history file contains more 
    ; history items than specified in $maxhist - 
    ; This usually happens if user changed maxhist 
    ; amount in config from greater to smaller
    $len = _FileCountLines($hist)
    If $len > $maxhist Then
        Local $array
        _FileReadToArray($hist, $array)
        For $i = $maxhist To UBound($array) - 1
            _ArrayDelete($array, $i)
        Next
        _FileWriteFromArray($hist, $array, 1)
    EndIf
    
    ; Check if there are any empty lines
    Local $hlist
    _FileReadToArray($hist, $hlist)
    $i = 1
    While $i <= $len
        If $hlist[$i] = "" Then
            _ArrayDelete($hlist, $i)
        EndIf
        $i = $i + 1
    WEnd
    _FileWriteFromArray($hist, $hlist, 1)
EndFunc

Func _WriteHist($x)
    Local $histexists
    FileOpen($hist, 1)
    $len = _FileCountLines($hist)
    If $len <= $maxhist Then
        $readf = FileRead($hist, 2)
        For $i = 1 To $len
            $line = FileReadLine($hist, $i)
            $liner = StringTrimRight($line, 1)
            ; Check for duplicates
            If $line = $x Or $line = $x & "\" Or $liner = $x Then
                $histexists = 1
                ExitLoop
            Else 
                $histexists = 0
                ; Don't write the history just yet.
                ; Continue loop until we're SURE there isn't a duplicate
            EndIf
        Next
        
        If $histexists = 0 Then
            FileWriteLine($hist, $x)
        EndIf
    EndIf
    FileClose($hist)
EndFunc

Func _ReadHist()
    _CheckHist()
    FileOpen($hist, 0)
    Local $hlist
    _FileReadToArray($hist, $hlist)
    $hlist = _ArrayToString($hlist, "|", 1, UBound($hlist)-2)
    GUICtrlSetData($input, $hlist)
    FileClose($hist)
EndFunc

Func _AutoComplete()
    $aString = ""
    _GUICtrlComboAutoComplete($input, $aString)
EndFunc

Func _CreateGUI()
    Global $GUI = GUICreate($title, $width, $height, -1, -1, BitOr($WS_POPUP, $WS_CAPTION, _ 
    $DS_SETFOREGROUND+$WS_SYSMENU), $WS_EX_TOOLWINDOW+$WS_EX_TOPMOST+$WS_EX_ACCEPTFILES)
    Global $input = GUICtrlCreateCombo("", 0, 0, $width, -1)
    GUICtrlSetStyle($input, $GUI_DROPACCEPTED)
    _ReadHist() 
    
    Global $gobtn = GUICtrlCreateButton("Go", $width, $height-2, 10, 10, $BS_DEFPUSHBUTTON)
    GUICtrlSetState($input, $GUI_FOCUS)
    GUICtrlSetFont($input, $fonts, -1, -1, $fontn)
    WinSetTrans($GUI, "", 0)
    GUISetState()
    
    _FadeIn()
    
    If StringIsFloat($acspeed) Then $acspeed = StringReplace($acspeed, ".", "") & "00"
    AdlibEnable("_AutoComplete", $acspeed)
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                _FadeOut()
                Exit
            Case $msg = $gobtn
                _RunThis()
                _FadeOut()
                Exit
        EndSelect
    WEnd
    AdlibDisable()
EndFunc

Func _FadeIn()
    If $fadein = "1" Then
        $fromTo = StringSplit($inft, "-")
        If $fromTo[0] = 2 Then 
            $from = $fromTo[1]
            $to = $fromTo[2]
            For $i = $from to $to Step $instep
                WinSetTrans($GUI, "", $i)
                Sleep(1)
            Next
        EndIf
    Else
        WinSetTrans($GUI, "", 255)
    EndIf
EndFunc

Func _FadeOut()
    If $fadeout = "1" Then
        $fromTo = StringSplit($outft, "-")
        If $fromTo[0] = 2 Then 
            $from = $fromTo[1]
            $to = $fromTo[2]
            For $i = $from to $to Step $outstep
                WinSetTrans($GUI, "", $i)
                Sleep(1)
            Next
        EndIf
    EndIf
EndFunc

Func _CheckEnv()
    If FileExists("envvars.txt") Then
        FileOpen("envvars.txt", 0)
        $envlist = FileRead("envvars.txt")
        $envlist = StringSplit($envlist, @CRLF)
        For $i = 1 to $envlist[0]
            $path = EnvGet("PATH")
            EnvSet("PATH", $path & ";" & $envlist[$i])
        Next
    EndIf
EndFunc

Func _CheckAlias()
    For $i = 1 To $alist[0][0]
        Global $alias = ""
        If $alist[$i][0] = $run Then
            Global $alias = StringFormat("%s", $alist[$i][1])
            ExitLoop
        EndIf
    Next
EndFunc

Func _CheckExt()
    If Not StringInStr($run, ".", 0, -1) Then
        $first = FileFindFirstFile($run & ".*")
        $next = FileFindNextFile($first)
        If @error <> 1 Then
            Global $run = $next
        EndIf
    EndIf
EndFunc

Func _RunThis()
    Global $run = GUICtrlRead($input)
    _CheckEnv()
    _CheckAlias()
    _CheckExt()
    $strleft = StringLeft($run, 1)
    $strleft4 = StringLeft($run, 4)
    $strmid = StringMid($run, 2, 1)
    If $strleft = "`" Then
        $run = StringTrimLeft($run, 1)
        _DOSCmd($run)
    ElseIf $strleft = "@" Then
        _ExecBroam($run)
    ElseIf $alias <> "" Then
        $run = $alias
        _DependRun($run)
    Else
        $run = StringFormat("%s", $run)
        _DependRun($run)
    EndIf
EndFunc

Func _DependRun($x)
    If @OSTYPE = "WIN32_WINDOWS" Then
            $go = _StartRun98($x)
            If $go = 1 Then _WriteHist($run)
    Else
        $go = _StartRun($x)
        If $go <> 1 Then 
            _FadeOut()
            MsgBox(0, "Error", $go)
            Exit
        Else
            _WriteHist($run)
        EndIf
    EndIf
EndFunc

Func _DOSCmd($x)
    Run(@ComSpec & " /c " & $x, "", @SW_HIDE)
EndFunc

Func _ExecBroam($y)
    If @OSTYPE = "WIN32_WINDOWS" Then
        _StartRun98($bbDir & "\blackbox.exe -exec " & $y)
    Else
        _StartRun($bbDir & "\blackbox.exe -exec " & $y)
        ; Don't check for errors, blackbox will handle those. 
    EndIf
EndFunc
    
; --- Call Functions ---
$g_szVersion = "mRun"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)
_CheckConfig()
_CreateGUI()

I know I have a bajillion functions in there, and it's a bit disorganized but...:x

Edited by Jettison

[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites



line 226: AdlibDisable should be AdlibDisable()

I think an options window that can modify the parameters in the .ini file would be handy.

other than that, nice work


[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites

Ack, that's fixed now.

Yeah, I'm working on a gui to edit the ini file .. also for some reason the CheckHist() func, or the part of it that checks for duplicate history items, doesn't work in 9x ...


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

Ok, see first post..The function to write history should work in 9x now.


[font="Impact"]Cats rule, humans drool.[/font]

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0