Copy link to clipboard
Copied
Hi,
I have made a script that is working good, but running very slowly.
Can any edit run faster than current?
#target "InDesign-8.0"
var doc = app.activeDocument;
attrDelete(doc);
function attrDelete(span)
{
for (var i = 0; i < span.xmlElements.length; i++)
{
try{
for(n=0; n<span.xmlElements.xmlAttributes.length; n++)
{
if(span.xmlElements.xmlAttributes
.name == "aid:cstyle") {
if(span.xmlElements.xmlContent.contents <= 643 && span.xmlElements.xmlContent.contents>= 608){
span.xmlElements.xmlAttributes.add("href", "M18_BERK9695_07_SE_C18.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 607 && span.xmlElements.xmlContent.contents>= 568){
span.xmlElements.xmlAttributes.add("href", "M17_BERK9695_07_SE_C17.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 567 && span.xmlElements.xmlContent.contents>= 536){
span.xmlElements.xmlAttributes.add("href", "M16_BERK9695_07_SE_C16.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 535 && span.xmlElements.xmlContent.contents>= 506){
span.xmlElements.xmlAttributes.add("href", "M15_BERK9695_07_SE_C15.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 505 && span.xmlElements.xmlContent.contents>= 468){
span.xmlElements.xmlAttributes.add("href", "M14_BERK9695_07_SE_C14.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 467 && span.xmlElements.xmlContent.contents>= 436){
span.xmlElements.xmlAttributes.add("href", "M13_BERK9695_07_SE_C13.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 435 && span.xmlElements.xmlContent.contents>= 406){
span.xmlElements.xmlAttributes.add("href", "M12_BERK9695_07_SE_C12.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 405 && span.xmlElements.xmlContent.contents>= 366){
span.xmlElements.xmlAttributes.add("href", "M11_BERK9695_07_SE_C11.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 365 && span.xmlElements.xmlContent.contents>= 334){
span.xmlElements.xmlAttributes.add("href", "M10_BERK9695_07_SE_C10.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 333 && span.xmlElements.xmlContent.contents>= 292){
span.xmlElements.xmlAttributes.add("href", "M09_BERK9695_07_SE_C09.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 291 && span.xmlElements.xmlContent.contents>= 256){
span.xmlElements.xmlAttributes.add("href", "M08_BERK9695_07_SE_C08.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 255 && span.xmlElements.xmlContent.contents>= 214){
span.xmlElements.xmlAttributes.add("href", "M07_BERK9695_07_SE_C07.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 213 && span.xmlElements.xmlContent.contents>= 182){
span.xmlElements.xmlAttributes.add("href", "M06_BERK9695_07_SE_C06.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 181 && span.xmlElements.xmlContent.contents>= 148){
span.xmlElements.xmlAttributes.add("href", "M05_BERK9695_07_SE_C05.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 147 && span.xmlElements.xmlContent.contents>= 114){
span.xmlElements.xmlAttributes.add("href", "M04_BERK9695_07_SE_C04.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 113 && span.xmlElements.xmlContent.contents>= 74){
span.xmlElements.xmlAttributes.add("href", "M03_BERK9695_07_SE_C03.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else if(span.xmlElements.xmlContent.contents <= 73 && span.xmlElements.xmlContent.contents>=42){
span.xmlElements.xmlAttributes.add("href", "M02_BERK9695_07_SE_C02.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
else(span.xmlElements.xmlContent.contents <= 41){
span.xmlElements.xmlAttributes.add("href", "M01_BERK9695_07_SE_C01.xhtml#page"+span.xmlElements.xmlContent.contents);
span.xmlElements.markupTag = "a";
span.xmlElements.xmlAttributes.item("class").remove()
}
}
}
}catch(e){}
attrDelete(span.xmlElements);
}
}
alert("Done!\rCreated by Sumit from Elhi 7.")
Sumit
Hi Oriup,
Here is likely how I would do that, although there might be a faster approach:
...// YOUR SETTINGS
// ---
const ATT_NAME_TARGET = 'aid:cstyle';
const HREF_PATTERN = "M%1_BERK9695_07_SE_C%1.xhtml#page%2";
const RANGES = [41,73,113,147,181,213,255,291,333,365,405,435,467,505,535,567,607,643];
const PAGE_TO_SECTION = (function(/*uint[]&*/a, n,s,i)
// -------------------------------------
// Return the func that converts a page number into a
// section number (1..N) depending on RANGES.
// E.g. PAGE_TO
Copy link to clipboard
Copied
Hi Oriup,
Here is likely how I would do that, although there might be a faster approach:
// YOUR SETTINGS
// ---
const ATT_NAME_TARGET = 'aid:cstyle';
const HREF_PATTERN = "M%1_BERK9695_07_SE_C%1.xhtml#page%2";
const RANGES = [41,73,113,147,181,213,255,291,333,365,405,435,467,505,535,567,607,643];
const PAGE_TO_SECTION = (function(/*uint[]&*/a, n,s,i)
// -------------------------------------
// Return the func that converts a page number into a
// section number (1..N) depending on RANGES.
// E.g. PAGE_TO_SECTION(320) => 9
{
for( n=a.length, s='1', i=-1 ; ++i < n ; s+='+(x>'+a+')' );
return Function('x', 'return '+s);
})(RANGES);
const updateAttributes = function F(/*DOM*/span, a,i,x,atts,p,s,t)
// -------------------------------------
// Recursive function that update all XMLElems having an
// ATT_NAME_TARGET attribute.
{
// Aliases.
// ---
const __ = $.global.localize;
const P2S = PAGE_TO_SECTION;
// Loop.
// ---
for( a=span.xmlElements.everyItem().getElements(), i=a.length ; i-- ; F(x) )
{
atts = (x=a).xmlAttributes;
// Check some conditions.
// ---
if( !atts.itemByName(ATT_NAME_TARGET).isValid ) continue;
if( ! ('contents' in (t=x.xmlContent)) ) continue;
// Page number (assuming contents represents such number!)
p = parseInt(t.contents,10);
// Apply the 'a' markuptag to the xmlElement.
// ---
x.markupTag = 'a';
// Add (or rewrite!) the 'href' attribute.
// ---
if( (t=atts.itemByName('href')).isValid ) t.remove();
isNaN(p) || atts.add('href', __(HREF_PATTERN,('0'+P2S(p)).substr(-2),p));
// Remove the 'class' attribute, if present.
// ---
if( (t=atts.itemByName('class')).isValid ) t.remove();
}
};
// Test.
// ---
var doc = app.properties.activeDocument;
updateAttributes(doc);
alert("Done!");
@+
Marc
Copy link to clipboard
Copied
Thank you Marc.
Copy link to clipboard
Copied
Hi Marc,
If you have time, can you explain more on these lines?
for( n=a.length, s='1', i=-1 ; ++i < n ; s+='+(x>'+a+')' ); //how to use it?
const __ = $.global.localize; //what is the use of it?
for( a=span.xmlElements.everyItem().getElements(), i=a.length ; i-- ; F(x) ) // Use of F(x)
isNaN(p) || atts.add('href', __(HREF_PATTERN,('0'+P2S(p)).substr(-2),p)); // attribute value
Sumit
Copy link to clipboard
Copied
Hi @Marc Autret ,
Below line always return 75 instead of input.
// E.g. PAGE_TO_SECTION(320) => 9
Regards,
Sumit
Copy link to clipboard
Copied
The code marked as correct here has been corrupted during the forum migration. I have corrected some portion of it and is posted at the following link
https://community.adobe.com/t5/indesign-discussions/how-to-check-where-x-falls/m-p/12544143#M454758
Go through the whole thread that I posted, it will surely help if you reached here looking for some answers
-Manan
Copy link to clipboard
Copied
Thank you Manan.
As you suggested, I have replaced
for( n=a.length, s='1', i=-1 ; ++i < n ; s+='+(x>'+a+')' );
to:
for( n=a.length, s='1', i=-1 ; ++i < n ; s+='+(x>'+a[i]+')' );
The [i] is missing due to migration.
Regards,