简体   繁体   English

在设计脚本中正确转义“&”

[英]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. 我有一个用于indesign的脚本,该脚本用用户指定的文本替换了文本元素中的某些文本。 Sometimes that text contains an & , and the indesign script screws up every time there is. 有时,该文本包含& ,并且每次出现indesign脚本时都会搞砸。 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. 有趣的是,如果我通过indesign界面在文本替换对话框中添加了“&”号,则无需进行转义。 Anyone know how to get around this? 有人知道如何解决这个问题吗? I've tried googling, and nothing relevant is coming up. 我已尝试使用Google谷歌搜索功能,但没有任何相关内容。

//@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"). 尝试转义(“ Q&A”)。 See: https://www.w3schools.com/JSREF/jsref_escape.asp 参见: 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. 修改了indesign文档的字体和页面布局后,该错误不再发生。 Can't say what was causing it, but something we changed fixed whatever was happening. 不能说是什么原因造成的,但是无论发生什么,我们都进行了更改。 Thanks for your suggestions! 感谢您的建议!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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