How to prevent multiple instances of a non-LC Windows app?

Graham Samuel livfoss at mac.com
Fri May 20 08:40:58 EDT 2011


Correction - in the real VB script I had not forgotten the quote before the first instance of 'firefox.exe' in the script below. Just a transcription error. The actual script that fails has the line

If objProcess.name = "firefox.exe" then

Sorry for that

Graham

========
Thanks to all who replied to this and my other queries about "tasklist" on Windows (not available on the basic version of XP that most of my users are running). Basically there does seem to be a bug in the LC 'launch' command, and I will report it. Meanwhile I am trying to follow Mike Bonner's advice as shown in the quote below. As I didn't want to use a separate file, I put this (VB) script into a field called "VBScript" in a test stack:

Set WshShell = WScript.CreateObject ("WScript.Shell")
Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")


For Each objProcess in colProcessList
If objProcess.name = firefox.exe" then
vFound = True
End if
Next
If vFound = True then
WshShell.AppActivate "firefox"
Else
WshShell.Run "firefox.exe"
End If 

Following Mike, I'm just using FireFox as a test program - in the end I will use the particular program I'm interested in.

I then run this LC script from a button of my program which has been compiled as a Windows standalone:

local scriptName
on mouseUp
   put "VBScript" into scriptName
   do fld "VBScript" as scriptName
   answer "Result is:"&&(the result)
end mouseUp

(BTW I found that using the word 'VBScript' directly results in a compile error, even tho VBScript is in the list of alternateLanguages - dunno why).

Anyway the result is 'execution error'.

As I know zilch about VB I am stuck  - can anyone suggest what I should have done? Do I **have** to use 'shell' - I am reluctant because it takes me more into unknown regions.

Finally, I should say that I do have a copy of FireFox on the PC, although I find that Windows Explorer doesn't show the '.exe' at the end of its name. So a subsidiary question is, how can I know exactly what Windows thinks the entire name of my particular executable program is, particularly if there's a space in the name (eg. "My Program" - does that get called as "My Program.exe" or what?)

TIA

Graham

On Wed, 18 May 2011 15:39:13 -0600, Mike Bonner <bonnmike at gmail.com> wrote:

> I can't test with win xp, but you might put the following into a .vbs file.
> 
> *Set WshShell = WScript.CreateObject ("WScript.Shell")*
> *Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from
> Win32_Process")*
> *
> *
> *For Each objProcess in colProcessList*
> *If objProcess.name = "firefox.exe" then*
> *vFound = True*
> *End if*
> *Next*
> *If vFound = True then*
> *WshShell.AppActivate "Firefox"*
> *Else*
> *WshShell.Run "Firefox.exe"*
> *End If *
> 
> In my example, it will check for firefox, if its running it will focus on
> it, if not it will start a new instance.  To use it, do a shell call.
> 
> get shell("myScript.vbs")
> 
> I'm sure it could be easily modified so you could pass it the program to
> find as a parameter, but if its always the same hard coded should be fine.
> 
> Got the script from here.
> http://www.tek-tips.com/viewthread.cfm?qid=1492889&page=6 and changed it
> slightly.
> 
> Hope it works on xp home, works fine on 7.





More information about the use-livecode mailing list