Jump to content

Create a control panel from a directory


jefhal
 Share

Recommended Posts

Here's a script that I use from any computer I log into that lets me display and run any of my script executables from a gui "control panel". You have to specify your target folder where required...

This is the latest version 2/6/2008 8:07 pm

;created by jeff hall 12/25/05 - 02/05/08
#include <guiconstants.au3>
#include <constants.au3>
#include <array.au3>
Dim $arExecutables
$array = 23
Global $i = 0, $n = 0, $m = 1, $k = 1, $j = 1
HotKeySet("{ESC}", "_terminate")
opt("TrayIconDebug", 1)

;;; Locate all of the exe's in the target folder:
;$search = FileFindFirstFile("c:\program files\autoit3\examples\*.exe")
$path = "c:\program files\autoit3\examples\"
$type = "*.exe"
$search = FileFindFirstFile($path & $type)
; MsgBox(1,"$search",$search)
; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
Dim $arExecutables[203]

;;; Fill up the array with the names of the executables found above

While 1
        ;MsgBox(1,"$m",$m)
    $file = FileFindNextFile($search)
        ;MsgBox(1,"$file",$file)
        ;sleep(1000)
    If @error Then ExitLoop
    $arExecutables[$m] = $file
    $arExecutables[0] = $m
        ;MsgBox(4096, "File:", $file & " , " & $m)
    $m = $m + 1
WEnd
;_ArrayDisplay($arExecutables,"$arExecutables")
; Close the search handle
FileClose($search)
        ;MsgBox(1,"",$arExecutables[0])

;;; Create the gui and make a new tab for each 60 executables found above

Global $fish[$arExecutables[0] + 1]

; Limit the number of buttons per tab to 60
Dim $numTabs
Dim $buttonspertab = 60
$numTabs = Ceiling($arExecutables[0]/$buttonspertab) + 1
                ;MsgBox(1,$numTabs,$numTabs)
$guiWidth = 1000
$space = 15
If $arExecutables[0] > $buttonspertab Then
    $guiHeight = 360 + 2 * $space
Else
    $guiHeight = (((Ceiling($arExecutables[0]/ 5)) + 1) * 30) + (3 * $space)
EndIf
GUICreate("My Gui", $guiWidth, $guiHeight)
$font = "Arial Narrow"
GUISetFont(9, 400, 1, $font)   ; will display underlined characters
For $t = 1 To $numTabs - 1
    $tab=GUICtrlCreateTab ($space, $space, $guiWidth - (2 * $space),$guiHeight - (2 * $space))
    $first = $arExecutables[($t-1) * ($k-1) * ($j-1) + 1]
    $last = $arExecutables[($t-1) * ($k-1) * ($j-1) + 60]
    If $last = "" then $last = $arExecutables[$arExecutables[0]]
                ;MsgBox(1,"",$arExecutables[$first])
    $tab0=GUICtrlCreateTabitem (" " & $first & "   to   " & $last & " ")
    If $t = $numTabs -1 Then
        $n = $arExecutables[0] - (($t - 1) * 60)
        MsgBox(1,"$n",$n)
    Else
        $n = 60
    EndIf
    
    For $k = 1 To Ceiling($n/5); number of rows
        For $j = 1 To 5                              ; number of columns
                ;MsgBox(1,"",$i & "   " & $arExecutables[0])
            $i = $i + 1
            If $i > $arExecutables[0] Then ExitLoop;$k = 1
                ;MsgBox(1,$i,$i)
            $exename = StringTrimRight($arExecutables[$i], 4)
            $fish[$i] = GUICtrlCreateButton($exename, ($j * 185) - 145, ($k * 25) + (2 * $space), 180, 25)
            $fish[0] = $i
                ;GUICtrlSetImage(-1, "shell32.dll", 15)
                ;GUICtrlCreateLabel("Button " & $ord,($j * 80) - 56, ($k * 85), 64, 15)
                ;   GUICtrlSetBkColor($fish[$i-1], 0x64C671)
                ;If ($t-1) * ($k-1) * ($j-1) +23 > $arExecutables[0] Then ExitLoop
        Next
                ;MsgBox(1,"$t*$k*$j",$t*$k*$j)
    Next
Next

GUISetState()

;;; Run the executable that the selected button represents

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
                ;_ArrayDisplay($fish,"$fish")
    For $n = 0 To $arExecutables[0] - 1
        If $msg = $fish[$n] Then
            $response = MsgBox(4, "Ready?", "Are you sure you want to run: " & $arExecutables[$fish[$n] -4 ] & "?")
            If $response = 7 Then ExitLoop
            MsgBox(1,"$path & exe name",$path & $arExecutables[$fish[$n] -4 ])
            Run(@ComSpec & ' /c ' & '"' & $path & $arExecutables[$fish[$n] -4 ] & '"',@TempDir,@SW_HIDE)
            ;Sleep(3000)
        EndIf
    Next
WEnd
Exit

Func _terminate()
    Exit 0
