Skip to main content
Participant
March 21, 2018
Question

close the dialog after File.openDialog

  • March 21, 2018
  • 1 reply
  • 264 views

Using the function below with Indeisgn CS6, the window is automatically closed after the xml file is selected

With the CC 2018 version, the window remains open during the script process. Have you an idea so this window will be closed immediately ?

THanks for your help

function ImporterXml(){

    myXmlFile = File.openDialog("Sélectionner le fichier XML", "Fichiers xml :*.xml");

   

    resultat = false;

    if (myXmlFile != null){

        _myXmlName = myXmlFile.displayName;    // que le nom

        _myXmlFileName = myXmlFile.fsName;        // chemin + nom

        doc.xmlElements.item(0).importXML(myXmlFile);           

       

         fichierXML = _myXmlFileName;

        resultat = true;

    }                 

    return resultat;

}

This topic has been closed for replies.

1 reply

Loic.Aigon
Legend
March 27, 2018

Hi,

Looking at your code, it seems you are laying on either global or external scopes for declaring variables values. The former is the worst, the latter is not always necessary.

Here is a possible approach to rationalize the code :

function ImporterXml(doc){

    var M = $.os[0]=="M",

MF = function(f){return (f instanceof Folder)||\.xml/i.test(f.name)},

WF = "Fichiers xml :*.xml",

FT,

myXmlFile = File.openDialog("Sélectionner le fichier XML", FT),

o = {result:false};

if ( !myXmlFile ) {

o.message  = "No file selected" );

return false;

}

o.file = myXmlFile;

try {

   doc.xmlElements.item(0).importXML(myXmlFile);

   o.result = true;

}

catch(err){

o.message = err.line+">>"+err.message;

}

   return o;

}

var doc = app.activeDocument;

var imported = ImporterXml ( doc );

alert( imported.result ? "Everything is fine" : imported.message );

Most changes are :

  1. Adding multi os filter (so code is effective on both Mac and Windows platforms).
  2. Use an object to store properties such as the result (true/false), the message (if sth wrong happened) and the file itself if needed (o.file)
  3. A try/Catch during XML import as xml import could fail (think of a bad xml file)
  4. A "doc" argument so you don't rely on external scope value but a strait reference to the object passed as argument.

And as for window close cycle, It may be a version issue, not your script specifically. Marc Autret did a schema of the inner ScriptUI cycle. I wish I could point you to it but I can't find it anymore.

HTH

Loic

Ozalto | Productivity Oriented - Loïc Aigon