AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Script Manager

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Veovis



Joined: 13 Feb 2006
Posts: 390
Location: Utah

PostPosted: Wed May 17, 2006 6:58 pm    Post subject: Script Manager Reply with quote

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:

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
}

_________________

"Power can be given overnight, but responsibility must be taught. Long years go into its making."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Wed May 17, 2006 7:50 pm    Post subject: Reply with quote

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.autohotkey.com/forum/viewtopic.php?p=55168
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Wed May 17, 2006 8:20 pm    Post subject: Reply with quote

My list is empty. Do I need to edit some configuration settings?
Back to top
View user's profile Send private message
Veovis



Joined: 13 Feb 2006
Posts: 390
Location: Utah

PostPosted: Wed May 17, 2006 8:43 pm    Post subject: Reply with quote

@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.
_________________

"Power can be given overnight, but responsibility must be taught. Long years go into its making."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Wed May 17, 2006 8:49 pm    Post subject: Reply with quote

Veovis wrote:
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).
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Wed May 17, 2006 8:54 pm    Post subject: Reply with quote

@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.
Back to top
View user's profile Send private message
Veovis



Joined: 13 Feb 2006
Posts: 390
Location: Utah

PostPosted: Wed May 17, 2006 8:57 pm    Post subject: Reply with quote

@Laszlo What OS do you have? I cant figure out why that isnt working... maybe evl/shimonov code would be better after all
_________________

"Power can be given overnight, but responsibility must be taught. Long years go into its making."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Wed May 17, 2006 9:05 pm    Post subject: Reply with quote

XP SP2
Back to top
View user's profile Send private message
Zippo()
Guest





PostPosted: Wed May 17, 2006 11:09 pm    Post subject: Reply with quote

Win XP Home SP2:

Back to top
Veovis



Joined: 13 Feb 2006
Posts: 390
Location: Utah

PostPosted: Wed May 17, 2006 11:32 pm    Post subject: Reply with quote

Now I'm really confused...

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


Quote:
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?
_________________

"Power can be given overnight, but responsibility must be taught. Long years go into its making."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Veovis



Joined: 13 Feb 2006
Posts: 390
Location: Utah

PostPosted: Wed May 17, 2006 11:52 pm    Post subject: Reply with quote

@Laszlo try

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


in either CMD or in the script

@Zippo() does anything relating to WMIC work?
_________________

"Power can be given overnight, but responsibility must be taught. Long years go into its making."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Zippo()
Guest





PostPosted: Thu May 18, 2006 2:07 am    Post subject: Reply with quote

Veovis wrote:
@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 Very Happy

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 Smile
Back to top
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Thu May 18, 2006 2:31 am    Post subject: Reply with quote

Veovis wrote:
@Laszlo try
Code:
WMIC process where name='autohotkey.exe' get processid,commandline
It worked. I got on the command line:
Code:
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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group