简体   繁体   中英

Proper escape for “&” in an indesign script

I have a script for indesign that replaces some of the text in text elements with user specified text. Sometimes that text contains an & , and the indesign script screws up every time there is. I've tried all the common escape character "/,\\,^,..." etc, but they don't work. The funny thing is, if I put an ampersand in the text replace dialogue through the indesign interface, I don't need to escape it. Anyone know how to get around this? I've tried googling, and nothing relevant is coming up.

//@target indesign
    //open template
    var template = app.open("/Users/pad/Desktop/PAD Master Templates/B-MASTER.indd");
    main();
    function main(){
        //hide all layers
        var doc = app.activeDocument;
        for (var i = 0; i < doc.layers.length; i++) {
          var item = doc.layers[i];
          item.visible = false;
        }

    //show appropriate design layer
    var padFont = doc.layers.item("Font");
    padFont.visible = true;
    var padArt = doc.layers.item("Artwork");
    padArt.visible = true;
    var links = doc.links;
    var link;
    var update = false;
    for (var i = links.length-1; i >= 0; i--)  {
        link = links[i];
        if (link.name == 'some_design.eps') {
            newLink = new File(link.filePath.replace('some_design.eps', "new_design.eps"));
            update = true;
        }           
        if (update == true && newLink.exists) {
            link.relink(newLink);
            try {
                link.update();
            }
            catch(err) {}                   
        }       
    }
        //Find and replace text.
        app.findTextPreferences = NothingEnum.NOTHING;
        app.changeTextPreferences = NothingEnum.NOTHING;
        app.findChangeTextOptions.caseSensitive = true;
        app.findChangeTextOptions.includeFootnotes = false;
        app.findChangeTextOptions.includeHiddenLayers = false;
        app.findChangeTextOptions.includeLockedLayersForFind = false;
        app.findChangeTextOptions.includeLockedStoriesForFind = false;
        app.findChangeTextOptions.includeMasterPages = false;
        app.findChangeTextOptions.wholeWord = false;
        app.findTextPreferences.findWhat = "line 1";


//this is a line where I need to escape the "&"

        app.changeTextPreferences.changeTo = "R&T";
        doc.changeText(); 
        app.findTextPreferences.findWhat = "line 2";


//this is a line where I need to escape the "&"


        app.changeTextPreferences.changeTo = "Q&A";
        doc.changeText();   
        //Exprt as jpeg
        doc.exportFile(ExportFormat.JPG, "/path/name.jpg");
        //Close the file without saving.
        doc.close (SaveOptions.NO);  
    }

Try escape("Q&A"). See: https://www.w3schools.com/JSREF/jsref_escape.asp

 document.write(escape("Q&A")); 

After fiddling with the font and page layout of the indesign document, the error is no longer occurring. Can't say what was causing it, but something we changed fixed whatever was happening. Thanks for your suggestions!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM