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

    Wscript.Echo objDialog.FileName

Else

    Wscript.Quit

End If

 

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:

 

  • FileName. You know how, in Word, if you click File and then Save Word’s File Save dialog box will open up in a specific folder and give you a default file name (typically Document1.doc)? You can accept that folder and file name, you can choose a new folder, you can type in a new file name, whatever. With our File Save dialog box, the FileName property lets you specify both the default folder and the default file name. In the example shown above, File Save will open up in the C:\Scripts folder and, by default, will set Script1.vbs as the default file name. Again, though, you casn change the folder, change the file name, do whatever. After all, this is a real, live dialog box: you can even delete files, rename files, view files as a list or as icons, a lot of the same things you can do in any File Save dialog box. The value you use for FileName is just a default placeholder value.
  • FileType. To tell you the truth, there isn’t a whole heck of a lot of value to this; it’s just some descriptive text that you can add to the Save as type dropdown list. It doesn’t filter the files in any way, and I couldn’t find a way to get it to display more than one file type; that is, I couldn’t find a way to have more than one item listed in the dropdown. But, then again, because it seems to be just decoration I guess that doesn’t really matter all that much.

 

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:

 

  • Calls the File Save dialog box.
  • Returns the path name.
  • Creates an instance of the FileSystem Object.
  • Saves the current date to the specified path.

 

The whole thing looks like this:

 

Set objDialog = CreateObject("SAFRCFileDlg.FileSave")

objDialog.FileName = "C:\Scripts\Script1.vbs"

objDialog.FileType = "VBScript Script"

intReturn = objDialog.OpenFileSaveDlg

 

If intReturn Then

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.CreateTextFile(objDialog.FileName)

    objFile.WriteLine Date

    objFile.Close

Else

    Wscript.Quit

End If

 

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.