• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Faster than current speed

Engaged ,
Nov 20, 2017 Nov 20, 2017

Copy link to clipboard

Copied

Hi All,

I have created a script for index hyperlinking from external file, this is running well but very slower.

Can anyone guide me how to run faster than current speed.

Here is my script:

//global variables

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

var indexIndd, myFolder, sourceDoc;

var findText1 = "(?<![-~=])\\<\\d+\\>(?![-~=])|(?<![-~=])\\<\\d+(?=[-~=tf])";

var findText2 = "(?<=\\d[-~=])\\d+(?=[f|t])|(?<=PEx-)\\<\\d+\\>";

destDoc = [];

main ();

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

function main () {

  indexIndd = File.openDialog ("Select index file...", filter_files, false); //select index indesign file

  myFolder = Folder(indexIndd.parent);

  create_book ();

  open_files_from_book ();

  sourceDoc = app.open(indexIndd, false);

  create_hyperlink_range ();

  create_hyperlink (findText1, "_n")

  alert("Done!");

}

function create_book (){

  var myFileList = myFolder.getFiles();

  if(myFileList.length > 0){ //check file length

  var myBookName = new File(myFolder + "/" + myFolder.name + ".indb"); //book name define here

  if(myBookName.exists){

  try {

  var myBook = app.open(File(myBookName));

  }catch(e) {

  myBook = app.books[0];

  }

  } else {

  var myBook = app.books.add(myBookName);

  myBook.automaticPagination = false;

  add_files_into_book (myFileList);

  myBook.save();

  }

  }

}

function add_files_into_book (myFileList){

  for(var a=0; a<myFileList.length; a++){

  var myFile = myFileList;

  if(myFile instanceof File && myFile.name.match(/\.indd$/i)){

  try{

  var myBook = app.books[0].bookContents.add(myFile);

  }catch(e){

  alert(myFile.name + " uses one or more plug-ins that are not currently available on your system. Cannot add this document to the book.");

  myBook.save();

  exit(0);

  }

  }

  }

}

function open_files_from_book (){

  var myBookContent = app.books[0].bookContents;

  for(var b=0; b<myBookContent.length; b++){

  var destinationFile = app.open(myBookContent.fullName, false);

  destDoc.push([destinationFile, destinationFile.pages.firstItem(), destinationFile.pages.lastItem()]);

  }

}

function create_hyperlink (findText, plusName) {

  app.findGrepPreferences = null;

  app.findGrepPreferences.findWhat = findText;

  var fnds = sourceDoc.findGrep();

  for (var c = 0; c < fnds.length; c++){

  var myFound = fnds;

  for (var d = 0; d < destDoc.length; d++) {

  if (Number(myFound.contents) >= Number(destDoc[1].name) &&

  Number(myFound.contents) <= Number(destDoc[2].name)) {

  var myLinkName = "#" + (c+1) + " to page " + myFound.contents + plusName;

  try {

  try {

  var mySource = sourceDoc.hyperlinkTextSources.add(myFound, {name: myLinkName});

  }catch(e){}

  try {

  var myDestination = create_hyperlink_destination (sourceDoc, destDoc[0], myFound.contents, myLinkName);

  }catch(e){}

  var myLink = sourceDoc.hyperlinks.add(mySource, myDestination, {name: myLinkName, visible: false});

  }catch (e) {}

  }

  }

  }

}

function create_hyperlink_range () {

  app.findGrepPreferences = null;

  app.findGrepPreferences.findWhat = "\\<\\d+[-~=]\\d+\\>";

  var dest = sourceDoc.findGrep();

  app.findGrepPreferences = null;

  app.findGrepPreferences.findWhat = "(?<=\\d[-~=])\\d+\\>";

  var find = sourceDoc.findGrep();

  app.findGrepPreferences = null;

  if(dest.length == find.length){

  for (var e = 0; e < find.length; e++){

  var myFounds = find;

  var spText = dest.contents.split(/\D/);

  var firstText = spText[0].substr(0, spText[0].length - spText[spText.length - 1].length) + spText[spText.length - 1];

  for (var f = 0; f < destDoc.length; f++) {

  if (Number(firstText) >= Number(destDoc[1].name) &&

  Number(firstText) <= Number(destDoc[2].name)) {

  var myLinkName = "#" + (e+1) + " to page range " + firstText;

  try {

  try {

  var mySourceDoc = sourceDoc.hyperlinkTextSources.add(myFounds, {name: myLinkName});

  }catch(e){}

  try {

  var myDestinationDoc = create_hyperlink_destination (sourceDoc, destDoc[0], firstText, myLinkName);

  }catch(e){}

  var myLink = sourceDoc.hyperlinks.add(mySourceDoc, myDestinationDoc, {name: myLinkName, visible: false});

  }catch (e) {}

  }

  }

  }

  }else {

  exit();

  }

}

function create_hyperlink_destination(sourceDoc, destinationDoc, theDestPage, myLinkName){

  try {

  if (sourceDoc.name != destinationDoc.name) {

  var destinationPage = destinationDoc.pages.itemByName(theDestPage);

  var myHyperlinkDestination = sourceDoc.hyperlinkExternalPageDestinations.add(destinationPage, {name: myLinkName});

  myHyperlinkDestination.viewSetting = HyperlinkDestinationPageSetting.fitHeight;

  } else {

  var destinationPage = sourceDoc.pages.itemByName(theDestPage);

  var myHyperlinkDestination = sourceDoc.hyperlinkPageDestinations.add(destinationPage);

  myHyperlinkDestination.viewSetting = HyperlinkDestinationPageSetting.fitHeight;

  }

  }catch(e) {}

  return myHyperlinkDestination;

}

//supporting functions

function filter_files(indexIndd) {

  if (indexIndd.constructor.name == "Folder") { return true }

  if (indexIndd.name.slice(-5) == ".indd") { return true }

  return false;

}

Thanks

Sumit

-Sumit
TOPICS
Scripting

Views

235

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation