{"id":67513,"date":"2006-04-18T15:17:00","date_gmt":"2006-04-18T15:17:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/18\/how-can-i-add-an-edit-command-to-the-context-menu-for-an-hta\/"},"modified":"2006-04-18T15:17:00","modified_gmt":"2006-04-18T15:17:00","slug":"how-can-i-add-an-edit-command-to-the-context-menu-for-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-an-edit-command-to-the-context-menu-for-an-hta\/","title":{"rendered":"How Can I Add an Edit Command to the Context Menu for an HTA?"},"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 add an Edit command to the context menu for an HTA? That way, any time I right-click an HTA file I can click Edit and then open the file in Notepad.<BR><BR>&#8212; TC<\/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, TC. You know, one of the Scripting Guys once had a friend who got a summer job baking pies in a local bakery. Admittedly, that sounds like the absolute perfect job and it was, except for one thing: this friend never actually got to <I>eat<\/I> the pies; because the pies were sold whole he didn\u2019t get to cut himself a slice every now and then. As it turned out this was the <I>worst<\/I> possible job: perpetually surrounded by the aroma of freshly-baked pies, yet unable to eat a single bite.<\/P>\n<P>We Scripting Guys can empathize with this poor pie-baking friend. After all, every day we write new scripts, yet we rarely get to use any of these scripts ourselves. We\u2019ve written scripts for managing Active Directory, yet, for some strange reason, they won\u2019t let us manage the Microsoft Active Directory. (Go figure.) We\u2019ve written scripts for managing DNS servers, but we\u2019re not allowed to put up our own DNS servers. We\u2019re written scripts for creating charts in Excel, but they won\u2019t let us create charts in Excel.<\/P>\n<P>Well, OK, they\u2019ll let us do that; we just don\u2019t have anything worth charting.<\/P>\n<P>But this task is different. We use HTAs all the time, and we invariably right-click an HTA and then click <B>Edit<\/B>; in turn, that opens the HTA up in Microsoft Word (or Microsoft FrontPage, depending on what we have installed on that particular machine), which is not our preferred editor for HTAs. Like you, we\u2019d just as soon use Notepad, which means that the Scripting Guys could also benefit from a script that would add an <B>Edit<\/B> command to HTA files. You know, a script similar to this:<\/P><PRE class=\"codeSample\">Const HKEY_CLASSES_ROOT = &amp;H80000000<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objRegistry = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;htafile\\Shell\\Edit\\Command&#8221;\nobjRegistry.CreateKey HKEY_CLASSES_ROOT,strKeyPath<\/p>\n<p>strValue = &#8220;%SystemRoot%\\system32\\NOTEPAD.EXE %1&#8221;\nobjRegistry.SetExpandedStringValue HKEY_CLASSES_ROOT,strKeyPath,vbNullString,strValue\n<\/PRE>\n<P>If you want to add a command to the context menu for an HTA file (and obviously you do) you first need to add a new key to the registry; in particular, you need to go to HKEY_CLASSES_ROOT and add the key <B>htafile\\Shell\\Edit\\Command<\/B>. That\u2019s what we do in the first half of the script. We define a constant named HKEY_CLASSES_ROOT and set the value to &amp;H80000000; that tells the script which registry hive to work with. We then connect to the WMI service on the local computer (although this script can also be run against remote computers), binding to the <B>StdRegProv<\/B> class in the <B>root\\default<\/B> namespace.<\/P>\n<P>Pretty easy so far, right? We then use this line of code to assign the path (within HKEY_CLASSES_ROOT) to the new registry key to a variable named strKeyPath:<\/P><PRE class=\"codeSample\">strKeyPath = &#8220;htafile\\Shell\\Edit\\Command&#8221;\n<\/PRE>\n<P>Once we\u2019ve done that we can then use the <B>CreateKey<\/B> method to create our new key. To do this we simply call CreateKey and pass it two variables: the constant HKEY_CLASSES_ROOT and the variable strKeyPath. Or, to be more precise:<\/P><PRE class=\"codeSample\">objRegistry.CreateKey HKEY_CLASSES_ROOT,strKeyPath\n<\/PRE>\n<P>What\u2019s that? What if you already <I>have<\/I> a registry key named htafile\\Shell\\Edit\\Command? Hey, no problem. If the key already exists nothing bad will happen; the script will simply skip the CreateKey method and move to the next line of code. No error occurs if the registry key already exists.<\/P>\n<P>And, yes, that is kind of nice, isn\u2019t it?<\/P>\n<P>What we have to do now is assign the actual command to be carried out (that is, \u201cOpen this file in Notepad\u201d) to the default value of the Command key. That\u2019s not especially hard but it <I>is<\/I> a tiny bit tricky. So maybe we should explain to you how it works.<\/P>\n<P>As you know, we want HTA files to have a context menu similar to VBScript files: we want to be able to right-click an HTA file, click <B>Edit<\/B>, and then have the HTA open up in Notepad. To be honest, we weren\u2019t 100% sure how to do that, so we simply took a peek at the default value for the VBSFile\\Shell\\Edit\\Command key. That looks like this:<\/P><PRE class=\"codeSample\">%SystemRoot%\\system32\\NOTEPAD.EXE %1\n<\/PRE>\n<P>Will that work for HTA files as well? Hey, you\u2019ll never know until you try, right? With that in mind, we assigned that same string to a variable named strValue:<\/P><PRE class=\"codeSample\">strValue = &#8220;%SystemRoot%\\system32\\NOTEPAD.EXE %1&#8221;\n<\/PRE>\n<P>Now we just need to assign the value of this variable to the default value for htafile\\Shell\\Edit\\Command. Before we do that we need to keep two things in mind. First, you might have noticed that the environment variable <B>%SystemRoot%<\/B> is embedded in our command. That\u2019s good: it means that we can enter the path to Notepad without having to know whether the Windows directory is C:\\Windows or D:\\Winnt or something else altogether. However, this also means that we have to configure the default value with a REG_EXPAND_SZ data type. A registry value with this data type will automatically \u201cexpand\u201d an environment variable with the actual physical path; in other words, it will change %SystemRoot%\\system32\\NOTEPAD.EXE into something like C:\\Windows\\system32\\NOTEPAD.EXE. <\/P>\n<P>Is it going to be hard to give the default value a REG_EXPAND_SZ data type? Come on: if it was going to be hard do you really think the Scripting Guys would try it? All we have to do is make sure that, when we assign the value, we use the <B>SetExpandedStringValue<\/B> method.<\/P>\n<P>Good point. Several of you have wondered why we keep using the generic phrase \u201cdefault value\u201d rather than using the name of the registry value we need to modify. There\u2019s actually a good reason for that: the default value for a registry key doesn\u2019t <I>have<\/I> a name. (It shows up as <B>(Default)<\/B> in RegEdit, but that\u2019s just a placeholder value and is <I>not<\/I> its name.) So how can we access a registry value that doesn\u2019t have a name? Fortunately, that turns out to be fairly easy, too: we simply don\u2019t specify a name when we call the SetExpandedStringValue method. Take a look at the line of code where we call SetExpandedStringValue:<\/P><PRE class=\"codeSample\">objRegistry.SetExpandedStringValue HKEY_CLASSES_ROOT,strKeyPath,vbNullString,strValue\n<\/PRE>\n<P>Notice the four parameters: the constant HKEY_CLASSES_ROOT; the variable strKeyPath; the VBScript constant vbNullString; and the variable strValue. The secret to working with the default value for a registry key lies in using vbNullString (which simply represents a Null value) instead of a value name. When you specify vbNullString in place of the value name then you will automatically be connected to the default value.<\/P>\n<P>Isn\u2019t WMI wonderful?<\/P>\n<P>Actually WMI <I>is<\/I> wonderful, but we don\u2019t really have time to chat today. For some reason, we have a hankering for a big piece of blackberry pie.<\/P>\n<P>Make that <I>two<\/I> big pieces of blackberry pie. With maybe a slice of chocolate cream pie for dessert.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add an Edit command to the context menu for an HTA? That way, any time I right-click an HTA file I can click Edit and then open the file in Notepad.&#8212; TC Hey, TC. You know, one of the Scripting Guys once had a friend who got a summer [&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":[26,3,4,5,30],"class_list":["post-67513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-registry","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I add an Edit command to the context menu for an HTA? That way, any time I right-click an HTA file I can click Edit and then open the file in Notepad.&#8212; TC Hey, TC. You know, one of the Scripting Guys once had a friend who got a summer [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67513","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=67513"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67513\/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=67513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}