TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: alanjt on March 18, 2014, 05:15:48 PM

Title: find list of running programs and make specific one active
Post by: alanjt on March 18, 2014, 05:15:48 PM
Is it possible to retrieve a list of running programs and set a particular program as the active window?
Title: Re: find list of running programs and make specific one active
Post by: BlackBox on March 18, 2014, 05:20:35 PM
Is it possible to retrieve a list of running programs...

This may be of use:

http://forums.augi.com/showthread.php?152383-Process-is-Running&p=1247591&viewfull=1#post1247591



As for this:

... and set a particular program as the active window?

The answer is yes, it can be done - but the link above does not (yet?) allow for such functionality.

Cheers
Title: Re: find list of running programs and make specific one active
Post by: Lee Mac on March 18, 2014, 05:53:17 PM
Is it possible to retrieve a list of running programs and set a particular program as the active window?

I haven't looked into retrieving a list of running programs, but for setting the active window, I would suggest using the AppActivate (http://msdn.microsoft.com/en-us/library/wzcddbek%28v=vs.85%29.aspx) method of the WSH.
Title: Re: find list of running programs and make specific one active
Post by: Lee Mac on March 18, 2014, 06:13:32 PM
Is it possible to retrieve a list of running programs

Converting code from here (http://stackoverflow.com/questions/191206/how-to-get-list-of-running-applications-using-powershell-or-vbscript):

Code: [Select]
(defun runningapps ( / obj rtn tsk )
    (if (setq obj (vlax-create-object "word.application"))
        (progn
            (setq rtn
                (vl-catch-all-apply
                   '(lambda ( / l )
                        (vlax-for x (setq tsk (vlax-get-property obj 'tasks))
                            (if (= :vlax-true (vlax-get-property x 'visible))
                                (setq l (cons (vlax-get-property x 'name) l))
                            )
                        )
                        (vl-sort l '<)
                    )
                )
            )
            (vl-catch-all-apply 'vlax-invoke (list obj 'quit))
            (if (= 'vla-object (type tsk))
                (vlax-release-object tsk)
            )
            (vlax-release-object obj)
            (if (not (vl-catch-all-error-p rtn)) rtn)
        )
    )
)
(vl-load-com) (princ)
Title: Re: find list of running programs and make specific one active
Post by: Lee Mac on March 18, 2014, 06:26:00 PM
This will retrieve all running processes:

Code: [Select]
(defun runningprocesses ( / qry rtn srv wmi )
    (if (setq wmi (vlax-create-object "wbemscripting.swbemlocator"))
        (progn
            (setq rtn
                (vl-catch-all-apply
                   '(lambda ( / lst )
                        (setq srv (vlax-invoke wmi 'connectserver)
                              qry (vlax-invoke srv 'execquery "Select * from Win32_Process")
                        )
                        (vlax-for itm qry
                            (vlax-for prp (vlax-get itm 'properties_)
                                (if (= "name" (strcase (vlax-get prp 'name) t))
                                    (setq lst (cons (vlax-get prp 'value) lst))
                                )
                            )
                        )
                        lst
                    )
                )
            )
            (foreach obj (list qry srv wmi)
                (if (= 'vla-object (type obj))
                    (vlax-release-object obj)
                )
            )
            (if (vl-catch-all-error-p rtn)
                (prompt (vl-catch-all-error-message rtn))
                rtn
            )
        )
    )
)
Title: Re: find list of running programs and make specific one active
Post by: alanjt on March 18, 2014, 07:31:01 PM
Thanks, guys. I was at a brick wall and couldn't go any farther.
Title: Re: find list of running programs and make specific one active
Post by: Bhull1985 on March 19, 2014, 10:31:05 AM
Oh wow, this is nice.

So, with the (runningprocesses) routine by Lee there, it returns a list of all the running processes that we could place a call to (member) in order to determine if a particular app is running, for what i'm working on currently I could use this to determine if I need a call to (vlax-release-object) before running the main function.

Or, alternatively, a call to (member) with the returned list could do all sorts of nifty tricks such as (alert)ing that a software is not opened that should be, for purposes of some routines.
Could also use this in conjunction with a reactor to be able to tell if any programs are opened/closed....via a reactor to check this list....could log all instances of a software being opened and by which user, etc.....all sorts of tricks this function makes available, really cool Lee/alan!
Title: Re: find list of running programs and make specific one active
Post by: Lee Mac on March 19, 2014, 07:05:31 PM
Thanks Brandon  :-)

Here is another method for returning the running processes:

Code: [Select]
(defun runningprocesses ( / exe rtn str wsh )
    (if (setq wsh (vlax-create-object "wscript.shell"))
        (progn
            (setq rtn
                (vl-catch-all-apply
                   '(lambda nil
                        (setq exe (vlax-invoke-method wsh 'exec "%comspec% /c tasklist")
                              str (vlax-get-property exe 'stdout)
                        )
                        (vlax-invoke-method str 'readall)
                    )
                )
            )
            (if (= 'vla-object (type str))
                (vlax-release-object str)
            )
            (if (= 'vla-object (type exe))
                (progn
                    (vl-catch-all-apply 'vlax-invoke-method (list exe 'terminate))
                    (vlax-release-object exe)
                )
            )
            (vlax-release-object wsh)
            (if (vl-catch-all-error-p rtn)
                (prompt (vl-catch-all-error-message rtn))
                (princ rtn)
            )
        )
    )
    (princ)
)
(vl-load-com) (princ)