Sort list of running scripts alphabetically

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Sort list of running scripts alphabetically

03 Mar 2016, 17:09

Hi, I've picked up a script from the old website, which was written by derRaphael (I believe that's his name), and the script opens up a gui which lists all currently running scripts. I have been trying all afternoon to find a way to have the list sort alphabetically. I've discovered some code that will indeed sort text alphabetically, but I have not been able to find out where in this script to place it. In between both sets of dividers (=============) below, is where I placed the code that will sort all the running scripts alphabetically, and then it pops up a messagebox to confirm it. This part works. But the gui is still not sorting the list alphabetically - I'm guessing this is to do with where exactly the sorting code is being inserted to the script. I'm hoping someone would be able to look it over and perhaps find out where exactly that sorting code needs to go... I realize this is a grand request so any help would be awesome :)

Code: Select all

;-------- saved at Sonntag, 21. Juli 2013 20:26:38 --------------
;-------- http://www.autohotkey.com/de/forum/viewtopic.php?p=19758#19758 ---
; getAllAHKscripts.ahk / zLib Style Release (c) 2008 derRaphael(at)oleco.net
; Published: $2008-01-25_22-47$

; This script shows all running scripts to offer the possibility to restart, to edit or to terminate

#NoTrayIcon

DetectHiddenWindows, ON
SetTitleMatchMode, 2

Menu, cMenu, Add, Edit, AHKEDIT
Menu, cMenu, Add
Menu, cMenu, Add, Reload, AHKRESTART
Menu, cMenu, Add, Close, AHKCLOSE
Menu, cMenu, Add
Menu, cMenu, Add, Properties, AHKPROPS

Menu, Tray, NoStandard
Menu, Tray, Add, Liste Anzeigen, AHKSkripts
Menu, Tray, Add, Exit, AppExit
Menu, Tray, Default, Liste Anzeigen

AHKSkripts:
;Gui, -Caption
Gui, Color, 3399cc, Blue 
Gui, Font, s8, Arial
Gui, Font, cWhite
 
 Gui, Add, ListView, r36 w224 vListView gAHKListe AltSubmit, Name|PID
 Gui, +ToolWindow +AlwaysOnTop
 Gui, Show, x200 y200, Script Control
 SetTimer, Update, 500
return

Update:
 oListe := Liste
 Liste := PIDs := ShortNames := ""

 WinGet, hwnd, List, ahk_class AutoHotkey

 Loop, %hwnd%
 {
   ID := hwnd%A_Index%
   WinGetTitle, Titel, ahk_id %ID%

   SkriptPath := RegExReplace(Titel, " - AutoHotkey v" A_AhkVersion )
   SplitPath,SkriptPath,KurzName

   WinGet, PID, PID, %SkriptPath% ahk_class AutoHotkey

   Liste      .= SkriptPath "`n"
   PIDs       .= PID "`n"
   ShortNames .= KurzName "`n"
   ;msgbox %Liste%
 }

 
 if (oListe!=Liste) {
   LV_Delete()

   StringSplit,PID,PIDs,`n
   StringSplit,FullPath,Liste,`n
   Loop, Parse, ShortNames, `n
     LV_Add("",A_LoopField, PID%A_Index%)

   LV_ModifyCol()

 
 
 
 
 
 ;===================================================================
;SORT THE LIST OF SCRIPTS ALPHABETICALLY BEGINS HERE: 

;this alphabeticalizes the list:
data = %liste%
 
StringSplit, line, data, `n,`r
Loop, parse, data, `n, `r
	data1 .= (data1?"`n":"") A_LoopField "|" A_Index
data1 := RegExReplace(data1, "[ `,“”\(\)\/`:]")
sort, data1
loop, parse, data1, `n,`r
	l := RegExReplace(A_LoopField, ".*\|")	, out .= (out?"`n":"") line%l%
msgbox %out% ;show a messagebox with the revised list of running scripts in alphabetical order
} 
;SORT THE LIST OF SCRIPTS ALPHABETICALLY ENDS HERE
;====================================================================
 return






AHKCLOSE:
  GoSub, GetSkript
  WinClose, %Skript% ahk_class AutoHotkey
return

AHKRESTART:
  GoSub, GetSkript
  Interpreter := A_AhkPath
  Kommando := """" Interpreter """ /restart """ Skript """"
  Run, %Kommando%
