Skip to main content
Known Participant
June 15, 2024
Answered

Script to change URL in buttons?

  • June 15, 2024
  • 2 replies
  • 1560 views

Hello,

 

I would like to know if it is possible to change the URLs of "buttons/forms" boxes inserted in a document. I have a document with many links. Over these links, I create many rectangles were created and the URLs of the links behind them were applied to these rectangles. I would like to know if I can change these button urls via script (just add the excerpt to the links, which may be different)?

The links are different from each other, but the root is the same. Example: "test . com/..." to "testnew . com/..."

 

This topic has been closed for replies.
Correct answer Peter Kahrel

Buttons can have various parents. You'll have to collect the independent and embedded ones in separate commands and concatenate the arrays. To set each button's URL to its content you could use this script:

 

d = app.documents[0];

b = d.buttons.everyItem().getElements();
b = b.concat (d.stories.everyItem().buttons.everyItem().getElements());

for (i = 0; i < b.length; i++) {
  if (b[i].gotoURLBehaviors.length > 0) {
    b[i].gotoURLBehaviors[0].url = b[i].groups[0].textFrames[0].parentStory.contents;
  }
}

2 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
June 17, 2024

Buttons can have various parents. You'll have to collect the independent and embedded ones in separate commands and concatenate the arrays. To set each button's URL to its content you could use this script:

 

d = app.documents[0];

b = d.buttons.everyItem().getElements();
b = b.concat (d.stories.everyItem().buttons.everyItem().getElements());

for (i = 0; i < b.length; i++) {
  if (b[i].gotoURLBehaviors.length > 0) {
    b[i].gotoURLBehaviors[0].url = b[i].groups[0].textFrames[0].parentStory.contents;
  }
}
Community Expert
June 16, 2024

How's it going ! Not sure if this is what you want exactly

 

It will find any URL with test and replace with testnew

 

// Get the active document
var doc = app.activeDocument;

// Function to update URLs
function updateURL(url) {
    return url.replace("test", "testnew");
}

// Update URLs in buttons
for (var i = 0; i < doc.buttons.length; i++) {
    var button = doc.buttons[i];
    for (var j = 0; j < button.gotoURLBehaviors.length; j++) {
        var behavior = button.gotoURLBehaviors[j];
        if (behavior.hasOwnProperty("url")) {
            behavior.url = updateURL(behavior.url);
        }
    }
}

// Update URLs in hyperlinks
for (var i = 0; i < doc.hyperlinks.length; i++) {
    var hyperlink = doc.hyperlinks[i];
    if (hyperlink.destination instanceof HyperlinkURLDestination) {
        var destination = hyperlink.destination;
        if (destination.hasOwnProperty("destinationURL")) {
            destination.destinationURL = updateURL(destination.destinationURL);
        }
    }
}

// Notify the user
alert("URLs in buttons and hyperlinks have been updated.");
Inspiring
June 16, 2024

Thank you very much! It will be very useful to me. I tried many ways to do it, but I didn't succeed!