Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Xml import script and missing links

Community Beginner ,
Jan 13, 2015 Jan 13, 2015

Hi everyone,

I'm working on a script, that imports xml file and checks for missing graphic files. They are placed on a server in certain locations and it might happen that some of them are gone or missing or not ready yet. If there are any missing links, script displays it as a list. Based on that list, I want to replace all missing links with single placeholder graphic file, which will be placed on a server.

The code so far is as below - checks for links, matches it with existing and displays list of missing files -  not sure what to do next, how to alter the file to replace it with place holder, please help

main();

function main(){

//xml -  import options

var myDocument = app.activeDocument;

var myXMLImportPreferences = myDocument.xmlImportPreferences;

myXMLImportPreferences.allowTransform = false;

myXMLImportPreferences.createLinkToXML = true;

myXMLImportPreferences.ignoreUnmatchedIncoming = false;

myXMLImportPreferences.ignoreWhitespace = false;

myXMLImportPreferences.importCALSTables = false;

myXMLImportPreferences.importStyle = XMLImportStyles.appendImport;

myXMLImportPreferences.importTextIntoTables = false;

myXMLImportPreferences.importToSelected = false;

myXMLImportPreferences.removeUnmatchedExisting = false;

myXMLImportPreferences.repeatTextElements = false;

//open  xml file

var lista = "";

var file = File.openDialog("Open file *.xml","*.xml",false);

    if (file != null) {

        file.open("read", "text");

        testq = file.read();

        file.close();

    } else

    return false;

var grafiki = testq.match(/(S|W).*?\d+\.(pdf|tif|eps)/g);

//no graphic file

   if (grafiki === null)  myDocument.importXML(File(file), false);

    else {

        for (i=0; i<=grafiki.length-1; i++)  {

        plik = File(grafiki).exists;

        if (plik == false)

        lista += grafiki.replace(/S:\/(pdf)\/\d\/\d\//g, "") +"\n";

        }

// List of missing files

    if (lista == "")

    {

    myDocument.importXML(file, false);

    return false

    }

    alert_scroll ("Missing links:", lista);

    function alert_scroll (title, input) {  // string, string/array

            if (input instanceof Array)

                input = input.join ("\r");

                var w = new Window ("dialog", title);

                var list = w.add ("edittext", undefined, input, {multiline: true, scrolling: true, canCancel: true});

                list.maximumSize.height = w.maximumSize.height - 100;

                list.minimumSize.width = 250;

                w.add ("button", undefined, "Continue", {name: "ok"}); //continue

                w.add ("button", undefined, "Cancel", {name:"Cancel"});

              

                w.orientation = "row";

                   

                    if (w.show () == 1){

                    //import xml

                   if (lista != null) {

                     tablica = lista.split("\n");

                            for (i = 0; i >tablica.length-1; i++) {

                           testq.replace(tablica, "/S:/Brak_pliku.pdf");

                             // tablica.xmlElements.item.contents = "/S:/Brak_pliku.pdf";

                            }

                      

                     ??

                   

        myDocument.importXML(file, false);

                            }

                        }

                        else exit();

        }

      }

}

TOPICS
Scripting
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 15, 2015 Jan 15, 2015

Hi,

It looks like you have a bigger issue waiting for you. I did some experimentations and here are some conclusions :

1) You can't import a xml file with missing links through Scripting. I mean it's just not allowed. I thought maybe you could import while preventing alerts and do the image replacements after the import. But you can't even get to this point.

SO you need to replace the urls prior to import, BUT :

2) if you instantiate a XML object, replace urls, and output a new XML, it's likely that you will alter the xml architecture that may output weirdly later within InDesign.

3) I also thought about some XSL file you could create on the fly and call in the XML import process but it looks like XSL 1.0 doesn't allow String replacement (bad luck InDesign XSL support is 1.0).

THEN what solution remains ?

Maybe loading XML file as regular text, replace contents there and save your new xml file to use. You may want to take care of the encoding then and also use a copy not to corrupt the original xml file.

I guess you are expecting code rather than verbose digression but I just wanted to let you know what I observed so you don't loose time on bad leads

FWIW,

Loic

http://www.ozato.com

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 15, 2015 Jan 15, 2015

Thanks for you suggestions, any lead is appreciated

I'm perfectly ok with changing the structure - the main goal is to have xml file imported without having to manually select files.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 22, 2015 Jan 22, 2015

Eventually I came up with two solutions - neither of them is perfect:

one is mentioned by Loic - to replace links, and save it as a temporary xml file - works fine but it changes the structure. And after a second thought I'm not ok with that .

The second option is to create missing file on the fly and save it on a server in the exact location - works also but there is danger that these operation will occur while file is being send to a server and valid file will be overwritten...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 22, 2015 Jan 22, 2015

Hi,

May I suggest that you drop on eye on EasyCatalog plugin ? It can link to remote files an even set a replacement file when one is missing. If you do many structured documents, it can be really woth it and avoid you scripting in this case.

FWIW,

Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 22, 2015 Jan 22, 2015

Ok, I will definitely look into it, thanks for suggestion

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 22, 2015 Jan 22, 2015
LATEST

My pleasure. Feel free to get in touch through my website for further questions regarding to this product.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines