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

Script for applying hyperlinks to text

Participant ,
Jan 13, 2016 Jan 13, 2016

Hello,

I made a people directory document in InDesign using data merge. Each person has information about his/her social network URLs (LinkedIn, Twitter).

Is it possible to make a script which searches the InDesign document for all strings looking like this:

LinkedIn Profile: http://a-very-long-url

and transforms them into just LinkedIn Profile but with the URL applied as a hyperlink?

If it's easier for the find/replace operation I could mark the string with delimiters before running the script, to avoid complex URL detection scripting. Maybe something like this (Markdown style): [LinkedIn Profile](http://a-very-long-url/)

Thank you!

TOPICS
Scripting
2.8K
Translate
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

correct answers 1 Correct answer

Enthusiast , Jan 13, 2016 Jan 13, 2016

Hi,

Quick snippet, try  ...

var doc = app.activeDocument;

// get URL

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

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

var mURL  = doc.findGrep();

// get Texte

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

app.findGrepPreferences.findWhat = 'LinkedIn Profile:';

var mSource  = doc.findGrep();

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

    var mHyperlinkD

...
Translate
Enthusiast ,
Jan 13, 2016 Jan 13, 2016

Hi,

Quick snippet, try  ...

var doc = app.activeDocument;

// get URL

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

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

var mURL  = doc.findGrep();

// get Texte

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

app.findGrepPreferences.findWhat = 'LinkedIn Profile:';

var mSource  = doc.findGrep();

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

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

    var mHyperlinkTextSource = doc.hyperlinkTextSources.add(mSource);

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

    mHyperlink.name =mURL.contents;

    mHyperlink.visible=false;

}

//remove URL text

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

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

app.changeGrepPreferences.changeTo = ''; 

doc.changeGrep();

//rename 'LinkedIn Profile: ' to 'LinkedIn Profile'

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

app.findGrepPreferences.findWhat = 'LinkedIn Profile: ';

app.changeGrepPreferences.changeTo = 'LinkedIn Profile'; 

doc.changeGrep();

Translate
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
Participant ,
Jan 13, 2016 Jan 13, 2016

Thank you! Your script worked very well.

Translate
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
New Here ,
Aug 08, 2018 Aug 08, 2018

Thank you for providing Ronald. This was super useful. However, being unfamiliar with coding I'm having trouble tweaking the code so it will operate for my purposes. Instead of hyperlinking LinkedIn Profile: I need to hyperlink #1111 but the digits can be variables and there may also be five digits at times.

Sample below:

help.PNG

How would I go about doing this?

After the # is where I'm stuck:

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.contents); 

   var mHyperlinkTextSource = doc.hyperlinkTextSources.add(mSource); 

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

  mHyperlink.name =mURL.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(); 

Translate
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
Community Beginner ,
Dec 10, 2018 Dec 10, 2018

Greetings,

That script worked well.

How can I tweak it to do the same for sets of digits

Sample below:

#10476 https://thiswebsite...

#10536 https://thiswebsite...

#12784 https://thiswebsite...

Here is what I tried:

var doc = app.activeDocument; 

// get URL 

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

app.findGrepPreferences.findWhat = '(?i)(?<=\\d{5})(https?|www)\\S+\\>';  

var mURL  = doc.findGrep(); 

// get Texte 

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

app.findGrepPreferences.findWhat = '\\d{5}';  

var mSource  = doc.findGrep(); 

 

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

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

    var mHyperlinkTextSource = doc.hyperlinkTextSources.add(mSource); 

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

    mHyperlink.name =mURL.contents; 

    mHyperlink.visible=false; 

//remove URL text 

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

app.findGrepPreferences.findWhat = '(?i)(?<=\\d{5} )(https?|www)\\S+\\>';  

app.changeGrepPreferences.changeTo = '';   

doc.changeGrep();  

//rename '#\d\d\d\d\d ' to '#\d\d\d\d\d ' 

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

app.findGrepPreferences.findWhat = '\\d{5} ';  

app.changeGrepPreferences.changeTo = '\\d{5}';   

doc.changeGrep();

------------------------------------

What am I doing wrong?

It keeps saying the error is in LINE 12

...Please and thanks

Translate
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
Contributor ,
Dec 10, 2018 Dec 10, 2018

The reason might be a missing Space in the first GREP query (line 4)

original:

app.findGrepPreferences.findWhat = '(?i)(?<=\\d{5})(https?|www)\\S+\\>';


how it should be:

app.findGrepPreferences.findWhat = '(?i)(?<=\\d{5} )(https?|www)\\S+\\>'; 

Greetings

Translate
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
Community Beginner ,
Dec 10, 2018 Dec 10, 2018
LATEST

Thanks for your help,

It was not the space.

Note: The first script, at the top of the page, with the LINKEDIN: verbiage, worked perfect.

I would need to replace LINKEDIN with the numbers like: '#10429'

Again, thanks for you reply and help.

Translate
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