Jump to content

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

Script Manager


  • Please log in to reply
12 replies to this topic
Veovis
  • Members
  • 389 posts
  • Last active: Mar 17 2009 12:24 AM
  • Joined: 13 Feb 2006
Here is a script that uses the CMD line WMIC to retrieve a list of all running scripts. You can then right click on them to Edit, Reload or Close them. If it does not work, open CMD and type WMIC and wait for it to install.

Comments are appreciated! It works on my computer, but there might be bugs, please tell me if you find any.

Heres the code:

;create Running Scripts context menu
menu,ScriptsMenu,Add,Edit Script,ScriptEdit
menu,ScriptsMenu,Add,Reload Script,ScriptReload
menu,ScriptsMenu,Add,Close Script,ScriptClose

gui, add, text,,Below is a list of all currently running, uncompiled AutoHotkey scripts.`nRight Click on a script to Edit, Reload, or close it.
gui, add, button,gUpdateList,Update List
gui, add, ListView,AltSubmit r20 w580 vScriptList gListViewClick,ScriptName:|PID

gui, show,,Script Manager
return


UpdateList:
output := CMDret_RunReturn("WMIC path win32_process get Processid,Commandline")
LV_Delete()
loop, parse, output,`n,
{
   ifinstring,A_loopfield,autohotkey.exe
	{
      ;extract the script name and pid from each line that contains "autohotkey.exe"
      stringgetpos,pos3,A_loopfield,",l3
      stringgetpos,pos4,A_loopfield,",l4
      pos3 := pos3 + 1
      stringmid,scriptname,A_loopfield,% pos3 +1 ,% pos4 - pos3
      stringtrimleft,pid,A_loopfield,% pos4+1
      stringreplace,pid,pid,%A_space%,,A
      stringtrimright,pid,pid,2
      LV_Add("",scriptname,pid)
   }
   
}
LV_ModifyCol()
return

ListViewClick:    ;open context menu
if A_guievent = RightClick
{
   if A_eventinfo > 0
   {
      LV_GetText(rc_scriptname,A_eventinfo,1)
      LV_GetText(rc_scriptPID,A_eventinfo,2)
      menu, ScriptsMenu, show, %A_guix%, %A_guiy%
   }
}

return

;actions to perform on scripts