return

AHKPROPS:
  GoSub, GetSkript
  Interpreter := A_AhkPath
  Kommando := """" Skript """"
  Run, properties %Kommando%
return

AHKEDIT:
  GoSub, GetSkript
  Interpreter := A_AhkPath
  Kommando := """" Skript """"
  Run, edit %Kommando%
return

GetSkript:
  Zeile  := LV_GetNext(0, "F")
  Skript := FullPath%Zeile%
return

AHKListe:
  If (A_GuiEvent="Normal") {
    Zeile := LV_GetNext(0, "F")
    ;ToolTip % FullPath%Zeile%   
    SetTimer, TT, 2500
  }
return

TT:
 SetTimer, TT, OFF
 ToolTip
return

GuiContextMenu:
  If (A_GuiControl="ListView") {
    Menu, cMenu, Show, %A_GuiX%, %A_GuiY%
  }
return

GuiClose:
GuiEscape:
 Liste := ""
 Gui, Destroy
 SetTimer, Update, OFF
return

AppExit:
  ExitApp
  
esc::
exitapp
return
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Sort list of running scripts alphabetically

03 Mar 2016, 20:39

Maybe the function below will help you create the gui you want a little easier. It put various information about all running AHK scripts in an array.

Code: Select all

AhkScripts(Scripts)
for key, Script in Scripts
	Titles .= Script.Title "`n"
Sort, Titles
MsgBox % Titles

;{ AHKScripts
; Fanatic Guru
; 2014 03 31
;
; FUNCTION that will find the path and file name of all AHK scripts running.
;
;---------------------------------------------------------------------------------
;
; Method:
;   AHKScripts(ByRef Array)
;
; Parameters:
;   1) {Array} variable in which to store AHK script path data array
;
; Returns:
;   String containing the complete path of all AHK scripts running
;   One path per line of string, delimiter = `n
;
; ByRef:
;   Populates {Array} passed as parameter with AHK script path data
;     {Array}.Path
;     {Array}.Name
;     {Array}.Dir
;     {Array}.Ext
;     {Array}.Title
;     {Array}.hWnd
;
; Example Code:
/*
	MsgBox % AHKScripts(Script_List)
	for index, element in Script_List
		MsgBox % "#:`t" index "`nPath:`t" element.Path "`nName:`t" element.Name "`nDir:`t" element.Dir "`nExt:`t" element.Ext "`nTitle:`t" element.Title "`nhWnd:`t" element.hWnd
	return
*/
AHKScripts(ByRef Array)
{
	DetectHiddenWindows, % (Setting_A_DetectHiddenWindows := A_DetectHiddenWindows) ? "On" :
	WinGet, AHK_Windows, List, ahk_class AutoHotkey
	Array := {}
	list := ""
	Loop %AHK_Windows%
	{
		hWin := AHK_Windows%A_Index%
		WinGetTitle, Win_Name, ahk_id %hWin%
		File_Path := RegExReplace(Win_Name, "^(.*) - AutoHotkey v[0-9\.]+$", "$1")
		SplitPath, File_Path, File_Name, File_Dir, File_Ext, File_Title
		Array[A_Index,"Path"] := File_Path
		Array[A_Index,"Name"] := File_Name
		Array[A_Index,"Dir"] := File_Dir
		Array[A_Index,"Ext"] := File_Ext
		Array[A_Index,"Title"] := File_Title
		Array[A_Index,"hWnd"] := hWin
		list .= File_Path "`n"
		
	}
	DetectHiddenWindows, %Setting_A_DetectHiddenWindows%
	return Trim(list, " `n")
}
;}
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: Sort list of running scripts alphabetically

04 Mar 2016, 22:37

Thanks FanaticGuru- that's very handy. I've managed to get as far as creating a listview containing the list of alphabeticalized scripts, but from there I can't find a way to click on any particular script in the list and then have a context menu pop up, giving the options to edit, reload, etc. I fear it's beyond my understanding even though I've spent the past day trying.

I've located another similar script, written by Wicked (I think), and this one seems even closer. All I do is change line# 11 from:

Gui, 1: Add, ListView, w277 h250 AltSubmit NoSortHdr vListView gListView, Name|PID

to:

