Page 1 of 1

Win: "launch [doc] with [app]" fails, & workaround

Posted: Sat Apr 17, 2021 2:35 pm
by AxWald
Hi,

I'd like to have this confirmed before I write another bug report (or is there one already? not found)
This seems to be a very old bug, I see it in Win versions from 6.7.10 to the freshest RCs.
I've a sample stack attached, feel free to try yourselves - I hope it's not limited to my machines :)

When, in Windows, you call:

Code: Select all

   launch "D:/myDir/myFile.txt" with "D:/myDir/myApp.exe"
you'll see nothing happen. No myApp started, no myFile opened.
But you just have created a zombie process "myApp.exe".
If you'll repeat it, the same will happen.

"the result" will be empty or a (random?) number; when repeated, this again or sometimes "process is already open".
Find & kill the process in taskmanager, tab -2 (details).

This is evil, because it's the only way LC provides to open a document with a certain (not default) program. For instance, you may have Adobe suite installed, but you want a small, quick PDF viewer (SumatraPDF etc.) to safely display PDFs.
We could call:

Code: Select all

   get shell("D:/myDir/myApp.exe" "D:/myDir/myFile.txt")
This gives the expected result, but it will "freeze" LC until you close myApp again. Not really useful.

The workaround - we define a custom command:

Code: Select all

on LaunchWith theDocPath, theAppPath
   /* - Does what "launch [docPath] with [appPath]" is supposed to do -
   (both theDocPath & theAppPath are expected to be full paths (as from "ask file ...")
   
   Set up variables & defaultFolder:   */
   put the defaultFolder into myOldDF
   set itemdel to slash
   put item 1 to -2 of theAppPath into myAppPath
   put item -1 of theAppPath into myAppName
   if char 1 of myAppname = slash then delete char 1 of myAppName  --  WTF???
   set itemdel to comma
   set the defaultFolder to myAppPath
   
   /* Here's the beef:   */
   set the hideConsoleWindows to true
   get shell("start" && myAppName && quote & theDocPath & quote)
   set the defaultfolder to myOldDF
   if it is empty then
      /* The following line is for demonstration & may be commented:   */
      answer information "Done. You see, I responded before you closed the app!" titled "Info:"  
      
      exit LaunchWith
   end if
   /* In case something went wrong ...   */
   answer error "This shell call didn't work:" & CR & \
         "start" && myAppName && quote & theDocPath & quote & CR & \
         "in dir: " & myAppPath & CR & \
         "Result: " & it titled "Error:"
end LaunchWith
And then call simply:

Code: Select all

   Launchwith "D:/myDir/myFile.txt","D:/myDir/myApp.exe"
It's a bit convoluted, but it works.

Bonus bug: If the itemDel = slash, item -1 of a list may start with it ;-)

Have fun!

Edit: changed "function" to the correct "command" in the "Workaround" subtitle ...

Re: Win: "launch [doc] with [app]" fails, & workaround

Posted: Sat Apr 17, 2021 3:22 pm
by Thierry
AxWald wrote:
Sat Apr 17, 2021 2:35 pm
get shell("D:/myDir/myApp.exe" "D:/myDir/myFile.txt")
Hi Axwald,

Ok, first I think you have to pass only 1 string to the shell() function.

Second, I believe the command you are looking for new Win box is: start /b aCommand
which should be the equivalent of aCommand& in Mac/Unix.

Could be something like (pseudo code):

get shell( "start /b 'D:/myDir/myApp.exe' 'D:/myDir/myFile.txt'" )

If this doesn't work you could try changing the single quotes with double ones.

Sorry, no Win10 machine opened near me to test.
Might be worth a try...

Regards,

Thierry

Re: Win: "launch [doc] with [app]" fails, & workaround

Posted: Sat Apr 17, 2021 8:24 pm
by AxWald
Hi,
Thierry wrote:
Sat Apr 17, 2021 3:22 pm
Ok, first I think you have to pass only 1 string to the shell() function.
get shell("D:/myDir/myApp.exe" "D:/myDir/myFile.txt") works fine. But you can write:
get shell("D:/myDir/myApp.exe D:/myDir/myFile.txt"), too. Works as well :)
Thierry wrote:
Sat Apr 17, 2021 3:22 pm
Second, I believe the command you are looking for new Win box is: start /b aCommand
which should be the equivalent of aCommand& in Mac/Unix.
Actually, what does the job in the "LaunchWith" workaround is:
get shell("start" && myAppName && quote & theDocPath & quote) ;-)

"start" in Win is a beast - the only way I found to have it work as desired is to set the defaultFolder to the app's directiory, and use the app name w/o path: get shell("start myApp.exe" "D:/myDir/myFile.txt").

The "/b" parameter doesn't exist in Win, "/B" reads: "Starts app w/o opening a new window. The app ignores Strg-C ..." Sounds dangerous.

Have fun!