ScriptEdit:   ; read from the regsitry how this person edits scripts and run accordingly
regread,editcommand,HKey_Classes_root,Autohotkeyscript\shell\edit\command
stringreplace,editcommand,editcommand,`%1,%rc_scriptname%
run, %editcommand%
return

ScriptReload:    ;extract the scripts dir and run it from its own dir
stringreplace,rc_scriptname,rc_scriptname,/,\,a
stringgetpos,pos,rc_scriptname,\,r1
stringleft,dir,rc_scriptname,%pos%
run, %rc_scriptname%, %dir%
return

ScriptClose:    ;end the process using its pid
process, close, %rc_scriptPID%
Gosub, UpdateList
return


;CMD return function

CMDret_RunReturn(CMDin)   ; CMDret 1.08 beta by corrupt
{
  Global cmdretPID
  idltm := A_TickCount + 20
  CMsize = 1
  VarSetCapacity(CMDout, 1, 32)
  VarSetCapacity(sui,68, 0)
  VarSetCapacity(pi, 16, 0)
  VarSetCapacity(pa, 12, 0)
  Loop, 4 {
    DllCall("RtlFillMemory", UInt,&pa+A_Index-1, UInt,1, UChar,12 >> 8*A_Index-8)
    DllCall("RtlFillMemory", UInt,&pa+8+A_Index-1, UInt,1, UChar,1 >> 8*A_Index-8)
  }
  IF (DllCall("CreatePipe", "UInt*",hRead, "UInt*",hWrite, "UInt",&pa, "Int",0) <> 0) {
    Loop, 4
      DllCall("RtlFillMemory", UInt,&sui+A_Index-1, UInt,1, UChar,68 >> 8*A_Index-8)
    DllCall("GetStartupInfo", "UInt", &sui)
    Loop, 4 {
      DllCall("RtlFillMemory", UInt,&sui+44+A_Index-1, UInt,1, UChar,257 >> 8*A_Index-8)
      DllCall("RtlFillMemory", UInt,&sui+60+A_Index-1, UInt,1, UChar,hWrite >> 8*A_Index-8)
      DllCall("RtlFillMemory", UInt,&sui+64+A_Index-1, UInt,1, UChar,hWrite >> 8*A_Index-8)
      DllCall("RtlFillMemory", UInt,&sui+48+A_Index-1, UInt,1, UChar,0 >> 8*A_Index-8)
    }
    IF (DllCall("CreateProcess", Int,0, Str,CMDin, Int,0, Int,0, Int,1, "UInt",0, Int,0, Int,0, UInt,&sui, UInt,&pi) <> 0) {
      Loop, 4
        cmdretPID += *(&pi+8+A_Index-1) << 8*A_Index-8
      Loop {
        idltm2 := A_TickCount - idltm
        If (idltm2 < 10) {
          DllCall("Sleep", Int, 10)
          Continue
        }
        IF (DllCall("PeekNamedPipe", "uint", hRead, "uint", 0, "uint", 0, "uint", 0, "uint*", bSize, "uint", 0 ) <> 0 ) {
          Process, Exist, %cmdretPID%
          IF (ErrorLevel OR bSize > 0) {
            IF (bSize > 0) {
              VarSetCapacity(lpBuffer, bSize+1)
              IF (DllCall("ReadFile", "UInt",hRead, "Str", lpBuffer, "Int",bSize, "UInt*",bRead, "Int",0) > 0) {
                IF (bRead > 0) {
                  TRead += bRead
                  VarSetCapacity(CMcpy, (bRead+CMsize+1), 0)
                  CMcpy = a
                  DllCall("RtlMoveMemory", "UInt", &CMcpy, "UInt", &CMDout, "Int", CMsize)
                  DllCall("RtlMoveMemory", "UInt", &CMcpy+CMsize, "UInt", &lpBuffer, "Int", bRead)
                  CMsize += bRead
                  VarSetCapacity(CMDout, (CMsize + 1), 0)
                  CMDout=a   
                  DllCall("RtlMoveMemory", "UInt", &CMDout, "UInt", &CMcpy, "Int", CMsize)
                }
              }
            }
          }
          ELSE
            break
        }
        ELSE
          break
        idltm := A_TickCount
      }
    }
    cmdretPID=
    DllCall("CloseHandle", UInt, hWrite)
    DllCall("CloseHandle", UInt, hRead)
  }
  IF (StrLen(CMDout) < TRead) {
    VarSetCapacity(CMcpy, TRead, 32)
    TRead2 = %TRead%
    Loop {
      DllCall("RtlZeroMemory", "UInt", &CMcpy, Int, TRead)
      NULLptr := StrLen(CMDout)
      cpsize := Tread - NULLptr
      DllCall("RtlMoveMemory", "UInt", &CMcpy, "UInt", (&CMDout + NULLptr + 2), "Int", (cpsize - 1))
      DllCall("RtlZeroMemory", "UInt", (&CMDout + NULLptr), Int, cpsize)
      DllCall("RtlMoveMemory", "UInt", (&CMDout + NULLptr), "UInt", &CMcpy, "Int", cpsize)
      TRead2 --
      IF (StrLen(CMDout) > TRead2)
        break
    }
  }
  StringTrimLeft, CMDout, CMDout, 1
  Return, CMDout
}

Posted Image
"Power can be given overnight, but responsibility must be taught. Long years go into its making."

evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
Just in case you're interested, it's possible to retrieve all running scripts purely with AHK thanks to some code from shimanov that can retrieve a list of processes and the command line that they were started with. I used it in my AutoHotkey Script Restarter:
http://www.autohotke...pic.php?p=55168

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
My list is empty. Do I need to edit some configuration settings?

Veovis
  • Members
  • 389 posts
  • Last active: Mar 17 2009 12:24 AM
  • Joined: 13 Feb 2006
@evl: Yeah i saw your script restarter. I was going to adapt that for my script manager, but i was really confused by the code... I understood that somehow you got a list all running PIDs and then requested what each one is, but i thought this was simpler.

@Laszlo: Hmmm. Open up CMD and type WMIC path win32_process get Processid,Commandline and see if you get a list of running processes in your command prompt. WMIC has to "install" for some reason the first time you use it so it might take a second.

Also, i forgot to have it update when you first open the program so you have to click update as soon as you start the script.
Posted Image
"Power can be given overnight, but responsibility must be taught. Long years go into its making."

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

Open up CMD and type WMIC path win32_process get Processid,Commandline and see if you get a list of running processes in your command prompt.

I only get "Invalid XML content". If I remove "Commandline", I get a long list of numbers (Process ID's).

evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
@Veovis: Yes, basically it builds a list of the paths to the scripts - if you look at the variables when running the script you can see what gets generated - it's been a while since I wrote it so can't recall off the top of my head.

Veovis
  • Members
  • 389 posts
  • Last active: Mar 17 2009 12:24 AM
  • Joined: 13 Feb 2006
@Laszlo What OS do you have? I cant figure out why that isnt working... maybe evl/shimonov code would be better after all
Posted Image
"Power can be given overnight, but responsibility must be taught. Long years go into its making."

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
XP SP2

Zippo()
  • Guests
  • Last active:
  • Joined: --
Win XP Home SP2:

Posted Image

Veovis
  • Members
  • 389 posts
  • Last active: Mar 17 2009 12:24 AM
  • Joined: 13 Feb 2006
Now I'm really confused...

The availability of WMI information does vary across different versions of Windows


To run WMIC requires administrator rights.



See here for more info about wmic

I'm totally perplexed as yo why WMIC returns an error for you Zippo()... does WMIC in cmdprompt do anything?
Posted Image
"Power can be given overnight, but responsibility must be taught. Long years go into its making."

Veovis
  • Members
  • 389 posts
  • Last active: Mar 17 2009 12:24 AM
  • Joined: 13 Feb 2006
@Laszlo try

WMIC process where name='autohotkey.exe' get processid,commandline

in either CMD or in the script

@Zippo() does anything relating to WMIC work?
Posted Image
"Power can be given overnight, but responsibility must be taught. Long years go into its making."

Zippo()
  • Guests
  • Last active:
  • Joined: --

@Zippo() does anything relating to WMIC work?


Well, to be honest, I really don't know. First I've heard of WMIC.

A Google search turned up a bunch of tech info that I don't feel like deciphering right now. I did note on the MS articles concerning WMIC that Win XP Home is one of the OS that the articles applies to, so I'm guessing it is supposed to be there somewhere :D

I can't run 'WMCI' or ' WMIC path win32_process get Processid,Commandline' from the CMD without the error, and of course I get only an empty list with the script.

Hope this helps some :)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

@Laszlo try

WMIC process where name='autohotkey.exe' get processid,commandline

It worked. I got on the command line:
CommandLine                                                               ProcessId
"C:\Program Files\AutoHotkey\AutoHotkey.exe"  "C:\t\AutoHotKey\main.ahk"  1680
Changing your script, accordingly, also works just fine. I get the ID's of both of my running scripts in the ListView.