{"id":67083,"date":"2006-06-19T09:16:00","date_gmt":"2006-06-19T09:16:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/19\/how-can-i-write-a-script-that-prevents-the-screensaver-from-starting\/"},"modified":"2006-06-19T09:16:00","modified_gmt":"2006-06-19T09:16:00","slug":"how-can-i-write-a-script-that-prevents-the-screensaver-from-starting","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-write-a-script-that-prevents-the-screensaver-from-starting\/","title":{"rendered":"How Can I Write a Script That Prevents the Screensaver from Starting?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I write a script that prevents the screensaver from coming on? Sometimes I get interrupted for a couple minutes (say I get a phone call or something) and the screensaver kicks in, which promptly locks the computer. Is there a way to keep that from happening?<BR><BR>&#8212; SS<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, SS. We hope you\u2019ll forgive us if we seem a little distracted today. As you probably heard, Bill Gates has decided to step down as Microsoft\u2019s Chief Software Architect. Needless to say, someone will have to replace him and, well &#8211; all modesty aside &#8211; who would be a more logical choice than the Scripting Guys? We\u2019re expecting the phone to ring any minute now.<\/P>\n<P>Ray Ozzie? No, never heard of him. Why do you ask?<\/P>\n<P>Of course, seeing as how this is Microsoft (although we <I>are<\/I> thinking about changing the company name after we take over) there\u2019s a good chance that they\u2019ll send us an email instead of calling us. That\u2019s why we decided to see if we could answer your question; after all, we don\u2019t want <I>our<\/I> screensaver kicking in the moment that email arrives.<\/P>\n<P>Not that it\u2019s arrived yet, mind you. But maybe the server is having problems.<\/P>\n<P>To begin with, we should point out that we had no idea whether we <I>could<\/I> find a way to keep the computer active and thus prevent the screensaver from running. But we did. And if that doesn\u2019t sound like the perfect qualification for Chief Software Architect, well, we don\u2019t know what does.<\/P>\n<P>Here\u2019s the script we came up with:<\/P><PRE class=\"codeSample\">If LCase(Right(Wscript.FullName, 11)) = &#8220;wscript.exe&#8221; Then\n    strPath = Wscript.ScriptFullName\n    strCommand = &#8220;%comspec% \/k cscript  &#8221; &amp; Chr(34) &amp; strPath &amp; chr(34)\n    Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\n    objShell.Run(strCommand)\n    Wscript.Quit\nEnd If<\/p>\n<p>Do While True\n    Set objIE = CreateObject(&#8220;InternetExplorer.Application&#8221;)\n    Wscript.Sleep 1000\n    objIE.Quit\n    Wscript.Sleep 60000\nLoop\n<\/PRE>\n<P>We should note that there might be other ways to perform this task. We tried a couple things that <I>didn\u2019t<\/I> work, then stumbled upon an approach that <I>did<\/I> work. As soon as we found something that worked, we called it good. After all, while we don\u2019t have any idea what a Chief Software Architect actually does, we assume that someone in that position probably shouldn\u2019t spend hours and hours looking for alternate ways to keep a screensaver from locking. That\u2019s why we went with the first solution we came up with. If you have a better\/easier way to do this, let us know.<\/P>\n<P>So what exactly <I>did<\/I> we come up with? Let\u2019s start by looking at this block of code:<\/P><PRE class=\"codeSample\">If LCase(Right(Wscript.FullName, 11)) = &#8220;wscript.exe&#8221; Then\n    strPath = Wscript.ScriptFullName\n    strCommand = &#8220;%comspec% \/k cscript  &#8221; &amp; Chr(34) &amp; strPath &amp; chr(34)\n    Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\n    objShell.Run(strCommand)\n    Wscript.Quit\nEnd If\n<\/PRE>\n<P>This is actually optional, but we needed a script that could run for an indefinite period of time; after all, there\u2019s no telling how long that telephone conversation or trip to the coffee machine will take. Because we don\u2019t know how long the script needs to run we can\u2019t put in any code that auto-terminates the thing after say, five minutes. In turn, that means we need a quick and easy way to manually terminate the script.<\/P>\n<P>And probably the quickest and easiest way to ensure that is to force the script to run in a command window under CScript.exe. After all, in that case we just need to close the command window and the script will end. <\/P>\n<P>We\u2019d like to see Ray Ozzie come up with an idea like <I>that<\/I>.<\/P>\n<P>That\u2019s why we start the script with the aforementioned block of code. That code is designed to see if the script is running under Wscript.exe (and thus not running in a command window). That\u2019s something we check for that with this line of code:<\/P><PRE class=\"codeSample\">If LCase(Right(Wscript.FullName, 11)) = &#8220;wscript.exe&#8221; Then\n<\/PRE>\n<P>In case you\u2019re wondering, the <B>FullName<\/B> property refers to the script host that the script is running under. The value of FullName will always be something like C:\\Windows\\System32\\Cscript.exe or C:\\Windows\\System32\\Wscript.exe. If the last 11 characters in FullName are <I>wscript.exe<\/I> then we\u2019re running under Wscript. And we need to do something about that.<\/P>\n<P>So can we change the script host after the script starts running? No, not as far as we know. However, we can do the next best thing: we can restart the script under CScript. That\u2019s what the rest of the code block is for. In order to restart the script, we first grab the value of the <B>ScriptFullName<\/B> property and store it in a variable named strPath; if you aren\u2019t familiar with ScriptFullName, this property holds the path to the script itself (e.g., C:\\Scripts\\Test.vbs). That brings us to this line of code:<\/P><PRE class=\"codeSample\">strCommand = &#8220;%comspec% \/k cscript  &#8221; &amp; Chr(34) &amp; strPath &amp; chr(34)\n<\/PRE>\n<P>What we\u2019re doing here is constructing a command-line command that will run the current script under CScript.exe. To do that we start by calling the <B>%comspec% \/k<\/B> environment variable; that will cause a command window to open and remain open (the \/k is short for \u201ckeep open\u201d). We then specify the script host &#8211; <B>cscript<\/B> &#8211; followed by a blank space. After that we insert a pair of double quotes &#8211; <B>Chr(34)<\/B> &#8211; followed by the path to the script, followed by a closing pair of double quotes. When we\u2019re done, the variable strCommand will be equal to something along these lines:<\/P><PRE class=\"codeSample\">%comspec% \/k &#8220;C:\\Scripts\\Test.vbs&#8221;\n<\/PRE>\n<P>Good question: why <I>do<\/I> we enclose the path in double quotes? Well, in this case, we don\u2019t need to; we only have to do that if the path to the script file contains a blank space (e.g., C:\\My Scripts\\Test.vbs). We added the double quotes just to be on the safe side.<\/P>\n<P>After we\u2019ve put together our command-line command we then use this line of code to start a second instance of the script:<\/P><PRE class=\"codeSample\">objShell.Run(strCommand)\n<\/PRE>\n<P>And then we call the <B>Quit<\/B> method to terminate the first instance of the script. That leaves us with one copy of the script, a copy running under CScript.exe.<\/P>\n<TABLE id=\"EGF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. What if the script already <I>is<\/I> running under CScript.exe? In that case we simply skip the initial code block altogether.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That\u2019s all fine and dandy, but what exactly is that script doing? Why, it\u2019s doing this:<\/P><PRE class=\"codeSample\">Do While True\n    Set objIE = CreateObject(&#8220;InternetExplorer.Application&#8221;)\n    Wscript.Sleep 1000\n    objIE.Quit\n    Wscript.Sleep 60000\nLoop\n<\/PRE>\n<P>What we\u2019ve done here is set up an endless loop, ensuring that the script will run forever and ever. (Or at least until you close the command window.) Inside that loop we create an instance of the <B>InternetExplorer.Application<\/B> object, an instance we never actually show on screen. After that we call the <B>Sleep<\/B> method, pausing the script for 1 second (1000 milliseconds). We then immediately terminate our instance of Internet Explorer, pause the script for 60 seconds (60000 milliseconds) and then repeat the process all over again, ad infinitum.<\/P>\n<TABLE id=\"E5F\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. There\u2019s nothing magical abut the 60-second pause; we used that because we had our screensaver timeout set for 2 minutes and we needed to make sure we had some activity before those 2 minutes expired. If you have your screensaver timeout set to a higher value then you can pause a correspondingly-longer time before creating the next instance of Internet Explorer.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Yes, it\u2019s a crazy idea, but it works: the screensaver stays off and, as an added bonus, nothing else pops up on the screen, either. If you add a shortcut to your Quick Launch taskbar you can invoke this script with a single mouse click. And then, of course, to end the script all you have to do is close the command window. Pretty sweet.<\/P>\n<P>So there you have it, SS. Now, we were wondering if you could do <I>us<\/I> a favor? Could you give us a call here at the office? We suspect that our phones must not be working. After all, that\u2019s the only reasonable explanation we could come up with as to why no one has called us yet.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I write a script that prevents the screensaver from coming on? Sometimes I get interrupted for a couple minutes (say I get a phone call or something) and the screensaver kicks in, which promptly locks the computer. Is there a way to keep that from happening?&#8212; SS Hey, SS. We [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,47,3,5],"class_list":["post-67083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I write a script that prevents the screensaver from coming on? Sometimes I get interrupted for a couple minutes (say I get a phone call or something) and the screensaver kicks in, which promptly locks the computer. Is there a way to keep that from happening?&#8212; SS Hey, SS. We [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67083"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67083\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}