Advertisement
jcunews

DevicePlugNotification.vbs

Sep 3rd, 2022
1,934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'DevicePlugNotification v1.0.1, Sep 2022.
  2. 'https://www.reddit.com/user/jcunews1
  3. 'https://pastebin.com/u/jcunews
  4. 'https://greasyfork.org/en/users/85671-jcunews
  5. '
  6. 'Script to display notifications when a device is plugged in.
  7. 'Each device may contain one or more subdevices.
  8. 'e.g. for USB thumb drive: the device itself (mass storage), a disk drive,
  9. 'then one or more disk volumes (partitions)
  10. 'Each notification will display 2 lines: device name and type.
  11. '
  12. 'Run the script to start monitoring.
  13. 'Run the script while it's already monitoring, to stop monitoring.
  14. 'Run the script with any arguments to omit displaying startup/shutdown dialogs.
  15.  
  16. '--------SETTINGS BEGIN
  17.  
  18. 'y position in pixels from screen bottom to start placing notifications.
  19. ypos     = 80
  20.  
  21. 'display time in milliseconds (1 sec = 1000ms) for each notification.
  22. 'notifications can be clicked to close it early.
  23. duration = 10000
  24.  
  25. 'notification font size in pixels
  26. fontsize = 12
  27.  
  28. '--------SETTINGS END
  29.  
  30. title = "DevicePlugNotification"
  31. set fs = createobject("scripting.filesystemobject")
  32. tp = fs.getspecialfolder(2)
  33. fna = tp & "\DevicePlugNotification.active"
  34. fns = tp & "\DevicePlugNotification.stop"
  35. py = 0
  36. on error resume next
  37. set fa = fs.createtextfile(fna)
  38. if err.number = 0 then 'already active
  39.  if wscript.arguments.count = 0 then
  40.     msgbox title& " will be active after this dialog is closed." _
  41.       &vblf& "Run this script again to stop monitoring.", 64, title
  42.   end if
  43. else
  44.   if msgbox(title& " is already active." &vblf& _
  45.     "Do you want to stop monitoring?", 33, title) = 1 then
  46.     fs.createtextfile(fns).close
  47.   end if
  48.   wscript.quit
  49. end if
  50. fs.deletefile fns
  51. on error goto 0
  52. set ps = createobject("scripting.dictionary")
  53. getobject("winmgmts:root\cimv2").execnotificationqueryasync _
  54.   wscript.createobject("wbemscripting.swbemsink", "ev_"), _
  55.   "select * from __instancecreationevent within 1" & _
  56.   " where targetinstance isa 'win32_pnpentity'"
  57. do
  58.   wscript.sleep 300
  59.   if fs.fileexists(fns) then exit do
  60. loop
  61. fa.close
  62. fs.deletefile fna
  63. on error resume next
  64. fs.deletefile fns
  65. if wscript.arguments.count = 0 then
  66.   msgbox title& " has been stopped.", 32, title
  67. end if
  68.  
  69. sub phide(y)
  70.   ps(y).parentwindow.p.hide
  71.   ps.remove y
  72.   if y = (py - 1) then py = 0
  73. end sub
  74.  
  75. sub pop(s)
  76.   set d = createobject("htmlfile")
  77.   d.write "<script>fontsize=" &fontsize& ";phide=null;" & _
  78.     "function pop(s,y){" & _
  79.       "p=createPopup();d=p.document;" & _
  80.       "d.write('" & _
  81.         "<body onclick=""parent.phide(" &py& ")""><div></div><style>" & _
  82.         "body{margin:0;border:1px solid #007;overflow:hidden;padding:.3em;" & _
  83.         "background:#ddf;font:" &fontsize& "px/normal sans-serif}</style>" & _
  84.         "</body>');" & _
  85.       "h=(a=String(s).split('\n')).length;w=0;" &_
  86.       "for(i=0;i<a.length;i++){l=a[i].length;if(l>w)w=l}" &_
  87.       "w=w*(fontsize*0.55);h=h*(fontsize*1.65);" & _
  88.       "d.close();" & _
  89.       "d.body.firstChild.innerText=s;" & _
  90.       "p.show(screen.width-w-10," & _
  91.         "screen.height-h-" &ypos& "-" &py& "*(h+10),w,h);" & _
  92.       "setTimeout(function(){parent.phide(" &py& ")}," &duration& ")" & _
  93.     "}</script>"
  94.   d.close
  95.   set d.parentwindow.phide = getref("phide")
  96.   d.parentwindow.pop s, py
  97.   ps.add py, d
  98.   py = py + 1
  99. end sub
  100.  
  101. sub ev_onobjectready(o, c)
  102.   set o = o.targetinstance
  103.   pop o.caption &vbcrlf& o.description
  104. end sub
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement