Skip to main content
K.Daube
Community Expert
Community Expert
March 2, 2015
Answered

How to test for Cancel in ChooseFile

  • March 2, 2015
  • 1 reply
  • 1203 views

Dear all,

Again I'm stuck with some silly problem.

When I cancel the ChooseFile dialogue, I want to bail out of the function GetTheFile. However, I have not found the appropriate test for this.

The inputFile is reported to be null in the Cancel case:

var prompt = "Select the formatted Citation file";
GetTheFile ();

function GetTheFile () {
  var inputFile = ChooseFile (prompt, GetDocPath (), "*.rtf", Constants.FV_ChooseSelect);
  if (inputFile == 0) {
    alert  ("No input - no processing");
    return;
  }
  alert ("Further processing of the inputfile = " + inputFile);
}

If I cancel, line 10 reports "Further processing of the inputfile = null".

Testing in line 6 for the string "null" does not help either.

The Scripting Guide says for this function:

«The method returns 0 if the user clicked Open, Select, Use, or Save; a nonzero value if the user clicked Cancel or an error occurred»

function GetDocPath () {
// ----------------------------------------------- Get the document/book path
  var docFile, scrPath, lastBSlash;
  if (app.ActiveBook.ObjectValid()) {
    docFile = app.ActiveBook.Name;
  } else {
    docFile = app.ActiveDoc.Name;
  } 
  lastBSlash = docFile.lastIndexOf("\\");        // \ needs escaping
  scrPath = docFile.substring(0, lastBSlash);
  return scrPath;                                 // no final \ !
} // --- end GetDocPath

Testing in line 6 for !== 0 catches the valid input-file names (of course). So what is a valid test for the Cancelled function?

This topic has been closed for replies.
Correct answer Klaus Göbel

Hi Klaus

you have to write the word "null"  :

if (inputFile == null)

then it should work

1 reply

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
March 2, 2015

Looking into the FDK documentation confuses me even more.

Here a very first parameter *choice (aka selected file) is present

IntT F_ApiChooseFile(StringT *choice, StringT title, ... and the function result is the error code.

In the JSX the function result is the selected string or nul or ... - but how to distinguish?

Klaus Göbel
Klaus GöbelCorrect answer
Legend
March 2, 2015

Hi Klaus

you have to write the word "null"  :

if (inputFile == null)

then it should work

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
March 3, 2015

Super -

What is this null? A constant? The editor knows it as a keyword - but what is the nature of this?

Step by step my project evolves - it seems to be at 2/3 of the coding now.

Again, thank You very much, Klaus