Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Script Chooser/Launcher


  • Please log in to reply
4 replies to this topic
engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
I write alot of scripts for my wife to use, and they are mostly utilities like
1) take all the pictures in this folder, and make a powerpoint presentation with filenames ac captions
2) take all these files and add an extension (changing mac files to PC files)

I also use some scripts I found on this forum, Seek, etc.

It was getting to the point that I needed a good launcher, so I wrote this, which people may find interesting:

;
; Language:       English
; Platform:       Win9x/NT
; Author:         engunneer _AT_ gmail.com
;
; Script Function:
;	Show list of autohotkey scripts available in subfolders, run the selection and quit.
;	Ignore self if possible.


Loop, complete\*.exe, 0, 1	;look for all exes to run
{
	;get variables
	highestindex = %A_index%
	exename%A_Index% = %A_LoopFileName%
	exepath%A_Index% = %A_LoopFileLongPath%

	exeactive%A_Index% = Inactive
	Process, Exist, %A_LoopFileName%
	If ErrorLevel <> 0
	{
		;MsgBox, %A_LoopFileName% is running already.
		exeactive%A_Index% = Active
	}
	StringTrimRight, descfilepath, A_LoopFileFullPath, 3
	descfilepath = %A_WorkingDir%\%descfilepath%txt		;get description file if it exists
	textofdesc = none
	IfExist, %descfilepath%
		FileReadLine, textofdesc, %descfilepath%, 1
	exedesc%A_Index% = %textofdesc%

}

