Copy link to clipboard
Copied
Hi all,
I want to create 10 conditions! So, I can do it like this and it works fine!
myCondition = app.activeDocument.conditions.item("Temp_01");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_01", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
myCondition = app.activeDocument.conditions.item("Temp_02");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_02", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
myCondition = app.activeDocument.conditions.item("Temp_03");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_03", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
myCondition = app.activeDocument.conditions.item("Temp_04");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_04", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
myCondition = app.activeDocument.conditions.item("Temp_05");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_05", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
myCondition = app.activeDocument.conditions.item("Temp_06");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_06", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
myCondition = app.activeDocument.conditions.item("Temp_07");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_07", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
myCondition = app.activeDocument.conditions.item("Temp_08");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_08", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
myCondition = app.activeDocument.conditions.item("Temp_09");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_09", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
myCondition = app.activeDocument.conditions.item("Temp_10");
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_10", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
… But I really think there's a best, shorter and more beautiful way to write it!
Thanks in advance!
Copy link to clipboard
Copied
Obi-wan, welcome to the dark side (of scripting, that is).
First thing to learn:
Copy link to clipboard
Copied
Cool! I I didn't know this site! Thank you Vamitul!
Copy link to clipboard
Copied
Right?
for (var i = 0; i < 10; i++) {
myCondition = app.activeDocument.conditions.item("Temp_0" + (i));
if (!myCondition.isValid)
{
myCondition = app.activeDocument.conditions.add ({name: "Temp_0" + (i), indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
}
}
Copy link to clipboard
Copied
well now.. try it and see what happens.
(look at the names of the conditions too).
A fast way to get a bit up to speed with javascript in general is JavaScript Tutorial
It won't teach you very good javascript, but good enough to get the basics, and from there there are tons and tons of tutorials and books and resources.
Copy link to clipboard
Copied
I'd tested before posting! It's good!
Thanks for the link!
Copy link to clipboard
Copied
No it's not.
Your original code adds conditions named from "Temp_01" to "Temp_10"
Your "for" code adds conditions named from "Temp_00" to "Temp_09"
To fix that you can either
change your loop to start from 1 and end after 10: for (var i=1; i<=10; i++)
or change the way you name the conditions: myCondition = app.activeDocument.conditions.item("Temp_0" + (i+1))
of course either of those still leaves you with one big problem: the tenth condition will not be named "Temp_10" but "Temp_010". There are quite a lot of ways to fix that, but i'll leave you to discover them (stackoverflow is your friend) since it's after midnight here and i do need my beauty sleep
May the force blah blah..
Copy link to clipboard
Copied
Vamitul,
In fact, I really wanted Temp_00 to Temp_09! True!
Thanks!
Copy link to clipboard
Copied
Hi Obi-wan,
Now there are good pratices I'd to highlight if you throw yourself into InDesign/ExtendScript programming:
1. Try to keep your settings distinct from the core.
Ok you just want to create 10 conditions and use "Temp_XX" for naming those ones, but this is arbitrary and shouldn't be part of the logic of a well-designed code. Suppose your client suddenly wants 15 conditions and feels "MyCond_XX" less obscure than "Temp_XX"? Then you'll waste time, again and again, at rewriting the inner routine while nothing actually needs to change in it. As a more general rule, separating data from processes is almost always a good move.
2. Make it functional.
Really, declaring a function is not a big deal. And it helps you enhance both the abstraction and the modularity of your script. Of course that seems superfluous for a 7-line project, but why not exercise the right gestures on simple codes? In addition, you can easily quit a function using return when some requirement is not satisfied. For example, when no document is available.
3. Minimize browsing stages within the DOM hierarchy.
Each time a loop is in action—which constantly happens in automation!—keep in mind that recurring DOM paths, such as app.activeDocument.conditions, involve commands that are executed at each iteration. Those commands take time and, in a serious project where function calls and loops are nested into each other, this leads sooner or later to performance issues. Hence, DOM specifiers that can be resolved before the loop starts up should be stored once (provided that they won't change during the process.) I suggest to use constants for things that are absolutely invariant (e.g. the value ConditionIndicatorMethod.USE_HIGHLIGHT), and variables for things that remains at some level context-relative (app.activeDocument), but it's a question of taste. My point is no to recompute paths and values we already have.
Here is an expeditious implementation of these few principles:
// Settings
// ---
const HOW_MANY_CONDS = 10;
const NAME_PATTERN = "Temp_%1";
function createConds(/*Document*/doc,/*uint*/count,/*str*/pattern)
{
if( !doc ) throw "No document available.";
// Local constants.
// ---
const COND_METH = ConditionIndicatorMethod.USE_HIGHLIGHT,
__ = $.global.localize;
// Local variables.
// ---
var conds = doc.conditions,
condName,
i;
// Loop.
// ---
for( i=0 ; i < count ; ++i )
{
condName = __( pattern, ('0'+i).substr(-2) );
if( conds.itemByName(condName).isValid ) continue;
conds.add({name:condName, indicatorMethod:COND_METH});
}
};
// Call the routine.
// ---
createConds(app.properties.activeDocument, HOW_MANY_CONDS, NAME_PATTERN);
@+
Marc
Copy link to clipboard
Copied
Merci Marc ! Là, c'est du "lourd" ! …
J'en suis encore à essayer de sauter à pieds joints sur la table et voici 33 lignes de code dignes des programmes Apollo!
Mais j'ai bien l'intention d'organiser une petite sauterie sur Mars d'ici la fin du siècle, histoire de me rapprocher de Tataouine ! …
Alors, je continuerai, contre vents et marées, ce passionnant apprentissage de Javascript et je continuerai à solliciter la matière grise de tous ceux présents sur ce forum d'Adobe que je considère être la plus belle clique de professeurs de la planète ! [Ceci dit, je ne connais pas ceux du reste du système solaire !]
Merci à tous ! … et merci pour votre patience !!