Yesterday’s entry, combined with today’s, reminds me of the old Batman TV show. If you happen to be, as they say, of a certain age, then you no doubt remember the old Batman show, which pretty much followed the exact same theme in each and every episode: Batman and Robin would get captured by the Joker or the Riddler or whoever and, at the end of the episode, would find themselves in a cannery or some suitably silly place, and the narrator would always say something like, “Looks like our dynamic duo is really 'in a pickle' this time. Will they possibly be able to avoid a ‘jarring’ conclusion? To find out, tune in again next week, same Bat time, same Bat channel!”
Of course, you knew that they would manage to escape at the beginning of next week’s episode; they always manage to esacpe at the beginning of next week’s episode. And you also knew that after escaping they would put the Riddler in jail, only to fall into the clutches of the Penguin or Catwoman at the end of the episode and start the cycle all over again. Ah, yes, the golden age of television ….
Anyway, yesterday I told you how to use a File Open dialog box from within a script (well, provided you are running Windows XP, that is), and then left you hanging on the edge of your seat by saying, “And tomorrow, we’ll tell you how to use a File Save dialog box from within a script.” I can hear the announcer now: Looks like Greg is really 'in a pickle' this time. Will he possibly be able to – well, you get the idea. Hopefully you didn’t lose too much sleep last night worrying about this.
Why? Because, just like with the caped crusaders, the suspense is only going to last a few minutes. Yes, it is possible to call a File Save dialog box from a script (although, again, you need to be running Windows XP), and yes, I’m going to show you how to do that. And then I’ll just have to pray that I don’t fall into the clutches of the Penguin or the Riddler at the end of this entry. (Although if it was Catwoman instead ….)
Let me show you the code, and then we’ll talk about what it all means:
Set objDialog = CreateObject("SAFRCFileDlg.FileSave")
objDialog.FileName = "C:\Scripts\Script1.vbs"
objDialog.FileType = "VBScript Script"
intReturn = objDialog.OpenFileSaveDlg
If intReturn Then
As you can see, we start out by creating an instance of the SAFRCFileDlg.FileSave object (try typing that three times quickly). We then set two optional property values:
Next we display the Save As dialog box by calling the OpenFileSaveDlg method. We then wait for the user to do something. If the user enters a file name (or accepts the default file name) and clicks OK, OpenFileSaveDlg returns True, and we echo the name of the file. If the user clicks Cancel, OpenFileSaveDlg returns False, and, in this case, we simply exit the script (using the Wscript.Quit command).
Now, there’s one big caveat here, and that’s this: choosing a file name and clicking OK doesn’t actually save anything; after all, what exactly would it save? All this does is return a path name. If you want to actually save something under that file name, you’ll have to pass the path name to, say, the FileSystem Object, and then include code for saving the information. For example, suppose you had a burning desire to save the current date to a file. Here’s code that:
The whole thing looks like this:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(objDialog.FileName)
How useful is this to most script writers? I can’t really answer that. I know this would have been very cool had we included it in the Scriptomatic; we didn’t, because it runs only on Windows XP, and a lot of our readers are still using Windows 2000. I suppose we could have created an XP-version and a non-XP version of the Scriptomatic, but we have enough trouble getting our utilities out the door as it is. Of course, the Scritpomatics are just HTAs, and you can open them up with Notepad and modify them to your heart’s content. If you wanted this functionality, it would be pretty easy to add it.
So are there other dialog boxes that can be called from a script? I think you already know the answer to that question: tune in again next week, same Bat time, same Bat channel.
P.S. By the way, it was interesting to see the people who posted what they thought were the best episodes of the Simpsons. But, what, no Junior Campers? No Monorail? No episode where Homer starts his own religion? I can see where we'll have to revisit this topic in the future.