Skip to main content
Participating Frequently
April 15, 2011
Answered

Web Link Buttons with Variable URLs

  • April 15, 2011
  • 5 replies
  • 23870 views

Hey all:

I asked my question earlier in the general InDesign section of the forum, but a helpful user suggested I repost it here as it will take some scripting magic to accomplish:

My work assignment of late has been developing an interactive PDF catalog of our products that my superior can distribute digitally and let potential customers browse without accessing the website.

We have an Excel file that lists all the details of our products - name, description, title, image, category, etc. I have used Data Merge to create this 300 page document rather than hand-copy each product's information to a single page.

Unfortunately I am now stuck - I have designed the catalog in such a way that I have an image that I want to make a button which, if the person wishes to purchase a product, will take them directly the the product page online where he or she can add it to the shopping cart. This is great, but I cannot seem to find a way to make the image button's link a variable as the rest of the page content. Links to each product page are included in the CSV file, but without a variable web link method, I would have to create the unique links individually, which kinda defeats the purpose of using Data Merge anyway.

So my question is this - is there any way to make a URL button (an image) have a web link function that can read from a CSV file and change each page like other imported content? If not, is there another way to automate the individual link process without setting it up on each generated page?

Thanks in advance,

Asher

This topic has been closed for replies.
Correct answer John Hawkinson

So would the button version of the code you posted be this? And I'm assuming it'd go before the original part?

var j;

for (j=0; j<p.buttons.length; j++) {

    if (p.buttons.label === 'urlbutton') {

        t = p.buttons;

        break;

    }

}


If you were using the aformentioned code to check the script label, it would have to go inside the for (i) loop, replacing the "b = p.buttons[0]" line. Though there was a typo because I set t to the button and instead I should have set b to the button.

Yes, a group. If you know that there is only one group on the page, then you don't even have to loop over the groups. Instead you can just use:

t = p.groups[0].textFrames[0];

b = p.groups[0].buttons[0];

Of course, that'll break if there's ever more than one group.

5 replies

Participant
July 13, 2024

HI, were you able to solve for this?
Please guide me if you have. Thanks.

BobLevine
Community Expert
Community Expert
July 13, 2024

This discussion is more than 13 years old. Please start a new one.

I'm locking this to avoid any further confusion.

Participant
March 10, 2023

I would like to use this script and it seems as though I am in the same delima as Asher so I need to know where I can find this script please? I would like to be sure it's what I am looking for, can you help?

 

Community Expert
March 13, 2023

Hi @Toni Rae ,

read my reply above where I did repost the code.

And also read my annotations carefully.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Community Expert
April 20, 2021

I cannot edit John's post so here the fixed code:

var b, i, p, t,
	doc=app.activeDocument;
for (i=0; i<doc.pages.length; i++)
{
	p = doc.pages[i];
	b = p.groups[0].buttons[0];
	t = p.groups[0].textFrames[0];
	b.gotoURLBehaviors.add({ url: t.contents });
	t.remove();
};

The one thing that was dropped by moving the code over to the new forum was iterator [i] inside the for-loop. This is a common issue with old threads showing code. So before it was: p = doc.pages which makes no sense, because it addresses the collection of all document pages in a document and not a specific page.

 

Now I changed that to p = doc.pages[i] so that with every turn of the loop a different page is addressed starting with psges[0], the first page* and goes on adding 1 to that iterator i until condition i<doc.pages.length is not met anymore. pages.length is the total amount of document pages, a number, in the document that's behind variable doc. i++ is the short form of i = i+1 and dictates the steps the iterator will take with every loop.

 

*ExtendScript is starting to count with 0 and not with 1.

 

Some annotations:

The code above is not testing if there is a group on the page. And it also does not test if the group contains a button and a text frame. It simply assumes that. This could lead to errors on pages that do not meet that minimum requirement. Also note, that the text frame in that group will be removed after the behavior was added to the button. That would leave you with a group of 1 element, the button, if your initial group has only two elements, the button and the textframe.

 

