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

Create hypherlinks from table numbers to the page

Explorer ,
Mar 10, 2024 Mar 10, 2024

Copy link to clipboard

Copied

Hello, 

Here is another task for creative minds. 

I am working with many account tables and some of them have links with source informations. I attached sample table and it described what I need. I need  automate this process, because of many tables have this kind of links. 

"Find the numbers under the page column and link it to the page it shows.Ëœ PDF describe it more. 

 

TOPICS
Scripting

Views

879

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

correct answers 2 Correct answers

Community Expert , Mar 10, 2024 Mar 10, 2024

Good exercise for a Sunday afternoon:

 

(function () {

  var d = app.activeDocument;

  var columns = d.stories.everyItem().
    tables.everyItem().
    columns.everyItem().
    getElements();

  function addLinks (cells, p) {
    for (var i = 1; i < cells.length; i++) {
      if (d.pages.item(cells[i].contents).isValid) {
        d.hyperlinks.add (
          d.hyperlinkTextSources.add (cells[i].texts[0]),
          d.hyperlinkPageDestinations.add (d.pages.item(cells[i].contents))
        );
  
...

Votes

Translate

Translate
Community Expert , Mar 11, 2024 Mar 11, 2024

I see what you mean. Sorry, I misunderstood. The script tries to create only document-internal links, but the destination page can be in a different file. Here's the upgraded script.

 

Open all the documents, then run the script.

 

(function () {

  var docs = app.documents.everyItem().getElements();

  function findExternalPage (folio) {
    for (var i = 0; i < docs.length; i++) {
      if (docs[i].pages.item(folio).isValid) {
        return docs[i].pages.item(folio);
      }
    }
    return n
...

Votes

Translate

Translate
Community Expert ,
Mar 10, 2024 Mar 10, 2024

Copy link to clipboard

Copied

Good exercise for a Sunday afternoon:

 

(function () {

  var d = app.activeDocument;

  var columns = d.stories.everyItem().
    tables.everyItem().
    columns.everyItem().
    getElements();

  function addLinks (cells, p) {
    for (var i = 1; i < cells.length; i++) {
      if (d.pages.item(cells[i].contents).isValid) {
        d.hyperlinks.add (
          d.hyperlinkTextSources.add (cells[i].texts[0]),
          d.hyperlinkPageDestinations.add (d.pages.item(cells[i].contents))
        );
      }
    }
  }

  for (var i = 0; i < columns.length; i++) {
    if (columns[i].cells[0].contents === 'Page') {
      addLinks (columns[i].cells.everyItem().getElements());
    }
  }

}());

Votes

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
Explorer ,
Mar 10, 2024 Mar 10, 2024

Copy link to clipboard

Copied

Peter Kahrel, 

This is superb  !!!!! 

You saved lot of my time.... I realy appreaciate your creativity... This works fine.... 

oh.... how powerful this script....

Thank you very much.... _/|\_ 

Votes

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
Explorer ,
Mar 10, 2024 Mar 10, 2024

Copy link to clipboard

Copied

Peter Kahrel, 

I think to develop this script as using multiple files in a book folder. 

If I want to link that numbers to another ID file, but in a same book folder, how can I do that? 

If  so, I think this scrip more and more useful. 

Votes

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
Community Expert ,
Mar 11, 2024 Mar 11, 2024

Copy link to clipboard

Copied

You can use this script to run your script against a folder of files:

https://creativepro.com/files/kahrel/indesign/batch_convert.html

See the section 'Run a script' for more details.

Votes

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
Explorer ,
Mar 11, 2024 Mar 11, 2024

Copy link to clipboard

Copied

Peter Kahrel, 

I am confusing.... This batch process not support to me. 

Actually I need to make links between two ID files with same book folder. 

Example : "If my book folder has 3 ID files. The table with page numbers in third ID file. Some numbers want to link with first ID file. " Page numbers are fixed. 

Is that your batch process script can handle this? 

Votes

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
Community Expert ,
Mar 11, 2024 Mar 11, 2024

Copy link to clipboard

Copied

I see what you mean. Sorry, I misunderstood. The script tries to create only document-internal links, but the destination page can be in a different file. Here's the upgraded script.

 

Open all the documents, then run the script.

 

(function () {

  var docs = app.documents.everyItem().getElements();

  function findExternalPage (folio) {
    for (var i = 0; i < docs.length; i++) {
      if (docs[i].pages.item(folio).isValid) {
        return docs[i].pages.item(folio);
      }
    }
    return null;
  }

  function addLinks (d, cells) {
    var page;
    for (var i = 1; i < cells.length; i++) {
      page = d.pages.item(cells[i].contents);
      if (page.isValid) {
        d.hyperlinks.add (
          d.hyperlinkTextSources.add (cells[i].texts[0]),
          d.hyperlinkPageDestinations.add (page)
        );
      } else {
        page = findExternalPage (cells[i].contents);
        if (page) {
          d.hyperlinks.add (
            d.hyperlinkTextSources.add (cells[i].texts[0]),
            d.hyperlinkExternalPageDestinations.add (page)
          );
        }
      }
    }
  }

  function processDocument (d) {
		
    var columns = d.stories.everyItem().
      tables.everyItem().
      columns.everyItem().
      getElements();
    
    for (var i = 0; i < columns.length; i++) {
      if (columns[i].cells[0].contents === 'Page') {
        addLinks (d, columns[i].cells.everyItem().getElements());
      }
    }
  }

  for (var i = 0; i < docs.length; i++) {
    processDocument (docs[i]);
  }

}());

Votes

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
Explorer ,
Mar 11, 2024 Mar 11, 2024

Copy link to clipboard

Copied

Peter Kahrel, 

This fulfilled my requirement. Great script for keeping hours of time from my works. Thank you very much. 

In your script, you targeting page column in the table, right? Is it possible to target specifit table text style? If so no need to worry about column header. Script catch all the values in text tyles we mention. Is it possible? If so, can I edit this script?

Please let me know.

Votes

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
Community Expert ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

What do you mean by 'table text style'? A character style? Cell style? Paragraph style?

Votes

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
Explorer ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

Peter Kahrel, 

Sorry, I mean paragraph style. 

Because, sometimes tables has column with header named "Page". Sometimes it's "Notes" . Maybe in future it can be another name.  But we can keep same paragraph style everytime.  

Votes

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
Community Expert ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

Here you go. Another change will cost you a case of the best Rioja!

 

Insert the name of your style where it now says ''page-number reference', open all the documents, and run the script.

 

(function () {
  
  var styleName = 'page-number reference';

  var docs = app.documents.everyItem().getElements();
	
  //------------------------------------------------------

  function findExternalPage (folio) {
    for (var i = 0; i < docs.length; i++) {
      if (docs[i].pages.item(folio).isValid) {
        return docs[i].pages.item (folio);
      }
    }
    return null;
  }

  function addLinks (d, found) {
    var page = d.pages.item (found.contents);
    if (page.isValid) {
      d.hyperlinks.add (
        d.hyperlinkTextSources.add (found),
        d.hyperlinkPageDestinations.add (page)
      );
    } else {
      page = findExternalPage (found.contents);
      if (page) {
        d.hyperlinks.add (
          d.hyperlinkTextSources.add (found),
          d.hyperlinkExternalPageDestinations.add (page)
        );
      }
    }
  }

  function processDocument (d) {
    app.findGrepPreferences.appliedParagraphStyle = 
      d.paragraphStyles.item (styleName);
    var pp = d.findGrep();
    for (var i = pp.length-1; i >= 0; i--) {
      addLinks (d, pp[i]);
    }
  }

  //--------------------------------------------------
	
  app.findGrepPreferences = null;

  for (var i = 0; i < docs.length; i++) {
    processDocument (docs[i]);
  }

}());

 

Votes

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
Explorer ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

Peter Kahrel, 

Script run and links generated perfectly. But attached error comes up. 

Why is that?  Maybe my poor knowledge about scripts missed something???

Votes

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
Community Expert ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

That indicates that there is a document that lacks the paragraph style named at the top of the script. Check that all your documents in fact do have that style, and that that style is not in a style group.

Votes

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
Explorer ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

Peter Kahrel, 

Perfect !!!!  Perfect !!!!  Perfect !!!!

Everything works fine. Than you so much. 

I know this helpful to most of account works..... 

Thank you again........

Votes

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
Community Beginner ,
Jun 20, 2024 Jun 20, 2024

Copy link to clipboard

Copied

Can this script work for me, I have a yarli calendar and want to auto link with the daily page foot of that date, if it can be done, if you help me for this, I will thank you very much. I am uploading you my .indd file. I have done manual work in it. I want to auto like that. Can you help me for that, I will thank you very much from the bottom of my heart

Votes

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
Community Expert ,
Jun 20, 2024 Jun 20, 2024

Copy link to clipboard

Copied

You need an entirely different script, and I doubt that it's feasible with the state of your document. For instance, the rectangles that should be clickable are not marked up at all. So for any rectangle, how do you know which date it is? Furthermore, on the target pages the dates are in a group and in the [Basic Paragraph] style, so you won't be able unambiguously to find the correct pages.

Votes

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
Community Beginner ,
Jun 20, 2024 Jun 20, 2024

Copy link to clipboard

Copied

Thank you for your prompt response. I would greatly appreciate your assistance with this change as you suggested. Your help would mean a lot to me, as I have been spending many days on this task, and it has become quite tedious. Please help me if you can. Thank you very much.

Votes

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
Community Expert ,
Jun 20, 2024 Jun 20, 2024

Copy link to clipboard

Copied

LATEST

As I said, the way your document is now I don't think a script is feasible.

Votes

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