Advertisement
jcunews

Choice.hta

Mar 25th, 2022
775
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html>
  3. <head>
  4.   <meta http-equiv="x-ua-compatible" content="IE=9" />
  5.   <hta:application id=hta border=dialog contextmenu=no maximizebutton=no />
  6.   <script id=helptxt type=text>
  7. Choice, March 2022.
  8. https://www.reddit.com/user/jcunews1
  9. https://pastebin.com/u/jcunews
  10. https://greasyfork.org/en/users/85671-jcunews
  11.  
  12. Usage: choice.hta [options]
  13.  
  14. Options:
  15. /t {str}  Set dialog title. Default is "Choice".
  16. /m {str}  Set dialog message. Default is predefined.
  17. /u    Allow multiple selections using checkboxes.
  18. /c {str}  Set text of choices. "`" separated. Required.
  19. /d {num}  Set index of default choice. Default: 1.
  20.       If /u is used, multiple indexes are specified as command separated.
  21. /n {num}  Set number of choice columns. Default: 1.
  22. /e {num}  Set timeout in seconds. Default: 0 (no timeout)
  23. /o    Choose OK on timeout instead of Cancel.
  24. /v    Use selected choice's text instead of index, as output.
  25. /l    Format the output to put each item in its own line. For use with /u.
  26. /f    Only use file as output.
  27.  
  28. If the user doesn't choose OK, no output will be written.
  29.  
  30. Unless /f option is specified, the output is written to the standard output
  31. (of a console window or redirection). If standard output is not available,
  32. the output is written into a file named "choice.out" in the temporary
  33. directory.
  34.  
  35. When /u is used, the output is a comma separated choice indexes.
  36. If /v is used, the output is "`" separated choice text.</script>
  37.   <style>
  38. #fs{display:none}
  39. html{background:buttonface}
  40. #content{display:inline-block}
  41. #choices table{border-collapse:collapse}
  42. #choices table td:last-child{word-break:break-all}
  43. #cwrap{margin-top:1em;overflow-y:auto}
  44. label{display:block}
  45. input{vertical-align:middle}
  46. span{display:inline-block;vertical-align:middle}
  47. #actions{margin-top:1em;text-align:center}
  48. #buttons{display:inline-block;white-space:nowrap}
  49. #cancel{margin-left:3ex}
  50.   </style>
  51. </head>
  52. <body>
  53.   <object id=fs classid="clsid:0D43FE01-F093-11CF-8940-00A0C9054228"></object>
  54.   <form id=content>
  55.     <div id=message></div>
  56.     <div id=cwrap><table id=choices></table></div>
  57.     <div id=actions>
  58.       <div id=buttons>
  59.         <button id=ok>OK</button><button id=cancel>Cancel</button>
  60.       </div>
  61.     </div>
  62.   </form>
  63.   <script language=vbscript>
  64. sub document_onkeypress(e)
  65.   if e.keycode = 27 then close
  66. end sub
  67.  
  68. sub ok_onclick(e)
  69.   e.returnvalue = false
  70.   r = ""
  71.   if ubound(achoices) > 0 then
  72.     set cs = content.choice
  73.   else
  74.     cs = array(content.choice)
  75.   end if
  76.   i = 0
  77.   for each c in cs
  78.     if c.checked then
  79.       if aline then
  80.         if atext then
  81.           r = r & c.value & vbcrlf
  82.        else
  83.          r = r & (i + 1) & vbcrlf
  84.        end if
  85.      elseif atext then
  86.        r = r & c.value & "`"
  87.      else
  88.        r = r & (i + 1) & ","
  89.      end if
  90.    end if
  91.    i = i + 1
  92.  next
  93.  if not aline then r = r & vbcrlf
  94.  if not afile then
  95.    on error resume next
  96.    fs.getstandardstream(1).write(r)
  97.    if err.number <> 0 then
  98.      on error goto 0
  99.      afile = true
  100.    end if
  101.  end if
  102.  if afile then
  103.    set f = fs.getspecialfolder(2).createtextfile("choice.out")
  104.    f.write r
  105.    f.close
  106.  end if
  107.  close
  108. end sub
  109.  
  110. sub cancel_onclick(e)
  111.  e.returnvalue = false
  112.  close
  113. end sub
  114.  
  115. sub timeout
  116.  atimeout = atimeout - 1
  117.  if atimeoutok then
  118.    content.ok.innertext = "OK (" & atimeout & ")"
  119.  else
  120.    content.cancel.innertext = "Cancel (" & atimeout & ")"
  121.  end if
  122.  if atimeout <= 0 then
  123.    clearinterval ti
  124.    if atimeoutok then
  125.      content.ok.click
  126.    else
  127.      close
  128.    end if
  129.  end if
  130. end sub
  131.  
  132. sub help
  133.  document.body.innerHTML = "<div style=""white-space:pre;" _
  134.     & "display:inline-block;word-wrap:break-word;font:16px/16px monospace"">" _
  135.     & mid(helptxt.text, instr(helptxt.text, vblf) + 1) & "</div>"
  136.  resizeto screen.width, screen.height
  137.  resizeto _
  138.    document.body.children(0).offsetWidth + (screen.width - innerwidth) * 3, _
  139.    document.body.children(0).offsetHeight + (screen.height - innerheight) * 2
  140. end sub
  141.  
  142. function getArg
  143.  s = m(i).submatches(0)
  144.  if left(s, 1) = """" then
  145.    if right(s, 1) = """" then
  146.      s = trim(mid(s, 2, len(s) - 2))
  147.    else
  148.      s = ltrim(mid(s, 2))
  149.    end if
  150.  end if
  151.  getArg = s
  152. end function
  153.  
  154. function getOptValue
  155.  i = i + 1
  156.  if i < m.count then
  157.    getOptValue = getArg
  158.  else
  159.    getOptValue = null
  160.  end if
  161. end function
  162.  
  163. atitle = "Choice"
  164. amessage = ""
  165. amulti = false
  166. adefault = 1
  167. acolumns = 1
  168. atimeout = 0
  169. atimeoutok = false
  170. atext = false
  171. aline = false
  172. afile = false
  173. set x = new regexp
  174. x.pattern = "\s*(""[^""]+""|\S+)"
  175. x.global = true
  176. set m = x.execute(hta.commandline)
  177. i = 1
  178. s = ""
  179. do while i < m.count
  180.  if getArg = "" then
  181.    achoices = empty
  182.    exit do
  183.  end if
  184.  if left(s, 1) = "/" then
  185.    s = ucase(s)
  186.    select case s
  187.      case "/T"
  188.        atitle = getOptValue
  189.        if isnull(atitle) then
  190.          achoices = empty
  191.          exit do
  192.        end if
  193.      case "/M"
  194.        amessage = getOptValue
  195.        if isnull(amessage) then
  196.          achoices = empty
  197.          exit do
  198.        end if
  199.      case "/U"
  200.        amulti = true
  201.      case "/C"
  202.        achoices = getOptValue
  203.        if isnull(achoices) then
  204.          achoices = empty
  205.          exit do
  206.        end if
  207.      case "/D"
  208.        adefault = getOptValue
  209.        if isnull(adefault) then
  210.          achoices = empty
  211.          exit do
  212.        end if
  213.      case "/N"
  214.        acolumns = getOptValue
  215.        if isnull(acolumns) then
  216.          achoices = empty
  217.          exit do
  218.        end if
  219.        on error resume next
  220.        acolumns = clng(acolumns)
  221.        if (err.number <> 0) or (acolumns < 1) then
  222.          on error goto 0
  223.          achoices = empty
  224.          exit do
  225.        end if
  226.        on error goto 0
  227.      case "/E"
  228.        atimeout = getOptValue
  229.        if isnull(atimeout) then
  230.          achoices = empty
  231.          exit do
  232.        end if
  233.        on error resume next
  234.        if (err.number <> 0) or (atimeout < 0) then
  235.          on error goto 0
  236.          achoices = empty
  237.          exit do
  238.        end if
  239.        atimeout = clng(atimeout)
  240.        on error goto 0
  241.      case "/O"
  242.        atimeoutok = true
  243.      case "/V"
  244.        atext = true
  245.      case "/L"
  246.        aline = true
  247.      case "/F"
  248.        afile = true
  249.      case else
  250.        achoices = empty
  251.        exit do
  252.    end select
  253.  else
  254.    achoices = empty
  255.    exit do
  256.  end if
  257.  i = i + 1
  258. loop
  259. if amessage = "" then
  260.  if amulti then
  261.    amessage = "Please select choice(s)."
  262.  else
  263.    amessage = "Please select a choice."
  264.  end if
  265. end if
  266. if not isempty(achoices) then
  267.  achoices = split(achoices, "`")
  268.  adefault = split(adefault, ",")
  269.  on error resume next
  270.  if amulti then
  271.    for i = 0 to ubound(adefault)
  272.      err.clear
  273.      adefault(i) = clng(adefault(i))
  274.      if (err.number <> 0) or (adefault(i) < 1) then
  275.        achoices = empty
  276.        exit for
  277.      end if
  278.      if adefault(i) > (ubound(achoices) + 1) then _
  279.        adefault(i) = ubound(achoices) + 1
  280.    next
  281.  else
  282.    adefault(0) = clng(adefault(0))
  283.    if (err.number <> 0) or (adefault(0) < 1) then
  284.      achoices = empty
  285.    elseif adefault(0) > (ubound(achoices) + 1) then
  286.      adefault(0) = ubound(achoices) + 1
  287.    end if
  288.  end if
  289.  on error goto 0
  290. end if
  291. if not isempty(achoices) then
  292.  if acolumns > (ubound(achoices) + 1) then acolumns = ubound(achoices) + 1
  293.  
  294.  f = fs.getspecialfolder(2).path & "\choice.out"
  295.  if fs.fileexists(f) then fs.deletefile f
  296.  document.title = atitle
  297.  message.innertext = amessage
  298.  nr = round((ubound(achoices) + 1) / acolumns)
  299.  if amulti then
  300.    t = "checkbox"
  301.  else
  302.    t = "radio"
  303.  end if
  304.  for ri = 0 to nr - 1
  305.    set r = choices.insertrow()
  306.    for ci = 0 to acolumns - 1
  307.      set c = r.insertcell()
  308.      i = ci * acolumns + ri
  309.      if i <= ubound(achoices) then
  310.        c.innerhtml = "<label><table><tr><td><input name=choice type=" & t & _
  311.          " /></td><td class=label></td></tr></table></label>"
  312.        set l = c.queryselector("input")
  313.        l.value = achoices(i)
  314.        k = false
  315.        for each j in adefault
  316.          if j = (i + 1) then
  317.            k = true
  318.            exit for
  319.          end if
  320.        next
  321.        l.checked = k
  322.        c.queryselector(".label").innertext = achoices(i)
  323.      end if
  324.    next
  325.  next
  326.  document.queryselector("#choices input:checked").focus
  327.  margin = content.offsetleft
  328.  resizeto screen.width, screen.height
  329.  border = screen.width - innerwidth
  330.  caption = screen.height - innerheight - border
  331.  wid = content.offsetwidth + (margin * 2) + int(border * 1.2)
  332.  hei = content.offsetheight + (margin * 2) + caption + int(border * 1.5)
  333.  if wid > int(screen.width * 0.7) then wid = int(screen.width * 0.7)
  334.  a = int(screen.height * 0.7)
  335.  if hei > a then
  336.    cwrap.style.height = (cwrap.offsetheight - (hei - a)) & "px"
  337.    hei = a
  338.  end if
  339.  resizeto wid, hei
  340.  moveto int((screen.width - wid) / 2), int((screen.height - hei) / 2)
  341.  if atimeout > 0 then
  342.    if atimeoutok then
  343.      content.ok.innertext = "OK (" & atimeout & ")"
  344.    else
  345.      content.cancel.innertext = "Cancel (" & atimeout & ")"
  346.    end if
  347.    ti = setinterval(getref("timeout"), 1000)
  348.  end if
  349. else
  350.  help
  351. end if
  352.  </script>
  353. </body>
  354.  
Advertisement
RAW Paste Data Copied
Advertisement