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

Script to change URL in buttons?

Explorer ,
Jun 15, 2024 Jun 15, 2024

Copy link to clipboard

Copied

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/..."

 

TOPICS
Feature request , How to , Scripting

Views

495

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 , Jun 16, 2024 Jun 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];
    
...

Votes

Translate

Translate
Community Expert , Jun 17, 2024 Jun 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.con
...

Votes

Translate

Translate
Community Expert ,
Jun 16, 2024 Jun 16, 2024

Copy link to clipboard

Copied

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

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 ,
Jun 16, 2024 Jun 16, 2024

Copy link to clipboard

Copied

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

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 ,
Jun 16, 2024 Jun 16, 2024

Copy link to clipboard

Copied

Does this function only work on unanchored objects? I think so. I'm going to try some changes here so that it searches for unanchored objects too!

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 16, 2024 Jun 16, 2024

Copy link to clipboard

Copied

I see that wasn't part of the spec on the opening post 😄
Fancy giving a test document with your exact needs and see what we can do?

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 ,
Jun 16, 2024 Jun 16, 2024

Copy link to clipboard

Copied

The code worked for what I needed and I really appreciate your effort. In my document, some of these rectangles (shaped like buttons) are anchored to the end of the text link. In these anchored cases, the script is unable to make the change. As a solution, I am removing the anchoring of all objects before the change.

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 16, 2024 Jun 16, 2024

Copy link to clipboard

Copied

I just discovered that if you run the script a 2nd time it adds new again into the URL.

 

I need help here - can't figure out how to stop it doing that
@Peter Kahrel 

Any hints?

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 ,
Jun 16, 2024 Jun 16, 2024

Copy link to clipboard

Copied

It does make the change again if the root of the name is the same. To avoid this, I inserted the ".com" snippet. Therefore, avoid always changing the same root.
Example: changing "text" to "text2" it will always capture the same 'root' and will make the change twice.
But if I change the text "text.com" to "text2.com", it will no longer find the same root "text.com" and will not do the same action again.
I'm here thinking of a way for him to anchor the paintings that were anchored in the same place.

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 17, 2024 Jun 17, 2024

Copy link to clipboard

Copied

LATEST

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;
  }
}

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