简体   繁体   English

Indesign Slug Text 变量脚本

[英]Indesign Slug Text variable script

I'm trying to come up with a script that can be loaded into the InDesign script panel, but that will also run when you open a given document.我正在尝试提出一个可以加载到 InDesign 脚本面板中的脚本,但是当您打开给定文档时它也会运行。

My documents are named like "REF_ClientRef" , which typically looks like 4294_GEN1 .我的文档被命名为"REF_ClientRef" ,通常看起来像4294_GEN1

I've got two text variables called REF and ClientRef which represent the first part and the second part of the file name.我有两个名为REFClientRef的文本变量,它们代表文件名的第一部分和第二部分。

How do I update the text variable REF with the first part of the file name and update ClientRef with the second part of the file name?如何使用文件名的第一部分更新文本变量 REF 并使用文件名的第二部分更新ClientRef

Here is two scripts that do the job.这是完成这项工作的两个脚本。

The main script creates or changes the variables:主脚本创建或更改变量:

try { var doc = app.activeDocument } catch(e) { exit() }

if (!doc.saved) exit();

// get a name of the current file without .indd
var doc_name = doc.name.replace('.indd', '');

// get a part of the file name before '_' and save it into the variable
var ref = doc_name.split('_')[0];
update_variable('REF', ref);

// try to get a part of the file name after '_' and save it into the variable
try {
    var client = doc_name.split('_')[1];
    update_variable('ClientRef', client);
} catch (e) {}

// function that changes an existed variable or creates new one
function update_variable(var_name, var_contents) {
    try {
        var variable = doc.textVariables.itemByName(var_name);
        variable.variableOptions.contents = var_contents;
    }
    catch(e) {
        var variable = doc.textVariables.add();
        variable.name = var_name;
        variable.variableType = VariableTypes.CUSTOM_TEXT_TYPE;
        variable.variableOptions.contents = var_contents;
    }
}

You can run it as usual from Script Panel any time.您可以随时从脚本面板照常运行它。

The second script makes InDesing to run the main script automatically every time you open any document:第二个脚本使 InDesing 在您每次打开任何文档时自动运行主脚本:

#targetengine "onAfterOpen"

app.eventListeners.add('afterOpen', main);

function main() {
    app.doScript(File('c:/path_to_the_main_script/main_script.jsx'));
}

You can run the second script manually or you can save it into the special folder.您可以手动运行第二个脚本,也可以将其保存到特殊文件夹中。 On Windows it can look look like this:在 Windows 上,它看起来像这样:

c:\Users\#username#\AppData\Roaming\Adobe\InDesign\#version#\en_GB\Scripts\Startup Scripts\

It can differ for your version, locale, OS, etc. Actually it's just a folder with name 'Startup Scripts' next to the folder with the rest of your scripts ('Scripts Panel').它可能因您的版本、区域设置、操作系统等而有所不同。实际上,它只是一个名称为“启动脚本”的文件夹,位于您的脚本的 rest 文件夹旁边(“脚本面板”)。 Any script in this folder runs automatically whenever you start InDesign.每当您启动 InDesign 时,此文件夹中的所有脚本都会自动运行。

And I think it's a good idea to limit the main script that way it will run only for particular files (particular file names, for example) or folders.而且我认为将主脚本限制为仅针对特定文件(例如特定文件名)或文件夹运行是一个好主意。 It depends on your workflow.这取决于您的工作流程。 If you can formulate a rule I could try to show how it can be done.如果你能制定一条规则,我可以尝试展示它是如何完成的。

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

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