• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1296
  • Last Modified:

How to run a shortcut (.lnk) file using WScript with JScript?

I have a shortcut file - somefile.lnk - that I'm trying to launch in a script.  I need to change the path before the shortcut runs, then change the path back aftwerwards.

I started with the following:

var objShell = WScript.CreateObject("WScript.Shell");
var objEnv = objShell.Environment;
var oldPath = objEnv.Item( "Path" );
var newPath = oldPath + ";C:\somepath;";
objEnv.Item( "Path" ) = newPath;
// launch the shortcut, pausing the script 'til the shortcut is done
objShell.Run( "somefile.lnk", 1, true );
objEnv.Item( "Path" ) = oldPath;

When I run this, though, I get an error message:  unable to wait for process.
(Note:  It does run the program, but it never completes the rest of the script.)

Next, I tried using the Exec method instead, adding a loop to wait for status from the shortcut:

objShell.Exec( "somefile.lnk" );
while( objShell.Exec.Status === 0 ) {
     WScript.Sleep( 100 );
}

This version, however, never launches the shortcut, and returns this error message:
somefile.lnk is not a valid Win32 application.

I'm using the shortcut because all of the app's parameters, start, and target information is present in the shortcut, and I didn't want to mess with trying to ensure all the settings were correct in my script - I thought this should just be easy.

Why won't Run pause the script on launching the shortcut, and why won't Exec launch the shortcut at all???  And, what can I do instead?
0
lisfolks
Asked:
lisfolks
  • 4
  • 3
1 Solution
 
RobSampsonCommented:
Do you actually need to wait for the application to finish?  As soon as you run the shortcut, whether the program is still running or not, you can still change the shortcut back, so just change this
objShell.Run( "somefile.lnk", 1, true );

to this
objShell.Run( "somefile.lnk", 1, false );

and the code will immediately run the shortcut, and change it back again.

If you want to, you could add
wscript.sleep 5000

to wait 5 seconds in between.

Regards,

Rob.
0
 
lisfolksAuthor Commented:
The application launched by the shortcut needs the changed path.  If I don't wait for the application to finish (which could potentially be a very long time) before changing the path back, the application won't work.  It requires the changed path until it is closed.

If I could pause the script until a user responded to a prompt, say, that might work...  I haven't looked that up as I just thought of it while reading your response...  (I'm a total newbie to WScript.)
0
 
RobSampsonCommented:
>> If I don't wait for the application to finish (which could potentially be a very long time) before changing the path back, the application won't work.

That surprises me.  Admittedly, I haven't tested that scenario, but I would have thought that once the shortcut has been run, then parameters and path are in memory, so you should be able to modify the shortcut without any effect on the currently running program.

Anyway, you could also monitor the process of the exe that is running, and just change the shortcut afterwards.

Regards,

Rob.
strComputer = ".";

var objWMIService = GetObject("winmgmts:\\\\" + strComputer + "\\root\\cimv2");
var running = true;
while( running == true )
{
    WScript.Sleep( 5000 );
	var colItems = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'notepad.exe'");
	var enumItems = new Enumerator(colItems); 
    running = false;
	for (; !enumItems.atEnd(); enumItems.moveNext())
	{
		var objItem = enumItems.item();
		running = true;		
	}
}
WScript.Echo("Application has finished.");

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
lisfolksAuthor Commented:
I'm confused a bit:  I'm not modifying the shortcut, rather I'm changing the PATH environment variable in Windows while the app launched by the shortcut is running.  This app is used only occasionally, so I want to change the Windows PATH variable only temporarily.  Is that what you mean by "changing the shortcut"?

Now, let me understand your code suggestion.  It looks like it's grabbing a list of processes with the name 'notepad.exe' (which I presume I would replace with the name of my app, 'someapp.exe').  It sets up an enumerator to go through that list.  Then, as long as someapp.exe is running (and thus appears in that list), the code will continue looping.  Is that about right?
0
 
RobSampsonCommented:
Oh, my mistake.  I thought you meant you were changing the path of the shortcut's executable!  OK, so I understand why you can't change the PATH environment variable during the program execution then.

In that case, my suggestion should work, and you have it's workings correct.  It views the current list of processes where the process name equals 'someapp.exe'.  If it is found, then the program is still running, and it will loop.  When the process is no longer visible, the loop will exit, and you can change your PATH value back.

Regards,

Rob.
0
 
lisfolksAuthor Commented:
Thanks very much, Rob!  That did the trick!
0
 
RobSampsonCommented:
No problem. Thanks for the grade.

Rob.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now