0

Being unfamiliar with coding I'm having trouble tweaking the code below. I need to hyperlink the "#" and everything after the "#" until a "space". The digits after the "#" can be variables and there may be as many as five digits.

Sample below:

enter image description here

var doc = app.activeDocument; 

// get URL 

app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 

app.findGrepPreferences.findWhat = '(?i)(?<=# )(https?|www)\\S+\\>='; 

var mURL = doc.findGrep(); 

// get Texte 

app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 

app.findGrepPreferences.findWhat = '#'; 

var mSource = doc.findGrep(); 

 

for (var k = 0; k <mSource.length; k++){ 

   var mHyperlinkDestination = doc.hyperlinkURLDestinations.add(mURL[k].contents); 

   var mHyperlinkTextSource = doc.hyperlinkTextSources.add(mSource[k]); 

  mHyperlink = doc.hyperlinks.add(mHyperlinkTextSource,mHyperlinkDestination); 

  mHyperlink.name =mURL[k].contents; 

  mHyperlink.visible=false; 

} 

//remove URL text 

app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 

app.findGrepPreferences.findWhat = '(?i)(?<=# )(https?|www)\\S+\\>='; 

app.changeGrepPreferences.changeTo = ''; 

doc.changeGrep(); 
mathmaniac88
  • 630
  • 7
  • 22
  • 2
    Are you asking for help to modify that code to do output hyperlinks or are you asking for help to add hyperlinks to that code? In either case, there's no reason for it to be tagged with the UNIX tool `grep` so I'm removing that tag, – Ed Morton Aug 09 '18 at 00:28
  • what type of javascript is this – mathmaniac88 Aug 09 '18 at 00:38
  • I'm unable to test this code at the moment (no ID on this machine) so I will assume it works but only add the hyperlink to the `#`. If that is indeed the case, try changing `app.findGrepPreferences.findWhat = '#';` to `app.findGrepPreferences.findWhat = '#\\d{1,5}';` – cybernetic.nomad Aug 09 '18 at 14:28
  • Thanks for you're help, y'all. It was scripting for InDesign. Cybernetic.nomad solved my dilemma. – NoviceDave Sep 21 '18 at 23:05

1 Answers1

0

This should work:

var doc = app.activeDocument; 

// get URL 
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 
app.findGrepPreferences.findWhat = '\\s(https?|www)(://).+$'; 
var mURL = doc.findGrep(); 

// get Texte 
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 
app.findGrepPreferences.findWhat = '#\\d{1,5}'; 
var mSource = doc.findGrep(); 

for (var k = 0; k <mSource.length; k++){ 
  var mHyperlinkDestination = doc.hyperlinkURLDestinations.add(mURL[k].contents); 
  var mHyperlinkTextSource = doc.hyperlinkTextSources.add(mSource[k]); 
  mHyperlink = doc.hyperlinks.add(mHyperlinkTextSource,mHyperlinkDestination); 
  mHyperlink.name =mURL[k].contents + '_' + k; 
  mHyperlink.visible=false; 
} 

//remove URL text 
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 
app.findGrepPreferences.findWhat = '(?<= )(https?|www)(://).+$'; 
app.changeGrepPreferences.changeTo = ''; 
doc.changeGrep(); 

Overall it, was just some minor tweaks

1) Change the grep search for the URLs

2) Changed the grep search for the numbers ("#" followed by 1-5 digits)

3) Two hyperlinks cannot have the same name. I changed that line to also number them, you may want to tweak to suit your needs, one thing you could do is use mSource[k].contents + '_' + mURL[k].contents, though if you have repeats, you'll run into the same problem.

cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31