Prompting Save As Dialog with JavaScript

Prompting Save As Dialog with JavaScript

1
NewbieNewbie
1

    May 01, 2008#1

    Does anyone know if there's an easy way to have a JavaScript save a document that has not previously been saved? For example, if I type something in a new window with the default label "Edit1", is there an easy way to have JavaScript prompt the "Save As" dialog box so the user can select a file name?

    Using UltraEdit.save() returns an error if the file has not previously been saved. UltraEdit.saveAs(), without an argument, does not seem to do anything. Running the followng:

    Code: Select all

    var text = UltraEdit.activeDocument.path;
    UltraEdit.saveAs(text);
    
    saves the file with the default name (e.g., "Edit1") automatically.

    The one approach I have been able to make work is to create a tool that does basically nothing (runs a "dir" command without saving the output), but that has the "Save Active File" box checked on the Options tab. I can run this tool from JavaScript and everything works fine (the "Save As" dialog is prompted if the document has not previously been saved, and is not prompted if the file has previously been saved). But this seems like an inelegant solution, particularly if other users want to use my program (they would need to have the same tool created on their machines).

    Does anyone else know an easier way to do this?

    Thanks in advance, and apologies if the solution is obvious (I looked for an answer, honest).

    - Ken

    6,683583
    Grand MasterGrand Master
    6,683583

      May 01, 2008#2

      You could use following (without testing):

      var NameOfFile = UltraEdit.getString("Please enter the file name:",1);
      UltraEdit.saveAs(NameOfFile);


      Then a user could enter the file without path - file will be saved to current working directory - or with a path. A good script writer would enclose the first line into a loop with additional checks of the file name the user has entered. It must have at least 1 character. Reserved characters like ?, *, <, >, ", |, /, etc. are not allowed in file names. A file name can only have one : as second char if any : exists. The file name should not be longer than 259 characters. And so on.

      But the best solution would be that UltraEdit would allow to use UltraEdit.saveAs("");
      In this case where an empty string is used for the file name UltraEdit should show the Save As dialog. In the macro environment the command SaveAs "" opens the Save As dialog. Write a feature request email to IDM support. I was astonished that in the script environment an empty file name string creates an error message instead of opening the Save As dialog.

      Edited on 2010-01-13:
      Starting with UE v15.00 the command UltraEdit.saveAs(""); can be used in scripts for saving new files with displaying the Save As dialog. But this works only for really new files - files without file name. Using UltraEdit.saveAs(""); on an existing file to save it with a different, user specified name still results in an error message instead of displaying the Save As dialog.
      Best regards from an UC/UE/UES for Windows user from Austria