0

I am trying to fix all of the hyperlinks in my indesign files, and replace the https with http. right now, in order for it to work, I run this script..

var
  i;
hls = app.activeDocument.hyperlinkURLDestinations;
for (i = 0; i < hls.length; i++) {
  if (!hls[i].destinationURL.match('http://')) {
    hls[i].destinationURL = 'http://' + hls[i].destinationURL;
  }
}

followed by this script, choosing https to be replaced by http...

Menu for find/replace

main();
function main(){
 var d = app.dialogs.add({name:"Replace Hyperlink URL Values"});
 var col1 = d.dialogColumns.add();
 var col2 = d.dialogColumns.add();
 col1.staticTexts.add({staticLabel:"Find (GREP):"});
 col1.staticTexts.add({staticLabel:"Replace:"});
 var find = col2.textEditboxes.add({minWidth:100});
 var change = col2.textEditboxes.add({minWidth:100});
 var result = d.show();
 if(!result){
  d.destroy();
  return;
 }
 var grepForFind = RegExp(find.editContents,"g");
 var grepForReplace = change.editContents;
 d.destroy();
 var dests = app.documents[0].hyperlinkURLDestinations.everyItem().getElements();
 for(var i=0;i<dests.length;i++){
  dests[i].destinationURL = dests[i].destinationURL.replace(grepForFind,grepForReplace);
 }
}

Once both of these have been ran, I notice that the "http://" has been duplicated on the hyperlinks that already contain "http://".

So I run the second script again replacing (http:// + http://) with "http://" which solves the problem.

My question, is how to make it into a single script that would work the first time.

**Note:**The second script presents this error if the first is not run, which baffles me as well.

Any and all help would be appreciated.

K.Bass
  • 1
  • 1

2 Answers2

2

On the first script you get http:// duplicated because you are adding it to its own reference i.e. "http://"+"http://…". You have to replace string, not to add it:

var
  i;
hls = app.activeDocument.hyperlinkURLDestinations;
for (i = 0; i < hls.length; i++) {
  if (!hls[i].destinationURL.match('http://')) {
    hls[i].destinationURL = hls[i].destinationURL.replace(/^https/,"http");
  }
}

Another approach:

Hyperlink.prototype.grep = function(findString,repString, specifiers){
 var r, dests = this.destination, url, dest, n = dests.length;
 
 if ( !n 
 || !findString
 || !repString
 || typeof (findString) != "string"
 || typeof (repString) != "string"
 || ( specifiers && typeof ( specifiers )!="string" )
 ) return;
 
 r = new RegExp ( findString, specifiers? specifiers:"gi" );
 
 while (n-- ) {
  dest = dests[n];
  if ( dest instanceof HyperlinkURLDestination ) {
   url = dest.destinationURL;
   dest.destinationURL = url.replace ( r, repString );
  }
 }
}

main();
function main(){
 var d = app.dialogs.add({name:"Replace Hyperlink URL Values"});
 var col1 = d.dialogColumns.add();
 var col2 = d.dialogColumns.add();
 col1.staticTexts.add({staticLabel:"Find (GREP):"});
 col1.staticTexts.add({staticLabel:"Replace:"});
 var find = col2.textEditboxes.add({minWidth:100, editContents:"^https"});
 var change = col2.textEditboxes.add({minWidth:100, editContents:"http"});
 var result = d.show();
 if(!result){
  d.destroy();
  return;
 }

 var grepForFind = RegExp(find.editContents,"g");
 var grepForReplace = change.editContents;
 app.documents[0].hyperlinks.everyItem().grep(find.editContents, change.editContents, "g");
 d.destroy();
}
Loic
  • 2,173
  • 10
  • 13
0

Bass

I ran over all the configurations and to the exception of an empty url destination that indeed thrown an error, I can't reproduce what you are facing. Maybe give this new snippet a try ? If still failing, any chance you share the file ? Go at ozalto.com on the contact page if you prefer.

Hyperlink.prototype.grep = function(findString,repString, specifiers){
 var r, dests = this.destination, url, dest, n = dests.length;
 
 if ( !n 
 || !findString
 || !repString
 || typeof (findString) != "string"
 || typeof (repString) != "string"
 || ( specifiers && typeof ( specifiers )!="string" )
 ) return;
 
 r = new RegExp ( findString, specifiers? specifiers:"gi" );
 
 while (n-- ) {
  dest = dests[n];
  if ( dest instanceof HyperlinkURLDestination ) {
   url = dest.destinationURL;
   url!="" && dest.destinationURL = url.replace ( r, repString );
  }
 }
}

main();
function main(){
 var d = app.dialogs.add({name:"Replace Hyperlink URL Values"});
 var col1 = d.dialogColumns.add();
 var col2 = d.dialogColumns.add();
 col1.staticTexts.add({staticLabel:"Find (GREP):"});
 col1.staticTexts.add({staticLabel:"Replace:"});
 var find = col2.textEditboxes.add({minWidth:100, editContents:"^https"});
 var change = col2.textEditboxes.add({minWidth:100, editContents:"http"});
 var result = d.show();
 if(!result){
  d.destroy();
  return;
 }

 var grepForFind = RegExp(find.editContents,"g");
 var grepForReplace = change.editContents;
 app.documents[0].hyperlinks.everyItem().grep(find.editContents, change.editContents, "g");
 d.destroy();
}
Loic
  • 2,173
  • 10
  • 13
  • unfortunately, I cannot share any of our files, but all of them are very large, with pages from other reference files. This snippet throws the same error, and opens one of the reference files. – K.Bass Dec 04 '15 at 18:19