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/..."
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];
...
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
...
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.");
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!
Copy link to clipboard
Copied
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?
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.
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?
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.
Copy link to clipboard
Copied
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;
}
}