Gui, 1: Add, ListView, w277 h250 AltSubmit SortHdr vListView gListView, Name|PID ;(changed NoSortHdr to SortHdr)

the listview now shows all scripts in alphabetical order upon launching. The problem is, when you click on a script to edit it, it opens up the wrong script. I'm hoping there's an easy fix to the code that will fix that? I can also post my other script that's close to working but I'll wait until asked, I don't want to bog down this topic. Here's Wicked's script with line#11 altered to sort the listview:

Code: Select all

; http://www.autohotkey.com/board/topic/95550-how-to-get-a-list-of-autohotkey-scripts-running/
; http://www.xp-waste.com/post23929.html#p23929
; http://www.autohotkey.com/docs/commands/ListView.htm

#SingleInstance Force
#NoEnv

_Process:=Object()

Gui, 1: Margin, 5, 5
;Gui, 1: Add, ListView, w277 h250 AltSubmit NoSortHdr vListView gListView, Name|PID ;DO NOT SORT THE LISTVIEW
Gui, 1: Add, ListView, w277 h250 AltSubmit SortHdr vListView gListView, Name|PID ;YES, SORT THE LISTVIEW
Gui, 1: Add, Button, x+-277 y+5 w50 h20, Refresh
Gui, 1: Add, Button, x+0 y+-20 w50 h20, Reload
Gui, 1: Add, Button, x+0 y+-20 w50 h20, Close
Gui, 1: Add, Button, x+0 y+-20 w50 h20, Save
Gui, 1: Add, Button, x+0 y+-20 w50 h20, Edit
Gui, 1: Add, Button, x+-250 y+0 w50 h20, Pause
Gui, 1: Add, Button, x+0 y+-20 w50 h20, Suspend
Menu, MyContextMenu, Add, Refresh, ButtonRefresh
Menu, MyContextMenu, Add, Reload, ButtonReload
Menu, MyContextMenu, Add, Close, ButtonClose
Menu, MyContextMenu, Add, Save, ButtonSave
Menu, MyContextMenu, Add, Edit, ButtonEdit
Menu, MyContextMenu, Add, Pause, ButtonPause
Menu, MyContextMenu, Add, Suspend, ButtonSuspend
LV_ModifyCol()

ButtonRefresh:
  Gui, 1: Show
    LV_Delete()
    _Processes:=0
    _Process.Remove(0,_Process.MaxIndex())
    GuiControl, -ReDraw, ListView
    For Process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where Name='AutoHotkey.exe'")
    {
        If(Process.ExecutablePath==A_AHKPath)
        {
            _Processes++
            _Process[_Processes]:=[Extract_Script_Name_From_CommandLine(Process.CommandLine)
                  ,Extract_Script_Path_From_CommandLine(Process.CommandLine),Process.ProcessID]
            LV_Add("",_Process[_Processes,1],Process.ProcessID)
        }
    }
    LV_ModifyCol()
    GuiControl, +ReDraw, ListView
Return

ButtonSave:
t := ""
Loop % LV_GetCount()
{
    LV_GetText(OutputVar1,A_Index,1)
    LV_GetText(OutputVar2,A_Index,2)
    t .= OutputVar1 A_Space OutputVar2 "`n"
}
/*
MsgBox % "Would you like to save this file to Clipboard? `n`n" t
IfMsgBox, Yes
Clipboard := t
; FileAppend, %t%, Running_Scripts.txt
*/
;MsgBox, "Would you like to save this file to Clipboard? `n`n" t
MsgBox, 262212, `n`n" t, Would you like to save this file to Clipboard? ; 262208+4
IfMsgBox, Yes, GoTo MN
IfMsgBox, No, Return
MN:
Clipboard := t
; FileAppend, %t%, Running_Scripts.txt
Return
/*
ButtonReload:
  If !LV_GetNext(0)
        Return
    _ScriptDIR:=_Process[LV_GetNext(0),2]
    Run, "%A_AHKPath%" /restart "%_ScriptDIR%"
    Sleep (500)
    GoSub, ButtonRefresh
Return
*/
ButtonReload:
  If !LV_GetNext(0)
        Return