EndFunc   ;==>_terminate
Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Piano_Man-

Can you try unremming this line and see if the files are being found?

;MsgBox(4096, "File:", $file & " , " & $m)

How many exe's should be in the folder? I tested this on a folder with 3 and all that happened was that I got a really short wide GUI with no visible buttons.

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Please see my previous post and tell me what you get when you run the script and that message box comes up...

I have the same problem. I tried using 3 .exe and only a GUI popped up.

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Hi, nice idea and thank you for sharing but your code needs work.

I get the same error that others reported and also another error if I run your script on my system32 directory (my system32 directory contains 339 executables)..

>Running:(3.2.10.0):C:\Program Files\AutoIt3\autoit3.exe "C:\AutoIt Stuff\test.au3" 
C:\AutoIt Stuff\test.au3 (30) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: 
$arExecutables[$m] = $file 
^ ERROR
->08:59:41 AutoIT3.exe ended.rc:1
>Exit code: 1   Time: 2.158
Looks like your wanting to generate controls dynamically but in the next breath your hard setting array sizes, eg: Dim $arExecutables[203]

I tried your msgbox debug method but it was quicker for me to write your code my own way.

#include <GuiConstants.au3>
#include <GuiListView.au3>

Global $Exe = _BrowseDir()
If $Exe = 0 Then Exit

Opt("GUIOnEventMode", 1)

$Gui = GUICreate("Example Executable Launcher - Executables Found: " & $Exe[0], 490, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "Event", $Gui)
$LV = GUICtrlCreateListView("", 5, 5, 480, 190)
_GUICtrlListView_SetView ($LV, 1)
For $i = 1 To $Exe[0]
    $ITXT = StringTrimRight(StringTrimLeft($Exe[$i], StringInStr($Exe[$i], "\", 0, -1)), 4)
    GUICtrlCreateListViewItem($ITXT, $LV)
    GUICtrlSetOnEvent(-1, "Event")
    If Not GUICtrlSetImage(-1, $Exe[$i], -1) Then GUICtrlSetImage(-1, "shell32.dll", -3)
Next
GUISetState(@SW_SHOW, $Gui)

While 1
    Sleep(100)
WEnd

Func Event()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 1 To $Exe[0]
                Local $LVIT, $PTXT
                $LVIT = _GUICtrlListView_GetItemText($LV, _GUICtrlListView_GetNextItem($LV))
                $PTXT = StringTrimRight(StringTrimLeft($Exe[$i], StringInStr($Exe[$i], "\", 0, -1)), 4)
                If $LVIT = $PTXT And FileExists($Exe[$i]) Then ShellExecute($Exe[$i])
            Next    
    EndSwitch
EndFunc

Func _BrowseDir()
    Local $FSF = FileSelectFolder("Select a folder containing executables", "")
    If Not @error Then 
        If StringRight($FSF, 1) = "\" Then
            Return _GetExe(StringTrimRight($FSF, 1))
        Else
            Return _GetExe($FSF)
        EndIf
    EndIf
EndFunc

Func _GetExe($ePath)
    Local $Search, $File, $Ex
    $Search = FileFindFirstFile($ePath & "\*.exe")
    If $search = -1 Then Return 0
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $Ex &= $ePath & "\" & $File & "|"
    WEnd
    Return StringSplit(StringTrimRight($Ex, 1), "|")
EndFunc

Cheers

Link to comment
Share on other sites

Hi, nice idea and thank you for sharing but your code needs work.

You are so right! I submitted the latest version with some new ideas at the top of the thread. I'll check your code out as soon as possible. Thanks!

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Hi,

I've tried your updated code on my system32 dircectory again..

Same array error as my first attempt (posted above)

You need to resize the array as exe's are found or add the exe names to a string with a delimiter and use stringsplit to build the array..

Hard setting the array to 203 is no good if your trying to do things dynamically.

Cheers

Link to comment
Share on other sites

Nice idea! As I use it exclusively for launching AutoIt scripts, all the icons would be the same :)

Maybe pull the icons from the exe and make them on the buttons....

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

You caught me! I put that number in as it exceeds the total number of scripts I have in my examples folder. It should work for any folder with less than that number, as the code jumps out of the loop as soon as an array element = "". However, it was pure laziness and expedience that led me to leaving that sizz-bomb in there... : :)

You need to resize the array as exe's are found or add the exe names to a string with a delimiter and use stringsplit to build the array..

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Maybe you could use the _BrowseDir() and _GetExe() from the code I posted earlier and implement them into your script.

This way a user can select the folder to open and your array gets created at the size based on the files found.

Cheers

Link to comment
Share on other sites

smashly-

I tried your code tonight and it's very nice. I had to upgrade to all the latest versions of AutoIT and Scite, but it was about time. It would be nice if the window that opens would self-autosize to show all available exes (my code was built to do something like that), and it was weird when selecting a folder given how slow the selection and resulting dialog box occurred. It was like it was in slow-mo...

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...