Regards,
Uwe Laubender

( ACP )

Participant
August 2, 2021

Hi,

 

I'm actually a noob at scripting. Thanks to this thread I’m so close to an (almost) fully automated work process. Thanks everyone!

 

I almost got this working, but the script skips a lot of groups. When the script runs, it usually transforms just one group (button + textframe) into a URL button and gives me an error. It leaves all other groups untouched. Re-running the script won't help.

 

It's probably because my document has multiple pages without groups and some pages with 2 groups.

If I test this script with one group on each page, it works fine. But I need 2 URL buttons on 1 page (greedy, I know).

It also seems the script will stop working if it encounters a page without a group. All groups on pages after that will not turn into URL buttons.

 

As mentioned, the above code does not "test" whether there is a group (or page) that meets all requirements.

And this code is probably only useful for one group per page.

 

Is there a way to change the code to check for multiple groups on one page? And also that it only targets pages that meet the requirements.

 

((For now, I'll put "dummy groups" on each page, on a different layer. After I run the script, I just delete the layer. Since I need 2 URL buttons on 1 page, I slide the second URL button from another page to its final position. I have to do that for 100+ pages. It's not ideal, but it's better than setting 200+ URL buttons by copying and pasting manually. ))

 

Thanks in advance! 😊

Community Expert
April 19, 2021

JCD-5DC5 said at Mar 12, 2021:
"I've implemented everything you recommanded here, but unfortunately InDesign seems not to run the .jsx file properly. Here is the message I get while running the script : "Object does not support the property or method 'groups' ".

We are now in 2021, I am working with Indesign 16. Would this explains why I'm facing such an issue?"

 

No, that does not explain it. It's way more trivial. In late 2019 this thread was moved from the old forum to the new InDesign forum. And by that all that code was damaged. A bug with moving threads with code around. Happened to a lot of threads that were done before October 2019.

 

Could you please post the ExendScript code you tested?

I think I will be able to fix it.

 

Thanks,
Uwe Laubender

( ACP )

Participant
April 20, 2021

Hello Uwe,

Thank you for getting back to me. 

Actually, I implemented exactly the process carefuly detailed in this thread.

See bellow the JS Code used:

 

var b, i, p, t,
    doc=app.activeDocument;
for (i=0; i<doc.pages.length; i++) {
    p = doc.pages;
    b = p.groups[0].buttons[0];
    t = p.groups[0].textFrames[0];
    b.gotoURLBehaviors.add({ url: t.contents });
    t.remove()
}
 
As I'm a beginer in scripting, there is certainly a mistake I wasn't able to spot on.
Many thanks for your support and suggestions.
John Hawkinson
Inspiring
April 15, 2011

Hi, Asher.

You asked this question in the regular forum: Web Link Buttons with Variable URLs

and I gave you an initial answer in that forum:

var b, i, p, t,
    doc=app.activeDocument;
for (i=0; i<doc.pages.length; i++) {
    p = doc.pages;
    b = p.buttons[0];
    t = p.textFrames[0];
    b.gotoURLBehaviors.add({ url: t.contents });
}

How does it fail to meet your needs?

a.simondsAuthor
Participating Frequently
April 20, 2011

Sorry for the late response. I am not really versed in Javascript and don't know exactly how to implement it. I copied the code into an ExtendScript window and it hung at the for loop, with the statusbar at the bottom saying "undefined is not an object."

In the original thread you stated that these lines of code would work only if there was one textbox and one button on each page - there are at least 12 other buttons and 8 other textboxes/frames.

Thanks for your help - this is really the last hurdle for this project to be finished.

John Hawkinson
Inspiring
April 20, 2011

Did you tell the ESTK to run your script in InDesign, rather than the script editor itself?:

Well, you if you have more than one text box on the page, how is each button supposed to know which text box to use for a URL? You'll have to come up with some scheme.

You can't expect the script to just read your mind...