loop, %highestindex%		;build GUI from scripts
{
	filename := exename%A_Index%
	filepath := exepath%A_Index%
	filestat := exeactive%A_Index%
	filedesc := exedesc%A_Index%
	;MsgBox, Name: %filename%`nPath: %filepath%`nStatus: %filestat%`nDescription: %filedesc%
	Gui, Add, Button, xm gButtonRun%A_index%, %A_index%
	Gui, Add, Text, x40 yp+0, %filename%
	Gui, Add, Text, x140 yp+0, %filestat%
	Gui, Add, Text, x200 yp+0, %filedesc%
}

Gui, Add, Button, default, OK  ; The label ButtonOK will be run when the button is pressed.
Gui, Show,, Choose your script 
return  ; End of auto-execute section. The script is idle until the user does something.

GuiCancel:
GuiClose:
Gui, Submit  ; Save the input from the user to each control's associated variable.
ExitApp

ButtonOK:
Gui, Submit  ; Save the input from the user to each control's associated variable.
ExitApp

ButtonRun1:	
Run, %exepath1% ;run whatever the first item was
ExitApp
Return
ButtonRun2:
Run, %exepath2%
ExitApp
Return
ButtonRun3:
Run, %exepath3%
ExitApp
Return
ButtonRun4:
Run, %exepath4%
ExitApp
Return
ButtonRun5:
Run, %exepath5%
ExitApp
Return
ButtonRun6:
Run, %exepath6%
ExitApp
Return
ButtonRun7:
Run, %exepath7%
ExitApp
Return
ButtonRun8:
Run, %exepath8%
ExitApp
Return
ButtonRun9:
Run, %exepath9%
ExitApp
Return

Usage:
- Create a folder with this script in it. I call mine Documents\projects\scripts\completed
- Create subfolders for each script you write. For me, the only exe files in the folder are completed scripts.
- Create a desctiption file for each script. for example, my powerpoint script is ..\completed\powerpointer\powerpointer.exe and the description file is \completed\powerpointer\powerpointer.txt

I know ther is alot to clean up here, but the biggest problem I have is the labels ButtonRunX, where X is the number of the button you hit. I am limited to 9 scripts right now, but I eventually would want to expand it. Can it generate the labels directly? I think I would need the equivalent of:
{pseudocode - snip}
loop, all EXE files
{
...
Gui, Add, Button, gRunProgram {indexvariable = %A_index%}, %A_index%
...
}

...
RunProgram:
Run, %program%%indexvariable%
...

The part I can't figure out is {indexvariable = %A_index%}. Can I set a variable when a button is pushed? Or should I use GuiControlGet, variable, Focus?

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Hi engunneer,
I would instead suggest a GUI with a listbox for the script name and a edit field next to it that holds the description.
So if the user selectes a script name in the list the description is shown in the edit field.
That way, you are independent on the number of scripts and the window size of the launcher is reasonable small. And you only need to have two or three buttons on that GUI.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
just had to update this recently


#NoEnv
Counter := 0

;get list of folders
Loop, %A_ScriptDir%\*.*, 2, 0
{
  ThisFolder := A_LoopFileName
  Loop, %A_LoopFileFullPath%\*.exe, 0, 0   ;look for all exes to run
  {
  
  
    Counter += 1
  
     ;get variables
     highestindex := Counter
     exename%Counter% := ThisFolder
     exepath%Counter% := A_LoopFileLongPath
  
     exeactive%Counter% = Inactive
     Process, Exist, %A_LoopFileName%
     If ErrorLevel <> 0
     {
        ;MsgBox, %A_LoopFileName% is running already.
        exeactive%Counter% = Active
     }
     StringTrimRight, descfilepath, A_LoopFileFullPath, 4
     descfilepath = %descfilepath%.txt      ;get description file if it exists
     textofdesc := "No Description"
     IfExist, %descfilepath%
        FileReadLine, textofdesc, %descfilepath%, 1
     exedesc%Counter% = %textofdesc%
  
  }
}

listboxdata := ""

loop, %highestindex%      ;build GUI from scripts
{
   filename := exename%A_Index%
   filepath := exepath%A_Index%
   filestat := exeactive%A_Index%
   filedesc := exedesc%A_Index%
   
    If (A_Index > 1)
      listboxdata .= "|" 
   listboxdata .= filename "`t" filedesc
    If (A_Index = 1)
      listboxdata .= "|" 
}

  ;create a menu
  Menu, FileMenu, Add, E&xit`tAlt+F4, GuiClose
  Menu, HelpMenu, Add, &About`tF1, About
  Menu, MyMenuBar, Add, &File, :FileMenu  ; Attach the two sub-menus that were created above.
  Menu, MyMenuBar, Add, &Help, :HelpMenu
  Menu, TRAY, NoStandard
  Menu, TRAY, Add, E&xit, GuiClose
  Menu, TRAY, Add, &About, About

  Gui, Add, Listbox, xm w600 h200 HScroll VScroll AltSubmit vWhichTool T120, % listboxdata "||"
  Gui, Add, Button, default xm w130 h30 gStartLabel, Start 
  Gui, Add, Button, x480 yp w130 h30 gExitLabel, Exit  

  Gui, Menu, MyMenuBar
  Gui, Add, Statusbar
  SB_SetText("Select a tool to execute")

  Gui, Show,, Launcher 
return  ; End of auto-execute section. The script is idle until the user does something.


StartLabel:
Gui, Submit
Run, % exepath%WhichTool%
;fallthrough
GuiCancel:
GuiClose:
ExitLabel:
ExitApp

About:
  helptext =
  (LTrim
  version 0.9 (beta) April 2012
  )

  MsgBox, 0, About, %helptext% 
Return


Maestr0
  • Members
  • 652 posts
  • Last active: Aug 17 2019 06:07 PM
  • Joined: 18 Oct 2008
Nice! I tweaked the script a little bit so it has:
1) a reload option
2) allows up/down keys to scroll through the list
3) allows alt- to start programs in the list
;http://www.autohotkey.com/community/viewtopic.php?f=2&t=5173&p=532885#p532885
#NoEnv
Counter := 0

