多个文档上的Indesign搜索文件名

发布于 2025-01-19 07:19:52 字数 4865 浏览 4 评论 0原文

我正在尝试在JavaScript中制作脚本,但是我在这个领域的经验不太经验,所以我在这里寻求帮助。

基本上,我在工作的地方,我每天都有很多带有不同名称和数字的不同图像的.indd文件。 当有人想知道哪个文档是某个图像时,我需要手动搜索每个图像。

我试图整理一个工作脚本,但没有任何运气。 我正在寻找的是一个脚本,让您选择一个文件夹,让您键入文件名的名称(或名称的一部分)要搜索,搜索文件夹中的所有文档而不一次打开所有文件(出于绩效原因,由于文件很多),如果找到了带有该名称的文件,请给您警报。

我已经在各个站点上找到的各种脚本合并在一起,但是由于某些未知原因,它不起作用。

代码为以下内容,

    app.scriptPreferences.enableRedraw = false;
    var Directory = Folder.selectDialog ("Seleziona una cartella");
    if (!Directory) {
        exit();
        }
    var Documents = Directory.getFiles();
    var Dialwindow = new Window("dialog");
    Dialwindow.text = "Search";
    Dialwindow.orientation = "column";
    var Textbar = Dialwindow.add('edittext');
    Textbar.preferredSize.width = 150;
    Textbar.active = true
    var Buttons = Dialwindow.add("group", undefined);
    Buttons.orientation = "row";
    var Search = Buttons.add("button", undefined, undefined);
    Search.text = "Search";
    Search.onClick = function() {
        Dialwindow.close();
        }
    var Exit = Buttons.add("button", undefined, undefined);
    Exit.text = "Exit";
    Exit.onClick = function() {

// So far, so good, now I'm looking for a code that stops the script completely, as at the moment clicking on Search or Exit gives the same result

        Dialwindow.close();
        }
    Dialwindow.show();
    for (var i = 0; i < Documents.length; i++) {

// Now I think it begins the part that gives me problem

        app.open(Documents[i]);

// As far as I understand this opens every indd files one by one

            Collegamenti = app.activeDocument.links;
            Result = Textbar.text;

// This script was found on a site. Normally it works, but merging it doesn't

            var Array = new Array();
            for(var i=0; i < Collegamenti.length; i++) {
                var Nome = Collegamenti[i].name;
                Nome = Nome.toLowerCase();
                if(Nome.indexOf(Result.toLowerCase()) != -1)Array.push(Collegamenti[i]);
                }

// I'm not sure if this is written correctly

            if (Array.length= 1) {
                alert("You can find the image " + Result + " in this file " + app.activeDocument.name.replace(/.[^.]+$/,''));
                app.activeDocument.close(SaveOptions.NO);
                }
            else {
                app.activeDocument.close(SaveOptions.NO);
                }
        }

因此该脚本应让您选择一个文件夹,使用文本栏创建一个对话框窗口,浏览文件夹中的文档,如果找到带有该名称的映像触发了警报,否则请关闭文档。

我感谢任何可以解释出错或建议改进/更正的人。 如果由于我的英语不好,请问,如果有些问题不清楚,我会尽力更加清楚。


编辑

