Hi,
Can I suggest another approach ? Having a second look at your code, a few things are problematic. You are for example changing the PAGE SIZE variable content per page. Doing so, you are just flattening values over and over and only the last one remains. That doesn’t make much sense to me.
So FWIW
#targetengine “onApplicationEvent”
main();
function main(){
var myApplicationEventListener = app.idleTasks.item(“onApplicationEvent”);
if ( !myApplicationEventListener.isValid) {
myApplicationEventListener = app.idleTasks.add({name:”onApplicationEvent”, sleep:10});
myApplicationEventListener.addEventListener(IdleEvent.ON_IDLE, onApplicationEventHandler, false);
}
}
function onApplicationEventHandler(idleEvent){
const IN = MeasurementUnits.inches;
var doc = app.properties.activeDocument;
if ( !doc ) return;
doc.viewPreferences.properties = {
horizontalMeasurementUnits:IN,
verticalMeasurementUnits:IN,
}
var fileNameVariable = doc.name.replace(/\.indd$/i, “”);
makeVariable (doc, “RE_Docket”, fileNameVariable);
//dealing with page variable but that can only host one value for all pages
makeVariable (doc, “RE_PageSize”, “some value that needs to cbe computed”);
//doc.fullName may throw error is document is not saved (i.e. converted or new)
//index [3] may cause undefined value of unadequate value so it’s preferable to circumvent this possibility
var client = doc.fullName.split(“/”);
client.length>=4 && client = client[3];
makeVariable (doc, “RE_Client”, doc.properties.fullName? String(client) : “” );
}
function makeVariable (doc, name, contents) {
var tv = doc.textVariables.item ( name );
!tv.isValid && tv = doc.textVariables.add ( );
tv.properties = {
name:name,
type:VariableTypes.CUSTOM_TEXT_TYPE,
}
tv.variableOptions.contents = contents;
}