Previous post got slightly mangled so here it is again.
local rescmd resparams
local clipcontent=clip.get
clip.clear
local apphandle=win.handle("under")
local appprocess=win.getprocessid(apphandle)
local wmicmd= ;;+
?"/c wmic /output:clipboard process "++appprocess++" GET commandline"
file.runwait(0, env("COMSPEC"), wmicmd, "", "hide")
local res=clip.get
clip.set(clipcontent)
win.terminateprocess(appprocess)
local pattern=?"(?m)^(?(?=\x22))" ;;+
++?"(?<quotedcmd>\x22[^\x22\r\n]+\x22)\x20*+" ;;+
++?"(?<qparams>.*)" ;;+
++?"|^(?!CommandLine)" ;;+
++?"(?<unquotedcmd>[^\x20\r\n]+)\x20*+(?<uparams>.*)"
local pos=regex.pcrematch(pattern, res)
if (pos>0) do
local rescmd=regex.pcregetnamedsubstring("quotedcmd")
local resparams=regex.pcregetnamedsubstring("qparams")
if (rescmd=="") do
rescmd=regex.pcregetnamedsubstring("unquotedcmd")
resparams=regex.pcregetnamedsubstring("uparams")
endif
do(rescmd,resparams)
endif
/*
win.debug("==============")
win.debug("restored clipboard:", clip.get)
win.debug("res from wmic:", res)
win.debug("quoted:", regex.pcregetnamedsubstring("quotedcmd"))
win.debug("qparams:", regex.pcregetnamedsubstring("qparams"))
win.debug("unquoted:", regex.pcregetnamedsubstring("unquotedcmd"))
win.debug("uparams:", regex.pcregetnamedsubstring("uparams"))
win.debug("rescmd:", rescmd)
win.debug("resparams:", resparams)
win.debug("==============")
*/
quit