感谢Yuri的快速回答,我从您的代码中学到了一些新知识(例如“ while(){}”和“ .Shift”部分,我不知道) 。 不幸的是,在我工作的地方,滚动所有TXT文件以查找我正在搜索的内容(因为有很多INDD文件)并不是很实用,即使您的脚本使我免于浪费很多时间。 我深表歉意,但我可能并不十分清楚,因为我的目的是了解原始代码出了什么问题,因此我可以学习而不是拥有一些现成的代码。 我不明白的部分是:

    for (var i = 0; i < Documents.length; i++) {
        app.open(Documents[i]);

这打开了所有文档,一个基本上是正确的,但我不能使用app.activecument.links;因为没有主动文档。 有一种方法可以使用以上代码使用变量而不是app.activecument.links。 我尝试使用“ while(documents.length){var shift = documents.shift(); var doc = app.open(shift);”但是出于同样的原因,我不知道如何继续下一个代码。 另一件事是:

    var Array = new Array();
    var Links = app.activeDocument.links
        for(var i=0; i < Links.length; i++) {
            var Name = Links[i].name;
            Name = Name.toLowerCase();
            if(Name.indexOf(Textbar.text.toLowerCase()) != -1)Array.push(Links[i]);
            }

它可以与打开的所有文档一起使用,但是我不知道如何将其与“ app.open”(documents [i]);由于没有主动文档打开。 并且有一种方法可以使用button.onclick = function(){}停止脚本?我尝试了出口();但这无效。


edit2

感谢Yuri,这是最终代码:

    app.scriptPreferences.enableRedraw = false;
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
    var folder = Folder.selectDialog ("Select a folder");
    if (!folder) exit();
    var files = folder.getFiles('*.indd');
    if (files.length == 0) { alert("The folder doesn\'t contain any indd files"); exit() }
    var name = prompt("Write a file name:");
    if (name == null) { alert("Empty textbar"); exit() }
    name = name.toLowerCase();
    var total = [];
    while (files.length) {
        var doc = app.open(files.shift());
        var links = doc.links.everyItem().getElements();
        if (links.length == 0) { alert("No links are present in document\r''" + doc.name.replace(/\.[^\.]+$/, '') + "''");}
        var find = [];
        while (links.length) {
            var link = links.shift();
            if (link.name.toLowerCase().indexOf(name) > -1) {find.push(link); total.push(link);}}
        if (find.length > 0) { alert("The link ''" + name + "'' is\ron ''" + doc.name.replace(/\.[^\.]+$/, '') + "''");}
        doc.close();}
    alert("Done\r" + total.length + " links found");

I'm trying to make a script in JavaScript, but I'm not very experienced in this field, so I'm here to seek help.

Basically where I work I have a ton of .indd files with a lot of different images with different names and numbers every day.
When someone want to know in which document is a certain image I need to search manually for every image.

I'm trying to put together a working script, but without any luck.
What I'm looking for is a script that let you select a folder, let you type the name (or part of the name) of the filename to search for, search through all the documents in the folder without opening all the files at once (for performance reason as the files are a lot), and give you an alert if the file with that name is found.

I have merged together various scripts found on various sites, but it doesn't work for some unknown reason.

The code is the following

    app.scriptPreferences.enableRedraw = false;
    var Directory = Folder.selectDialog ("Seleziona una cartella");
    if (!Directory) {
        exit();
        }
    var Documents = Directory.getFiles();
    var Dialwindow = new Window("dialog");
    Dialwindow.text = "Search";
    Dialwindow.orientation = "column";
    var Textbar = Dialwindow.add('edittext');
    Textbar.preferredSize.width = 150;
    Textbar.active = true
    var Buttons = Dialwindow.add("group", undefined);
    Buttons.orientation = "row";
    var Search = Buttons.add("button", undefined, undefined);
    Search.text = "Search";
    Search.onClick = function() {
        Dialwindow.close();
        }
    var Exit = Buttons.add("button", undefined, undefined);
    Exit.text = "Exit";
    Exit.onClick = function() {

// So far, so good, now I'm looking for a code that stops the script completely, as at the moment clicking on Search or Exit gives the same result

        Dialwindow.close();
        }
    Dialwindow.show();
    for (var i = 0; i < Documents.length; i++) {

// Now I think it begins the part that gives me problem

        app.open(Documents[i]);

// As far as I understand this opens every indd files one by one

            Collegamenti = app.activeDocument.links;
            Result = Textbar.text;

// This script was found on a site. Normally it works, but merging it doesn't

            var Array = new Array();
            for(var i=0; i < Collegamenti.length; i++) {
                var Nome = Collegamenti[i].name;
                Nome = Nome.toLowerCase();
                if(Nome.indexOf(Result.toLowerCase()) != -1)Array.push(Collegamenti[i]);
                }

// I'm not sure if this is written correctly

            if (Array.length= 1) {
                alert("You can find the image " + Result + " in this file " + app.activeDocument.name.replace(/.[^.]+$/,''));
                app.activeDocument.close(SaveOptions.NO);
                }
            else {
                app.activeDocument.close(SaveOptions.NO);
                }
        }

So this script should let you select a folder, create a dialog window with a text bar, search through the documents in the folder, if an image with that name is found trigger an alert, else close the document.

I thank anyone who can explain what is wrong or can suggest improvements/corrections.
If something is not clear due to my bad English, please ask, I'll try to be more clear.


EDIT

Thanks Yuri for your quick answer, I have learned a few new things from your code (like the "while() {}" and the ".shift" parts that I didn't know about).
Unfortunately, where I work, it's not very practical to scroll all the txt file to find what I was searching (as there are a lot of indd files), even if your script saves me from wasting a lot of time.
I apologize but I have probably not been very clear, as my intent was to understand what is wrong with the original code, so I could learn instead of having some ready-to-go code.
The parts I don't understand are:

    for (var i = 0; i < Documents.length; i++) {
        app.open(Documents[i]);

This opens all the documents, one by one which is essentially correct, but I can't use app.activeDocument.links; because there is no active document.
There is a way to use a variable instead of app.activeDocument.links with the code above?
I've tried to use the "while (Documents.length) {var Shift = Documents.shift();var Doc = app.open(Shift);" but for the same reason I don't know how to continue with the next code.
Another thing is this:

    var Array = new Array();
    var Links = app.activeDocument.links
        for(var i=0; i < Links.length; i++) {
            var Name = Links[i].name;
            Name = Name.toLowerCase();
            if(Name.indexOf(Textbar.text.toLowerCase()) != -1)Array.push(Links[i]);
            }

It works with all the documents opened, but I don't know how use it with the "app.open(Documents[i]);" as there is no active document opened.
And there is a way to stop the script with the Button.onClick = function() {}? I've tried exit(); but it doesn't work.


EDIT2

Thanks to Yuri, this is the final code:

    app.scriptPreferences.enableRedraw = false;
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
    var folder = Folder.selectDialog ("Select a folder");
    if (!folder) exit();
    var files = folder.getFiles('*.indd');
    if (files.length == 0) { alert("The folder doesn\'t contain any indd files"); exit() }
    var name = prompt("Write a file name:");
    if (name == null) { alert("Empty textbar"); exit() }
    name = name.toLowerCase();
    var total = [];
    while (files.length) {
        var doc = app.open(files.shift());
        var links = doc.links.everyItem().getElements();
        if (links.length == 0) { alert("No links are present in document\r''" + doc.name.replace(/\.[^\.]+$/, '') + "''");}
        var find = [];
        while (links.length) {
            var link = links.shift();
            if (link.name.toLowerCase().indexOf(name) > -1) {find.push(link); total.push(link);}}
        if (find.length > 0) { alert("The link ''" + name + "'' is\ron ''" + doc.name.replace(/\.[^\.]+$/, '') + "''");}
        doc.close();}
    alert("Done\r" + total.length + " links found");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2025-01-26 07:19:52

由于您的代码包含太多错误,因此很难解释每一个错误。我尝试修复错误并保持算法或多或少完整。这是工作代码:

var directory = Folder.selectDialog("Seleziona una cartella");
if (!directory) exit();

// dialog window -------------------------------------------------------
var dialwindow = new Window("dialog", "Search");

    var textbar = dialwindow.add('edittext');
        textbar.preferredSize.width = 150;
        textbar.active = true

    var buttons = dialwindow.add("group", undefined);
        buttons.orientation = "row";

    var search = buttons.add("button", undefined, "Search");
    var cancel = buttons.add("button", undefined, "Exit");

dialwindow.defaultElement = search;
dialwindow.cancelElement  = cancel;

if (dialwindow.show() != 1) exit();

// main code -----------------------------------------------------------
var result = textbar.text;
var documents = directory.getFiles('*.indd');

for (var i = 0; i < documents.length; i++) {
    var doc = app.open(documents[i]);
    var links = doc.links;
    var arr = [];

    for (var j = 0; j < links.length; j++) {
        var link_name = links[j].name.toLowerCase();
        if (link_name.indexOf(result.toLowerCase()) > -1) {
            arr.push(links[j]);
            break;
        }
    }

    if (arr.length > 0) {
        alert("You can find the image " + result + " in this file " + doc.name.replace(/\.[^\.]+$/, ''));
    }

    doc.close(SaveOptions.NO);
}

Since your code contains too much errors it's hard to explain every single one of them. I've tried fix the errors and keep your algorithm more or less intact. Here is the working code:

var directory = Folder.selectDialog("Seleziona una cartella");
if (!directory) exit();

// dialog window -------------------------------------------------------
var dialwindow = new Window("dialog", "Search");

    var textbar = dialwindow.add('edittext');
        textbar.preferredSize.width = 150;
        textbar.active = true

    var buttons = dialwindow.add("group", undefined);
        buttons.orientation = "row";

    var search = buttons.add("button", undefined, "Search");
    var cancel = buttons.add("button", undefined, "Exit");

dialwindow.defaultElement = search;
dialwindow.cancelElement  = cancel;

if (dialwindow.show() != 1) exit();

// main code -----------------------------------------------------------
var result = textbar.text;
var documents = directory.getFiles('*.indd');

for (var i = 0; i < documents.length; i++) {
    var doc = app.open(documents[i]);
    var links = doc.links;
    var arr = [];

    for (var j = 0; j < links.length; j++) {
        var link_name = links[j].name.toLowerCase();
        if (link_name.indexOf(result.toLowerCase()) > -1) {
            arr.push(links[j]);
            break;
        }
    }

    if (arr.length > 0) {
        alert("You can find the image " + result + " in this file " + doc.name.replace(/\.[^\.]+$/, ''));
    }

    doc.close(SaveOptions.NO);
}
慢慢从新开始 2025-01-26 07:19:52

开始吧:

var file_prefs = 'find_links.json';
var prefs = load_prefs();

main();

function main() {

    // get all indd files from the folder
    var folder = get_folder();
    prefs.folder = folder.fullName;
    var files = get_files(folder);
    if (files.length == 0) { alert('The folder doesn\'t contain any indd files'); exit() }

    // get the link name to search for
    var name = prompt("Write a file name:", prefs.name);
    if (name == null) exit();
    prefs.name = name;

    var report = ['Folder: ' + folder.fsName];
    report.push(['Search for: ' + name]);
    name = name.toLowerCase();

    // search the name among of the links of every indd document
    while (files.length) {
        var doc = app.open(files.shift());
        report.push('\n' + doc.name);

        var links = doc.links.everyItem().getElements();
        while (links.length) {
            var link = links.shift();
            if (link.name.toLowerCase().indexOf(name) > -1)
                report.push(link.filePath);
        }

        doc.close();
    }

    // save the report in a text file and open the file
    var report_file = File(folder + '/report.txt');
    report_file.encoding = 'utf-8';
    report_file.open('w');
    report_file.write(report.join('\n'));
    report_file.close();
    if (report_file.exists) report_file.execute();

}

save_prefs();

// ----------------------------------------------------------------------------
function load_prefs() {
    var file = File(Folder.temp + '/' + file_prefs);
    if (!file.exists) return { folder: '', name: '' }
    return $.evalFile(file);
}

function save_prefs() {
    var file = File(Folder.temp + '/' + file_prefs);
    file.encoding = 'utf-8';
    file.open('w');
    file.write(prefs.toSource());
}

function get_folder() {
    if (prefs.folder != '') { var folder = Folder(prefs.folder) }
    else { var folder = new Folder() }
    return folder.selectDlg();
}

function get_files(folder) {
    var files = folder.getFiles('*.indd');
    files = files.sort(sortFileNames);
    return files;
}

// additional function to correct sorting
// https://community.adobe.com/t5/indesign-discussions/help-tweaking-indesign-script-to-alphabetize-imagecatalog-jsx/td-p/10812327
function sortFileNames( a,b ) {

    a = a.name;
    b = b.name;

    var myRegExp = new RegExp('^\\d+');
    var aObj = {num: a.match(myRegExp) != null ? a.match(myRegExp)[0] : '_', string: a.replace(/^\d+/, '')};
    var bObj = {num: b.match(myRegExp) != null ? b.match(myRegExp)[0] : '_', string: b.replace(/^\d+/, '')};

    // a and b are fully numeric :
    if (aObj.string =='' && bObj.string == '') return a - b;

    // the numeric parts of a and b are equal :
    if (aObj.num == bObj.num ) return aObj.string.toLowerCase() > bObj.string.toLowerCase();

    // the numeric part of a is greater than the numeric part of b :
    if (aObj.num > bObj.num ) return 1 ;
};

脚本询问文件夹和文件名(或部分名称),打开给定文件夹中的所有 indd 文件,并在每个 indd 文件的链接中查找给定名称。然后它将报告保存在文件“report.txt”中(到同一文件夹中)并打开 txt 文件。

Here you go:

var file_prefs = 'find_links.json';
var prefs = load_prefs();

main();

function main() {

    // get all indd files from the folder
    var folder = get_folder();
    prefs.folder = folder.fullName;
    var files = get_files(folder);
    if (files.length == 0) { alert('The folder doesn\'t contain any indd files'); exit() }

    // get the link name to search for
    var name = prompt("Write a file name:", prefs.name);
    if (name == null) exit();
    prefs.name = name;

    var report = ['Folder: ' + folder.fsName];
    report.push(['Search for: ' + name]);
    name = name.toLowerCase();

    // search the name among of the links of every indd document
    while (files.length) {
        var doc = app.open(files.shift());
        report.push('\n' + doc.name);

        var links = doc.links.everyItem().getElements();
        while (links.length) {
            var link = links.shift();
            if (link.name.toLowerCase().indexOf(name) > -1)
                report.push(link.filePath);
        }

        doc.close();
    }

    // save the report in a text file and open the file
    var report_file = File(folder + '/report.txt');
    report_file.encoding = 'utf-8';
    report_file.open('w');
    report_file.write(report.join('\n'));
    report_file.close();
    if (report_file.exists) report_file.execute();

}

save_prefs();

// ----------------------------------------------------------------------------
function load_prefs() {
    var file = File(Folder.temp + '/' + file_prefs);
    if (!file.exists) return { folder: '', name: '' }
    return $.evalFile(file);
}

function save_prefs() {
    var file = File(Folder.temp + '/' + file_prefs);
    file.encoding = 'utf-8';
    file.open('w');
    file.write(prefs.toSource());
}

function get_folder() {
    if (prefs.folder != '') { var folder = Folder(prefs.folder) }
    else { var folder = new Folder() }
    return folder.selectDlg();
}

function get_files(folder) {
    var files = folder.getFiles('*.indd');
    files = files.sort(sortFileNames);
    return files;
}

// additional function to correct sorting
// https://community.adobe.com/t5/indesign-discussions/help-tweaking-indesign-script-to-alphabetize-imagecatalog-jsx/td-p/10812327
function sortFileNames( a,b ) {

    a = a.name;
    b = b.name;

    var myRegExp = new RegExp('^\\d+');
    var aObj = {num: a.match(myRegExp) != null ? a.match(myRegExp)[0] : '_', string: a.replace(/^\d+/, '')};
    var bObj = {num: b.match(myRegExp) != null ? b.match(myRegExp)[0] : '_', string: b.replace(/^\d+/, '')};

    // a and b are fully numeric :
    if (aObj.string =='' && bObj.string == '') return a - b;

    // the numeric parts of a and b are equal :
    if (aObj.num == bObj.num ) return aObj.string.toLowerCase() > bObj.string.toLowerCase();

    // the numeric part of a is greater than the numeric part of b :
    if (aObj.num > bObj.num ) return 1 ;
};

The script asks folder and file name (or part of the name), the open all indd files in the given folder and find the given name among links of each of the indd files. Then it saves report in file 'report.txt' (into the same folder) and open the txt file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文