'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.
My documents are named like "REF_ClientRef", which typically looks like 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.
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?
Solution 1:[1]
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:
#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:
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'). Any script in this folder runs automatically whenever you start 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