RowNumber = 0 ; This causes the first loop iteration to start the search at the top of the list
Loop
{
    RowNumber := LV_GetNext(RowNumber) ; Resume the search at the row after that found by the previous iteration
    If NOT RowNumber
        Break
    ;LV_GetText(Text, RowNumber)
    ;MsgBox, The next selected row is #%RowNumber%, whose first field is "%Text%".
    _ScriptDIR:=_Process[LV_GetNext(RowNumber-1),2]
    Run, "%A_AHKPath%" /restart "%_ScriptDIR%"
    Sleep (500)
}
GoSub, ButtonRefresh
Return
/*
ButtonEdit:
  If !LV_GetNext(0)
        Return
    _ScriptDIR:=_Process[LV_GetNext(0),2]
    ;Run, "%A_AHKPath%" /restart "%_ScriptDIR%"
;MsgBox % _ScriptDIR
Run, Notepad.exe %_ScriptDIR%
Return
*/
ButtonEdit:
  If !LV_GetNext(0)
        Return
RowNumber = 0
Loop
{
    RowNumber := LV_GetNext(RowNumber)
    If NOT RowNumber
        Break
    _ScriptDIR:=_Process[LV_GetNext(RowNumber-1),2]
    Run, Notepad.exe %_ScriptDIR%
    Sleep (500)
}
Return
/*
ButtonClose:
  If !LV_GetNext(0)
        Return
    Process, Close, % _Process[LV_GetNext(0),3]
    Sleep (500)
    GoSub, ButtonRefresh
Return
*/
ButtonClose:
  If !LV_GetNext(0)
        Return
RowNumber = 0
Loop
{
    RowNumber := LV_GetNext(RowNumber)
    If NOT RowNumber
        Break
    Process, Close, % _Process[LV_GetNext(RowNumber-1),3]
    Sleep (500)
}
GoSub, ButtonRefresh
Return

ButtonPause:
WM_COMMAND := 0x111
CMD_RELOAD := 65400
CMD_EDIT := 65401
CMD_PAUSE := 65403
CMD_SUSPEND := 65404
DetectHiddenWindows, On
  If !LV_GetNext(0)
        Return
RowNumber = 0
Loop
{
    RowNumber := LV_GetNext(RowNumber)
    If NOT RowNumber
        Break
;MsgBox % _Process[LV_GetNext(RowNumber-1),3]
GoSub, CMDP
    Sleep (500)
}
Return
CMDP:
Process, Exist
this_pid := _Process[LV_GetNext(RowNumber-1),3]
control_id := WinExist("ahk_class AutoHotkey ahk_pid " this_pid)
WinGet, id, list, ahk_class AutoHotkey
Loop, %id%
{
	this_id := id%A_Index%
    If (this_id = control_id)
	{
		PostMessage, WM_COMMAND, CMD_PAUSE,,, ahk_id %this_id%
		;PostMessage, WM_COMMAND, CMD_SUSPEND,,, ahk_id %this_id%
	}
}
Return

ButtonSuspend:
WM_COMMAND := 0x111
CMD_RELOAD := 65400
CMD_EDIT := 65401
CMD_PAUSE := 65403
CMD_SUSPEND := 65404
DetectHiddenWindows, On
  If !LV_GetNext(0)
        Return
RowNumber = 0
Loop
{
    RowNumber := LV_GetNext(RowNumber)
    If NOT RowNumber
        Break
;MsgBox % _Process[LV_GetNext(RowNumber-1),3]
GoSub, CMDS
    Sleep (500)
}
Return
CMDS:
Process, Exist
this_pid := _Process[LV_GetNext(RowNumber-1),3]
control_id := WinExist("ahk_class AutoHotkey ahk_pid " this_pid)
WinGet, id, list, ahk_class AutoHotkey
Loop, %id%
{
	this_id := id%A_Index%
    If (this_id = control_id)
	{
		;PostMessage, WM_COMMAND, CMD_PAUSE,,, ahk_id %this_id%
		PostMessage, WM_COMMAND, CMD_SUSPEND,,, ahk_id %this_id%
	}
}
Return

ListView:
If A_GuiEvent = DoubleClick
GoSub, ButtonEdit
Return

GuiContextMenu: ; Launched in response to a right-click:
If A_GuiControl <> ListView ; Display the menu only for clicks inside the ListView
Return
; Show the menu at the provided coordinates, A_GuiX and A_GuiY:
Menu, MyContextMenu, Show
Return

