Skip to main content
Participating Frequently
January 24, 2025
Answered

Help writing a script to create a button that opens a linked file's path

  • January 24, 2025
  • 14 replies
  • 1139 views

Hi:

I'm wondering if anyone knows how to write a script (or another way to do it) that will create a button in which the URL is the link to that linked file's path. So, once I export an Interactive PDF, if I click on that button on the page where I'm looking at a linked file (PDF file, for instance), the button will take me to that file using the path of that linked file.

 

There is a Text Variable that already can display the linked file's path, but I can't figure out how to get that Path into the URL of a button.

 

Any ideas?

 

Thanks!

Ryan

Correct answer m1b

Hi @Ryan33910431r01h, here is a quick demo script that—if I understand you correctly—does what you want. Or at least it may give you an idea of how to do it. I tested the output and the button worked as expected in Acrobat—it opened the linked pdf file in my browser.

 

Let me know if this is on the right track.

- Mark

/**
 * @file Make Button For Linked File.js
 * 
 * Usage:
 *   1. Select a linked image (must have a valid URI).
 *   2. Run Script
 * 
 * Script will create a button over the top of the linked image.
 * The button, when exported as interactive PDF, will attempt
 * to open the linked file's URI.
 * 
 * @author m1b
 * @version 2025-01-30
 * @discussion https://community.adobe.com/t5/indesign-discussions/help-writing-a-script-to-create-a-button-that-opens-a-linked-file-s-path/m-p/15109743
 */
function main() {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (
        !item.hasOwnProperty('graphics')
        || 0 === item.graphics.length
    )
        return alert('Please select a linked image and try again.');

    var graphic = item.graphics[0],
        link = graphic.itemLink,
        URI = link.linkResourceURI;

    var button = item.parent.buttons.add({
        geometricBounds: item.geometricBounds,
        name: '"' + link.name + '" button',
    });

    button.gotoURLBehaviors.add({
        behaviorEvent: BehaviorEvents.MOUSE_UP,
        url: URI,
    });

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Make Linked File Button');

 

14 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
January 30, 2025

Hi @Ryan33910431r01h, here is a quick demo script that—if I understand you correctly—does what you want. Or at least it may give you an idea of how to do it. I tested the output and the button worked as expected in Acrobat—it opened the linked pdf file in my browser.

 

Let me know if this is on the right track.

- Mark

/**
 * @file Make Button For Linked File.js
 * 
 * Usage:
 *   1. Select a linked image (must have a valid URI).
 *   2. Run Script
 * 
 * Script will create a button over the top of the linked image.
 * The button, when exported as interactive PDF, will attempt
 * to open the linked file's URI.
 * 
 * @author m1b
 * @version 2025-01-30
 * @discussion https://community.adobe.com/t5/indesign-discussions/help-writing-a-script-to-create-a-button-that-opens-a-linked-file-s-path/m-p/15109743
 */
function main() {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (
        !item.hasOwnProperty('graphics')
        || 0 === item.graphics.length
    )
        return alert('Please select a linked image and try again.');

    var graphic = item.graphics[0],
        link = graphic.itemLink,
        URI = link.linkResourceURI;

    var button = item.parent.buttons.add({
        geometricBounds: item.geometricBounds,
        name: '"' + link.name + '" button',
    });

    button.gotoURLBehaviors.add({
        behaviorEvent: BehaviorEvents.MOUSE_UP,
        url: URI,
    });

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Make Linked File Button');

 

Participating Frequently
February 3, 2025

Hi Mark:

You are amazing! That works just as expected! One thing that I was trying to do was to get this link populated into a separate button, rather than make the button the image itself. Is that even possible?

 

Thanks!

Ryan

Robert at ID-Tasker
Legend
February 3, 2025

This should work: 

item.parent.buttons[0].gotoURLBehaviors[0].url = URI;

Instead of this: 

var button = item.parent.buttons.add({
        geometricBounds: item.geometricBounds,
        name: '"' + link.name + '" button',
    });

    button.gotoURLBehaviors.add({
        behaviorEvent: BehaviorEvents.MOUSE_UP,
        url: URI,
    });

 

John D Herzog
Inspiring
January 24, 2025

In the button palette you can set the frame you have selected to go to url. You then just put the link in the url text box.

Participating Frequently
January 26, 2025

Thanks! I know how to do it manually. But, I want to see if there's a way to dynamically put the URL from the linked file's Text Variable: Path object. So, if I replace the linked file with a different linked file, the URL would dynamically update to the new path. Any ideas?