[color=#FF0000]hotkey, IfWinActive, Launcher ahk_class AutoHotkeyGUI		; so the hotkeys below only work when the script has focus (so does not interfere with normal up/down usage)
	hotkey, up, sub_up
	hotkey, down, sub_down
hotkey, IfWinActive,[/color]

;get list of folders
Loop, %A_ScriptDir%\*.*, 2, 0
{
  ThisFolder := A_LoopFileName
  Loop, %A_LoopFileFullPath%\*.exe, 0, 0   ;look for all exes to run
  {
  
  
    Counter += 1
  
     ;get variables
     highestindex := Counter
     exename%Counter% := ThisFolder
     exepath%Counter% := A_LoopFileLongPath

     [color=#FF0000]hotkey, !%Counter%, hotkey_run[/color]

     exeactive%Counter% = Inactive
     Process, Exist, %A_LoopFileName%
     If ErrorLevel <> 0
     {
        ;MsgBox, %A_LoopFileName% is running already.
        exeactive%Counter% = Active
     }
     StringTrimRight, descfilepath, A_LoopFileFullPath, 4
     descfilepath = %descfilepath%.txt      ;get description file if it exists
     textofdesc := "No Description"
     IfExist, %descfilepath%
        FileReadLine, textofdesc, %descfilepath%, 1
     exedesc%Counter% = %textofdesc%
  
  }
}

listboxdata := ""

loop, %highestindex%      ;build GUI from scripts
{
   filename := exename%A_Index%
   filepath := exepath%A_Index%
   filestat := exeactive%A_Index%
   filedesc := exedesc%A_Index%
   
    If (A_Index > 1)
      listboxdata .= "|" 
   listboxdata .= filename "`t" filedesc
    If (A_Index = 1)
      listboxdata .= "|" 
}

  ;create a menu
  Menu, FileMenu, Add, E&xit`tAlt+F4, GuiClose
  Menu, HelpMenu, Add, &About`tF1, About
  Menu, MyMenuBar, Add, &File, :FileMenu  ; Attach the two sub-menus that were created above.
  Menu, MyMenuBar, Add, &Help, :HelpMenu
  Menu, TRAY, NoStandard
  Menu, TRAY, Add, E&xit, GuiClose
  [color=#FF0000]Menu, TRAY, Add, &Reload, Reload[/color]
  Menu, TRAY, Add, &About, About

  Gui, Add, Listbox, xm w600 h200 HScroll VScroll AltSubmit vWhichTool T120, % listboxdata "||"
  Gui, Add, Button, default xm w130 h30 gStartLabel, Start 
  Gui, Add, Button, x480 yp w130 h30 gExitLabel, Exit  

  Gui, Menu, MyMenuBar
  Gui, Add, Statusbar
  SB_SetText("Select a tool to execute")

  Gui, Show,, Launcher 
return  ; End of auto-execute section. The script is idle until the user does something.
reload:
	reload
return

[color=#FF0000]; add up / down buttons functionality
sub_up:
	hasfocus :=
	GUIControlGet, hasfocus, FocusV ; retrieves the name of the focused control's associated variable.
	if hasfocus = WhichTool
	{
		gui, submit, nohide ; makes %whichtool% have the current selected row
		if whichtool = 1
			return
		whichtool -- 
		guicontrol, choose, whichtool, %whichtool%
	}
return
sub_down:
	hasfocus :=
	GUIControlGet, hasfocus, FocusV ; retrieves the name of the focused control's associated variable.
	if hasfocus = WhichTool
	{
		gui, submit, nohide ; makes %whichtool% have the current selected row
		whichtool ++
		guicontrol, choose, whichtool, %whichtool%
	}
return

hotkey_run:
	StringReplace, WhichTool, A_ThisHotkey, !,,
	Run, % exepath%WhichTool%
return
[/color]
StartLabel:
Gui, Submit
Run, % exepath%WhichTool%
;fallthrough
GuiCancel:
GuiClose:
ExitLabel:
ExitApp

About:
  helptext =
  (LTrim
  version 0.9 (beta) April 2012
  )

  MsgBox, 0, About, %helptext% 
Return


Abrin
  • Members
  • 50 posts
  • Last active: Aug 16 2016 03:24 AM
  • Joined: 19 Jan 2014

Thank you so much to everyone for these code examples! :)