JavaScript + InDesign:在.indd文档中找到一个单词,并写一个带有页码位置的.txt文件

时间:2019-01-14 18:26:39

标签: javascript find adobe adobe-indesign extendscript

在尝试执行脚本来查找特定单词(在本例中为书中项目的名称)并将InDesign文档中所有出现的页码写入时,我遇到了问题。 txt文件。

问题:无论出于何种原因,返回的“ parentPage.name”值都是乱序列出的(例如:例如,在第184页上出现的值出现在列表中,例如在第11页之前,等等)

我不确定它是否与此代码(在下面)或其他地方相关:

pg_nr = found_txt.parentTextFrames[0].parentPage.name;

如果有人对如何解决此问题有任何想法,那就太好了。谢谢!

我的代码的完整版本如下:

main ();
function main() {

var file_name = new Date() + "-Title-Project_Name-Page_Location";
var filepath = "~/Desktop/" + file_name + ".txt";
var write_file = File(filepath);
    write_file = new File(filepath);
    var write_text;
        write_text = write_file.open('w', undefined, undefined);
        write_file.encoding = "UTF-8";
        write_file.lineFeed = "Macintosh";
        #target indesign;
        var doc = app.activeDocument;
            app.findTextPreferences = NothingEnum.nothing;
            app.changeTextPreferences = NothingEnum.nothing;
            app.findChangeTextOptions.includeLockedLayersForFind = true;
            app.findChangeTextOptions.includeLockedStoriesForFind = true;
            app.findChangeTextOptions.includeHiddenLayers = false;
            app.findChangeTextOptions.includeMasterPages = false;
            app.findChangeTextOptions.includeFootnotes = true;
            app.findChangeTextOptions.caseSensitive = false;
            app.findChangeTextOptions.wholeWord = false;
            project_name = "Project Name";
            app.findTextPreferences.findWhat = project_name;
                find_txt = doc.findText();
                for (var i = 0; i < find_txt.length; i++) {
                    if (find_txt[i].contents == project_name) {
                        found_txt = find_txt[i];
                        pg_nr = found_txt.parentTextFrames[0].parentPage.name;
                        write_file.writeln(project_name + " : p. " + pg_nr + "\r\r");
                        }
                    }
            app.findTextPreferences = NothingEnum.nothing;
            app.changeTextPreferences = NothingEnum.nothing;
    write_file.close();
return;
}

1 个答案:

答案 0 :(得分:0)

这里修改了脚本,以将结果推送到数组,然后按文档中出现的顺序对它们进行排序。

显然,答案是这样晚了......但我们希望它在未来使用的。

main ();
function main() {
    var arrResults = [];
    #target indesign;
    var doc = app.activeDocument;
    app.findTextPreferences = NothingEnum.nothing;
    app.changeTextPreferences = NothingEnum.nothing;
    app.findChangeTextOptions.includeLockedLayersForFind = true;
    app.findChangeTextOptions.includeLockedStoriesForFind = true;
    app.findChangeTextOptions.includeHiddenLayers = false;
    app.findChangeTextOptions.includeMasterPages = false;
    app.findChangeTextOptions.includeFootnotes = true;
    app.findChangeTextOptions.caseSensitive = false;
    app.findChangeTextOptions.wholeWord = false;
    project_name = "Project Name";
    app.findTextPreferences.findWhat = project_name;
        find_txt = doc.findText();
        for (var i = 0; i < find_txt.length; i++) {
            if (find_txt[i].contents == project_name) {
                found_txt = find_txt[i];
                pg_nr = found_txt.parentTextFrames[0].parentPage.name;
                pg_index = found_txt.parentTextFrames[0].parentPage.documentOffset;
                // pushes results into an array of objects
                arrResults[arrResults.length] = {"project_name":project_name,"page_number":pg_nr,"page_index":pg_index};
                }
            }
    app.findTextPreferences = NothingEnum.nothing;
    app.changeTextPreferences = NothingEnum.nothing;
    // sorts array by the document offset (order of appearance in document)
    arrResults = arrResults.sort(function(a,b){return a.page_index>b.page_index});
    // convert object to string
    for(var i = 0; i<arrResults.length; i++){
        arrResults[i] = "project:"+arrResults[i].project_name+", pg:"+arrResults[i].page_number;
        }
    var file_name = new Date() + "-Title-Project_Name-Page_Location";
    var filepath = "~/Desktop/" + file_name + ".txt";
    var write_file = new File(filepath);
    var write_text;
    write_text = write_file.open('w');
    write_file.encoding = "UTF-8";
    write_file.lineFeed = "Macintosh";
    write_file.write(arrResults.join("\n"));
    return;
    }