سؤال

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 !

هل كانت مفيدة؟

المحلول

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"

نصائح أخرى

To check whether Outlook is running, first install sys-proctable:

gem install sys-proctable

Then you can do this:

require 'sys/proctable'

puts Sys::ProcTable.ps.select{ |pe| pe.caption == "OUTLOOK.EXE" }.any?

Which returns true if there is a process with the name "OUTLOOK.EXE". This should be sufficient.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top