Hey, Scripting Guy! Is there any way I can install a font using VBScript?
— RS
Hey, RS. As a general rule, the Scripting Guy who writes this column steers clear of popular culture. (Actually, it’s not just a general rule; popular culture has a restraining order which requires him to stay at least 100 yards away from it at all times.) Nevertheless, every now and then he accidentally stumbles upon a bit of popular culture that absolutely fascinates him.
For example, just this morning he read about a movement to remove the apostrophe from the English language. As near as he can tell, there are two primary concerns that people have with the apostrophe. For one, they believe the apostrophe is way too hard to use; therefore, it should be eliminated. (This, by the way, is how you can tell that Americans are behind the movement to eliminate the apostrophe: if something is hard, then don’t do it. That’s the American way!)
The other argument is this: no one uses apostrophes in text message. (You say they do use apostrophes? we say they dont.) According to this theory, text messaging is becoming more and more popular; therefore, we should adopt the same grammar, syntax, and spelling used in text messaging.
Because, as most people would agree, text messaging should be the ultimate arbiter when it comes to the English language.
Note. Apparently text messaging is not just influencing the use of apostrophes; it’s also influencing the names that parents bestow on their children. As a public service for those of you expecting a new bundle of joy, you can find a handy list of text messaging acronyms and shorthand codes right here. Please give little An our best. (An, by the way, is short for Ann, apparently because a three-letter name like Ann needs to be trimmed down a bit.) |
Here’s a true story for you. The Scripting Guy who writes this column once worked with an editor who was also opposed to the apostrophe, but for a different reason: she didn’t – sorry, she did not – believe that any word containing an apostrophe was a real word. Can’t? Not a real word; it should be “cannot.” Doesn’t? Not a real word; it should be “does not.” Debra’s computer? Not a real phrase; it should be “the computer belonging to Debra.” Ah, good times ….
Note. What about the phrase “the hard disk belonging to the computer?” Turns out that’s not a real phrase, either. Why not? Because “computers are inanimate objects and cannot possess anything.” Which seems odd to us. After all, Scripting Guy Dean Tsaltas is pretty much an inanimate object, and he possesses all sorts of things. |
So how does any of that help you install a font using VBScript? Well, to be honest, we’re not sure that it does. But this should help a little:
Const FONTS = &H14& Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(FONTS) objFolder.CopyHere "C:\Scripts\Myfont.ttf"
As it turns out, there’s nothing particularly difficult about installing a new font. Suppose you have a font file named C:\Scripts\Myfont.ttf. How can you install that font? Here’s one simple way: just drag the font into your Fonts folder. As soon as the font has been added to the folder, the operating system will immediately install the font for you.
Which, now that we think about it, is pretty cool, isn’t it?
Of course, that might also get you to thinking; in fact, you might be thinking, “Hey, all I have to do is copy a font into the Fonts folder and that font will automatically be installed for me.” And that’s true, but with one very important caveat: you must copy the file using the Shell object. Admittedly, you can use WMI or the FileSystemObject to copy a file into the Fonts folder; however, when you do so the operating system will not automatically install the font for you. As far as we know, the only programmatic way to get Windows to recognize that a new font has been added to the Fonts folder, and thus get Windows to install that font for you, is to use the Shell object.
Note. And yes, that does make it a bit of a sticky wicket to try and install fonts on a remote computer; after all, the Shell object isn’t designed to work against remote computers. One possible solution? Put your font-installing code in a logon script; after all, logon scripts always run locally. A less-elegant solution would be to copy the fonts to a remote machine, then instruct the user at that remote computer to open and close their Fonts folder. Opening and closing the Fonts folder has the net effect of installing any new fonts that are in the folder but not yet installed. Like we said, that’s not very elegant. But it’ll work. |
As for the script itself, we start out by defining a constant named FONTS and setting the value to &H14&; we’ll use this constant to tell the script which “special folder” (i.e., the Fonts folder) we want to work with.
Note. Couldn’t we just use a hardcoded path like C:\Windows\Fonts to represent the Fonts folder? Sure. However, keep in mind that the Fonts folder doesn’t have to be C:\Windows\Fonts. By using the special folders constant the script will always find the Fonts folder for us, regardless of where that folder actually lives. |
After we define the constant we create an instance of the Shell.Application object, then use the following line of code to bind to the Fonts folder:
Set objFolder = objShell.Namespace(FONTS)
Once we’re connected to the Fonts folder all we have to do is call the CopyHere method, specifying the complete path to the font we want to install:
objFolder.CopyHere "C:\Scripts\Myfont.ttf"
It’s that easy. And yes, now that you mention it, this script does work on Windows Vista; just make sure you run the script as an Administrator. Otherwise you’ll get a security popup asking you if you really do want to copy a file to the Fonts folder.
But the heck with fonts; let’s get back to something people really care about: apostrophes. As a Microsoft employee, the Scripting Guy who writes this column isn’t supposed to take a stand on controversial issues, at least not when publishing articles on Microsoft.com. Nevertheless, he’s going to go out on a limb here and state – for the record – that he believes the apostrophe should continue to be used; he’d just like to see it used a bit more correctly. Being out of touch with the way things are done these days, the Scripting Guy who writes this column still hasn’t come to grips with words spelled any which way the writer pleases, nor with apostrophes put in the wrong places.
In fact, in researching this article (yes it is kind of hard to believe that a article like this is researched, isn’t it) the Scripting Guy who writes this column discovered there’s a technical term for the wanton use of apostrophes: greengrocer’s apostrophes, a name bestowed in honor of grocery stores that have signs like “Banana’s: 5 for a Dollar” and “Hamburger and Hot Dog Bun’s Half-Price.” If you have the stomach for it (and an old-fashioned curmudgeon like the Scripting Guy who writes this column really doesn’t) you might check out the Apostrophe Abuse Web site. Some of the examples there will curl your hair.
And, if you’re lucky, will give you a name for your new baby as well.
Historical footnote. Former US President Andrew Jackson once stated that he had “little respect for a man who can only think of one way to spell a word.” Historians are unsure of Jackson’s view on apostrophes; we’ll continue to research this and keep you posted. |
0 comments