GuiControl,, Refresh,
GuiControl,, Reload,
GuiControl,, Close,
GuiControl,, Save,
GuiControl,, Edit,
GuiControl,, Pause,
GuiControl,, Suspend,
Return

Extract_Script_Name_From_CommandLine(P) {
    StringSplit,R,P,"
    SplitPath,R4,F
    Return F
}

Extract_Script_Path_From_CommandLine(P) {
    StringSplit,R,P,"
    Return R4
}

^Esc::ExitApp
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: Sort list of running scripts alphabetically

06 Mar 2016, 00:09

After 3 days of trying, it looks like I finally hacked something together that works. Go easy one me, I'm far from a pro ;) All you have to do is enter the folder location of your scripts on line 12. Just right-click on a script in the list to edit, reload or close the script. Anyways, there's probably also a lot of redundant code in here and a much better way to do things and most of this is amateur-ish, but like I said I'm mostly a tinkerer but I think I did get this one to work so it lists the scripts alphabetically in the listview. Credit goes to Wicked, who programmed this code. I just added the stuff that alphabeticalized the listview, since I couldn't find any other way to do it. Maybe somebody will find this useful and I hope it works on everyone else's system :)

Code: Select all

 
 
;-------- saved at Sonntag, 21. Juli 2013 20:26:38 --------------
;-------- http://www.autohotkey.com/de/forum/viewtopic.php?p=19758#19758 ---
; getAllAHKscripts.ahk / zLib Style Release (c) 2008 derRaphael(at)oleco.net
; Published: $2008-01-25_22-47$
 
; This script shows all running scripts to offer the possibility to restart, to edit or to terminate
 
 
 
 Var2 = C:\MyScripts\   ;enter your scripts folder location here - be sure to include the \ at the end of it
 
 
 
#NoTrayIcon
 
DetectHiddenWindows, ON
SetTitleMatchMode, 2
 
Menu, MyContextMenu, Add, Edit, Edit ;this pops up on right click
Menu, MyContextMenu, Add, Reload, Reload ;this pops up on right click
Menu, MyContextMenu, Add, Close, Close ;this pops up on right click
 
 
 
AHKSkripts:
;Gui, -Caption 
Gui, Font, s8, Arial
 
 
 Gui, Add, ListView, r36 w224 SortHdr vListView gAHKListe AltSubmit, Name|PID
 Gui, +ToolWindow +AlwaysOnTop
 Gui, Show, x200 y200, Script Control
 SetTimer, Update, 500
return
 
