JavaScript + InDesign:在.indd文档中找到一个单词,并写一个带有页码位置的.txt文件 [英] JavaScript + InDesign: Find a word in a .indd document and write a .txt file with page number locations

查看:156
本文介绍了JavaScript + InDesign:在.indd文档中找到一个单词,并写一个带有页码位置的.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm running into a problem while trying to execute a script to find a specific word — in this case the name of a project within a book — and write the page numbers of all its occurrences in my InDesign document to a .txt file.

问题:无论出于何种原因,返回的"parentPage.name"值均不按顺序列出(例如:例如,在第184页上出现的值出现在列表中,例如在第11页之前,等等)

Problem: For whatever reason, the returned "parentPage.name" values are listed out of order (i.e.: its occurrence on p. 184 appears in the list before p. 11, for example, etc.)

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

I'm not sure if it's related to this code (below) or elsewhere:

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

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

If anyone has any ideas for how to resolve this, that'd be great. Thank you!

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

A full version of my code is below:

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;
}

推荐答案

此处是修改后的脚本,用于将结果推送到数组,然后按文档中出现的顺序对其进行排序.

Here's the script modified to push results to an array, and then sort them by the order of appearance in the document.

很明显,答案来得太晚了……但希望将来会有用.

Clearly the answer is way late... But hopefully it's of use in the future.

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;
    }

这篇关于JavaScript + InDesign:在.indd文档中找到一个单词,并写一个带有页码位置的.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