Author Topic: find list of running programs and make specific one active  (Read 2584 times)

0 Members and 1 Guest are viewing this topic.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
find list of running programs and make specific one active
« 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?
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

BlackBox

  • King Gator
  • Posts: 3770
Re: find list of running programs and make specific one active
« Reply #1 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
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: find list of running programs and make specific one active
« Reply #2 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 method of the WSH.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: find list of running programs and make specific one active
« Reply #3 on: March 18, 2014, 06:13:32 PM »
Is it possible to retrieve a list of running programs

Converting code from here:

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)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: find list of running programs and make specific one active
« Reply #4 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
            )
        )
    )
)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: find list of running programs and make specific one active
« Reply #5 on: March 18, 2014, 07:31:01 PM »
Thanks, guys. I was at a brick wall and couldn't go any farther.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Bhull1985

  • Guest
Re: find list of running programs and make specific one active
« Reply #6 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!

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: find list of running programs and make specific one active
« Reply #7 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)