Update:
 oListe := Liste
 Liste := PIDs := ShortNames := ""
 
 WinGet, hwnd, List, ahk_class AutoHotkey
 
 Loop, %hwnd%
 {
   ID := hwnd%A_Index%
   WinGetTitle, Titel, ahk_id %ID%
 
   SkriptPath := RegExReplace(Titel, " - AutoHotkey v" A_AhkVersion )
   SplitPath,SkriptPath,KurzName
 
   WinGet, PID, PID, %SkriptPath% ahk_class AutoHotkey
 
   Liste      .= SkriptPath "`n"
   PIDs       .= PID "`n"
   ShortNames .= KurzName "`n"
   ;msgbox %Liste%
 }
 
 if (oListe!=Liste) {
   LV_Delete()
 
   StringSplit,PID,PIDs,`n
   StringSplit,FullPath,Liste,`n
   Loop, Parse, ShortNames, `n
     LV_Add("",A_LoopField, PID%A_Index%)
 
   LV_ModifyCol()
 }
return
 
 
GetSkript:
  Zeile  := LV_GetNext(0, "F")
  Skript := FullPath%Zeile%
return
 
AHKListe:
  If (A_GuiEvent="Normal") {
    Zeile := LV_GetNext(0, "F")
       }
return
 
AppExit:
  ExitApp
 
 
GuiContextMenu:  
If A_GuiControl <> ListView ; Display the menu only for clicks inside the ListView
Return
 
 
;================================================================================
LV_GetText(RowText, A_EventInfo)  
 
Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%
return
 
Edit:
{
DetectHiddenWindows, On  
SetTitleMatchMode, RegEx 
 
Script := RowText
 
WinGet, PID, PID, % "i)^.+\\" CleanEx(Script) " - AutoHotkey v.+$"
 
PIDClose1 = %PID% 
 
	Var1=	
	Var3=
 
    Var1=%RowText%    
    Var3=%Var2%%Var1% 		
 
	IfExist, %Var3%	
	{
    Run, Notepad.exe %Var3%  
	return
	}
 
	else
 
	IfNotExist, %Var3%
	{
	msgbox, That file doesn't exist in your folder
    return
	}
}
return
;================================================================================
 
 
;================================================================================
LV_GetText(RowText, A_EventInfo)  
Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%
return
 
Reload:
{
DetectHiddenWindows, On  
SetTitleMatchMode, RegEx 
 
Script := RowText
 
WinGet, PID, PID, % "i)^.+\\" CleanEx(Script) " - AutoHotkey v.+$"
 
PIDClose2 = %PID% 
 
 
	Var1=	
	Var3=
 
    Var1=%RowText%    
    Var3=%Var2%%Var1% 	
 
	IfExist, %Var3%	
	{
    Process, Close, %PIDClose2%
	sleep, 500	
    Run, %Var3% 
	return
	}
 
	else
 
	IfNotExist, %Var3%
	{
	msgbox, That file doesn't exist in your folder
    return
	}
}
return
;================================================================================
 
 
 
;================================================================================
LV_GetText(RowText, A_EventInfo)  
Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%
return
 
Close:
{
DetectHiddenWindows, On  
SetTitleMatchMode, RegEx 
 
Script := RowText
 
WinGet, PID, PID, % "i)^.+\\" CleanEx(Script) " - AutoHotkey v.+$"
;MsgBox, % Script "'s PID is:`n" PID
PIDClose3 = %PID%
 
 
	Var1=	
	Var3=
 
    Var1=%RowText%    
    Var3=%Var2%%Var1%  		
 
	IfExist, %Var3%	
	{
    Process, Close, %PIDClose3%
	return
	}
 
	else
 
	IfNotExist, %Var3%
	{
	msgbox, That file doesn't exist in your folder
    return
	}
}
return
;================================================================================
 
