Search code examples
rubyoutlookshellexecute

How to detect opened application with Ruby


I'm making my first application using Ruby. The thing is that I open outlook using shell.ShellExecute('outlook.exe',... But I need to know if outllok is already open or not. (the script make an outlook instance everytime I call it... it's bad for me :p !)

Second question. When I open an application with shellExecute, is it possible to minimise it after opening ?

Thx !


Solution

  • All can be done using Ruby Standard library win32ole.


    Second question. When I open an application with shellExecute, is it possible to minimise it after opening ?

    Yes there is an option in doing so(taken from rubyonwindows):

    shell.ShellExecute(FILE, ARGUMENTS, DIRECTORY, OPERATION, SHOW)
    

    Now Look below:

    SHOW: Recommends how the window that belongs to the application that performs the operation should be displayed initially (0 = hidden, 1 = normal, 2 = minimized, 3 = maximized). The application can ignore this recommendation. If this parameter is not specified, the application uses its default value.


    But I need to know if outlook is already open or not.

    Yes there is an option in doing so(taken from Windows Management Instrumentation (WMI))

    require 'win32ole'
    
    shell = WIN32OLE.new('Shell.Application')
    
    wmi = WIN32OLE.connect("winmgmts://")
    processes = wmi.ExecQuery("select * from win32_process")
    processes.each{|i| p "already opened" if i.name == "OUTLOOK.EXE"}
    # => nil
    shell.ShellExecute('OUTLOOK.EXE')
    
    wmi = WIN32OLE.connect("winmgmts://")
    processes = wmi.ExecQuery("select * from win32_process")
    processes.each{|i| p "already opened" if i.name == "OUTLOOK.EXE"}
    # => "already opened"