safe shell command

Rich Herz herz at ames.ucsd.edu
Thu Feb 20 23:25:01 EST 2003


Alex Rice wrote:

I have gleaned these tidbits from the list. Can anyone add to this code
for making sure the shell command is going to run correctly, minimized
on all windows variants? Does anyone know if Win XP has one of these
two variants or does it have another "command" command?

Alex,

Win XP has both cmd.exe (the default) and command.com. The latter seems to have
different options and/or behavior than command.com on Win 98, which only has
command.com as I can see.  Below is script to run an application "myapp.exe"
that is at a path gMyappPath that may contain spaces in path name.

Setting MC/Rev hideConsoleWindows to true may help, and I do it, but
can't rely on it.  Even with script below I get a minimized console
button appearing in the Win task bar at bottom of screen while
myapp.exe runs, at least on one of the Win platforms (can't remember
now but could check)..

Rich Herz

on runWin32
 
  put gMyappPath into lWinPath
  replace "/" with "\" in lWinPath
 
  # check for existance of the executable
  # name must match name below in two places
  set the directory to gModuleSupportPath
  if there is a file "myapp.exe" then
    # exe file is there
  else
    answer "Can't run! The folder or file is missing or was renamed!"
    exit to metacard
  end if
 
  # different Windows OS have different shell command programs as default
  # on Win 98 command.com is default
  # on Win XP cmd.exe is default
  # on Win XP, command.com options and behavior different than same on Win 98
 
  switch shellCommand
  case "command.com"
    runCommandDotCom
    break
  case "cmd.exe"
    runCmdDotExe
    break
  default
    answer "unknown shellCommand"
    break
  end switch
 
end runWin32

on runCmdDotExe
  # running on Win XP or similar
 
  # can't just use "start" and full path to executable because of possible
spaces in path
  # so have to cd to directory first (doesn't care about spaces in path), then
"start"
  # cannot use two separate get shell lines
  # see "&" below to put two or more command lines in one get shell()
 
  # the command continuation char in cmd.exe is & (don't confuse with MC &)
  put "get shell(" & quote & "cd" && lWinPath & "& start /min /high /wait
myapp.exe" & quote & ")" into todo
 
  do todo
 
  # nothing comes back to MC "it" or the result as with command.com on Win 98
 
end runCmdDotExe


on runCommandDotCom
 
  # running on Win 98 or similar
 
  # because of get MC shell() syntax requiring quotes around entire input
string
  # and because of possible spaces in path and because command.com's cd
  # is sensitive to spaces in path
  # need to write a batch file and do "cd" with quotes around path followed by
  # command in batch file to execute exe file
  # and finally have MC shell() execute the batch file
  # the command continuation character in command.com is | but doesn't
  # help because need quotes around entire "get shell" and around path for "cd"
 
  put "C:\temp_mc.bat" into tBatPath
  put "file:" & tBatPath into tBatUrl
 
  # need literal quote characters around path with spaces for command.com
  put "cd" && quote & lWinPath & quote into temp
  put cr & "myapp.exe" & cr after temp
 
  try
    put temp into url tBatUrl
  catch errornum
    answer "Error: can't write command file to C:\ and so can't run!"
    exit to metacard
  end try
 
  # no priority options or /b as on Win XP cmd.exe, and /m here not /min
  # want /wait option so focus doesn't return to MC until after execution is
done
 
  put "get shell(" & quote & "start /m /wait" && tBatPath & quote & ")" into
todo

  do todo
 
  # command.com, at least on Win98, returns entire console contents to MC "it"
  # and so could check .exe output (Fortran program "STOP") for good run but
  # have to check for existance of full output files for cmd.exe anyway
 
  # clean up
  if there is a file tBatPath then delete file tBatPath
 
end runCommandDotCom



More information about the use-livecode mailing list