CleanEx(Needle)
{ 
StringReplace, Needle, Needle, \, \\, All
StringReplace, Needle, Needle, ., \., All
StringReplace, Needle, Needle, *, \*, All
StringReplace, Needle, Needle, ?, \?, All
StringReplace, Needle, Needle, +, \+, All
StringReplace, Needle, Needle, [, \[, All
StringReplace, Needle, Needle, `{, \`{, All
StringReplace, Needle, Needle, |, \|, All
StringReplace, Needle, Needle, (, \(, All
StringReplace, Needle, Needle, ), \), All
StringReplace, Needle, Needle, ^, \^, All
StringReplace, Needle, Needle, $, \$, All
Return Needle
}    
 
 
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Sort list of running scripts alphabetically

06 Mar 2016, 04:16

Most of this is just about how to create a ListView Gui and handling clicks.

The function handles all the getting of the info then you just have to know how to create the gui.

With a ListView there is no need to pre-sort. ListView has sorting built in.

Below is a very simple example of using the AHKScripts function to get the info and then creating a simple Listview Gui with a right click context menu.

Code: Select all

DetectHiddenWindows, On

Menu, MyContextMenu, Add, Open, MenuHandler
Menu, MyContextMenu, Add, Reload, MenuHandler
Menu, MyContextMenu, Add, Edit, MenuHandler
Menu, MyContextMenu, Add, Pause, MenuHandler
Menu, MyContextMenu, Add, Suspend, MenuHandler
Menu, MyContextMenu, Add, Close, MenuHandler

AHKScripts(Scripts)
Gui, Add, ListView, Sort, Script
For key, Script in Scripts
	LV_Add(, Script.Title)
Gui, Show
return

GuiContextMenu:
	LV_GetText(Title, A_EventInfo)
	Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%
return

MenuHandler:
	For Pick, Script in Scripts
		If (Title = Script.Title)
			break
	if (A_ThisMenuItem = "Close")
		WinKill, % "ahk_id " Scripts[Pick].hWnd
	else if (A_ThisMenuItem = "Open")
		PostMessage, 0x111, 65300,,, % "ahk_id " Scripts[Pick].hWnd
	else if (A_ThisMenuItem = "Reload")
		PostMessage, 0x111, 65400,,, % "ahk_id " Scripts[Pick].hWnd
	else if (A_ThisMenuItem = "Edit")
		PostMessage, 0x111, 65401,,, % "ahk_id " Scripts[Pick].hWnd
	else if (A_ThisMenuItem = "Pause")
		PostMessage, 0x111, 65403,,, % "ahk_id " Scripts[Pick].hWnd
	else if (A_ThisMenuItem = "Suspend")
		PostMessage, 0x111, 65404,,, % "ahk_id " Scripts[Pick].hWnd
return

GuiClose:
ExitApp

;{ AHKScripts
; Fanatic Guru
; 2014 03 31
;
; FUNCTION that will find the path and file name of all AHK scripts running.
;
;---------------------------------------------------------------------------------
;
; Method:
;   AHKScripts(ByRef Array)
;
; Parameters:
;   1) {Array} variable in which to store AHK script path data array
;
; Returns:
;   String containing the complete path of all AHK scripts running
;   One path per line of string, delimiter = `n
;
; ByRef:
;   Populates {Array} passed as parameter with AHK script path data
;     {Array}.Path
;     {Array}.Name
;     {Array}.Dir
;     {Array}.Ext
;     {Array}.Title
;     {Array}.hWnd
;
; Example Code:
/*
	MsgBox % AHKScripts(Script_List)
	for index, element in Script_List
		MsgBox % "#:`t" index "`nPath:`t" element.Path "`nName:`t" element.Name "`nDir:`t" element.Dir "`nExt:`t" element.Ext "`nTitle:`t" element.Title "`nhWnd:`t" element.hWnd
	return
*/
AHKScripts(ByRef Array)
{
	DetectHiddenWindows, % (Setting_A_DetectHiddenWindows := A_DetectHiddenWindows) ? "On" :
	WinGet, AHK_Windows, List, ahk_class AutoHotkey
	Array := {}
	list := ""
	Loop %AHK_Windows%
	{
		hWnd := AHK_Windows%A_Index%
		WinGetTitle, Win_Name, ahk_id %hWnd%
		File_Path := RegExReplace(Win_Name, "^(.*) - AutoHotkey v[0-9\.]+$", "$1")
		SplitPath, File_Path, File_Name, File_Dir, File_Ext, File_Title
		Array[A_Index,"Path"] := File_Path
		Array[A_Index,"Name"] := File_Name
		Array[A_Index,"Dir"] := File_Dir
		Array[A_Index,"Ext"] := File_Ext
		Array[A_Index,"Title"] := File_Title
		Array[A_Index,"hWnd"] := hWnd
		list .= File_Path "`n"
		
	}
	DetectHiddenWindows, %Setting_A_DetectHiddenWindows%
	return Trim(list, " `n")
}
;}
You can read the help document on ListView and fancy the look up some.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: Sort list of running scripts alphabetically

06 Mar 2016, 04:25

oh man... was it really this easy? I quickly checked out your code and yes, it seems to be that easy... let me take a better look in the morning and I'll respond back :)
just me
Posts: 9617
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Sort list of running scripts alphabetically

06 Mar 2016, 06:53

You can easily store all information needed for the context menu actions in the ListView. And you can hide the associated columns, if you prefer. Here's an example (I used the full path of the scripts to my liking):

Code: Select all

; ------------------------------------------------------------------------------
; This script shows all running scripts to offer the possibility to edit,
; pause, reload, suspend, or terminate the scripts.
; ------------------------------------------------------------------------------
#NoEnv
#NoTrayIcon
; Script settings
SetBatchLines, -1
DetectHiddenWindows, On
; Variables
PreviousList := ""
CurrentList  := ""
ScriptList   := ""
; Script commands
WM_COMMAND  := 0x111
CMD_RELOAD  := 65400
CMD_EDIT    := 65401
CMD_PAUSE   := 65403
CMD_SUSPEND := 65404
; Context Menu - this pops up on right click
Menu, ContextMenu, Add, Edit, PostCommand
Menu, ContextMenu, Add, Pause, PostCommand
Menu, ContextMenu, Add, Reload, PostCommand
Menu, ContextMenu, Add, Suspend, PostCommand
Menu, ContextMenu, Add
Menu, ContextMenu, Add, Close, Close
Menu, ContextMenu, Add, Kill, Kill
Menu, ContextMenu, Add
Menu, ContextMenu, Add, Open Folder, OpenFolder
; GUI
Gui, +ToolWindow +AlwaysOnTop +Resize +MinSize400x100
Gui, Margin, 0, 0
Gui, Font, s8, Arial
Gui, Add, ListView, w400 r20 Sort vScriptView -E0x0200, Path|PID|ID
GoSub, Update
Gui, Show, x200 y200, Script Control
SetTimer, Update, On
Return
; ------------------------------------------------------------------------------
GuiClose:
ExitApp
; ------------------------------------------------------------------------------
GuiContextMenu:
   ; Display the menu only for right-clicks on a ListView item
   If (A_GuiControl = "ScriptView") && (A_EventInfo) {
      LV_GetText(MenuPath, A_EventInfo, 1)
      LV_GetText(MenuID, A_EventInfo, 3)
      SplitPath, MenuPath, , MenuFolder, MenuExt ; <<<<< MenuExt added
      If (MenuExt = "exe")
         Menu, ContextMenu, Disable, Edit
      Else
         Menu, ContextMenu, Enable, Edit
      Menu, ContextMenu, Show, %A_GuiX%, %A_GuiY%
   }
Return
; ------------------------------------------------------------------------------
GuiSize:
   If (A_EventInfo <> 1) ; minimized
      GuiControl, Move, ScriptView, w%A_GuiWidth% h%A_GuiHeight%
Return
; ------------------------------------------------------------------------------
Update:
   CurrentList := ""
   WinGet, ScriptList, List, ahk_class AutoHotkey
   Loop, %ScriptList%
      If (ScriptList%A_Index% <> A_ScriptHwnd)
         CurrentList .= ScriptList%A_Index%
   If (CurrentList <> PreviousList) {
      GuiControl, -Redraw, ListView
      LV_Delete()
      Loop, %ScriptList% {
         ID := ScriptList%A_Index%
         If (ID = A_ScriptHwnd) ; skip this script
            Continue
         WinGetTitle, Title, ahk_id %ID%
         WinGet, PID, PID, ahk_id %ID%
         ScriptPath := RegExReplace(Title, " - AutoHotkey v.*$")
         LV_Add("", ScriptPath, PID, ID)
      }
      Loop, % LV_GetCount("Column")
         LV_ModifyCol(A_Index, "AutoHdr")
      GuiControl, +Redraw, ListView
   }
   PreviousList := CurrentList
Return
; ------------------------------------------------------------------------------
PostCommand:
   PostMessage, %WM_COMMAND%, % CMD_%A_ThisMenuItem%, 0, , ahk_ID %MenuID%
Return
; ------------------------------------------------------------------------------
Close:
   WinClose, ahk_id %MenuID%
Return
; ------------------------------------------------------------------------------
Kill:
   WinKill, ahk_id %MenuID%
Return
; ------------------------------------------------------------------------------
OpenFolder:
   Run, "%MenuFolder%"
Edit: Fixed code -> added MenuExt variable
Last edited by just me on 07 Mar 2016, 06:28, edited 1 time in total.
just me
Posts: 9617
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Sort list of running scripts alphabetically

07 Mar 2016, 06:29

I don't mind, but I fixed the code. ;)
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: Sort list of running scripts alphabetically

08 Mar 2016, 00:03

now those are just neat - I guess it was much easier than I had thought - I'm just not at that knowledge level :( Only one thing about the latest codes - when running a new script, the title gets inserted to the bottom of the listview instead of alphabetically (I'm guessing this is by design, as it's easy this way to find out which is the most recently-loaded script) - but is there a way to have the most recent-run scripts inserted in alphabetical order or is that a big change?
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Sort list of running scripts alphabetically

08 Mar 2016, 00:23

Just Me's is alphabetical (uses the sort option in the listview)
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: Sort list of running scripts alphabetically

08 Mar 2016, 09:17

When you run the code, the listview shows all your running scripts alphabetically. Next, if you go into your scripts folder and run another script of your choice, that script gets inserted to the bottom of the listview instead of being inserted alphabetically. How would you keep any new scripts alphabelticalized every time you add one?
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Sort list of running scripts alphabetically

08 Mar 2016, 13:41

Sort: The control is kept alphabetically sorted according to the contents of the first column.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Hansielein, Spawnova and 63 guests