Skip to main content
Participant
January 30, 2024
Answered

Hyperlink Script for Part #s

  • January 30, 2024
  • 1 reply
  • 349 views

I have flyers and catalogs that have Part#s that I need to hyperlink so when clicked it takes you to that product on our website. I have searched in the community and found tons of scripts but none of them do what I need and I tried to change them to do what I want but it didn't work. Does anyone know of a script that already exists that I can edit to work for me.

 

Part #s aren't all the same number of characters and there is a "-" in the part#. Any help would be appreciated. Thank You.

This topic has been closed for replies.
Correct answer Manan Joshi

Try the following modification of your code. You need to have a text selection for it to work. It does not check anything on what text you have selected, it just appends that text to the base URL you have and creates a hyperlink. I tested it with a part no 11886-02500320 and it worked.

var obj = app.selection[0];
var part = obj.contents;
var ItemText = obj.texts[0];
var url = "https://www.nextind.com/itemdetail/" + part;

addHyperURL(app.documents[0], ItemText, url, url, url, url);

function addHyperURL(myDoc, pageItemSource, url, hyperName, destiName, sourceName) {

    var source = myDoc.hyperlinkTextSources.add(pageItemSource);

    if (sourceName == undefined) { var sourceName = "sourceName"; }
    if (destiName == undefined) { var destiName = "destiName"; }

    var destination = myDoc.hyperlinkURLDestinations.add();
    with (destination) {
        try { name = destiName; } catch (e) { destiName = destiName + "_" + objID; name = destiName; }
        destinationURL = url;
    }
    if (hyperName == undefined) { var hyperName = "HyperLink" + sourceName + "_" + destiName; }
    try { var hyperLink = myDoc.hyperlinks.add(source, destination, { visible: false }); } catch (e) { return; }
    try {
        hyperLink.name = hyperName;
    } catch (e) {
        hyperLink.name = hyperName + "_" + hyperLink;
    }
    //{highlight: hyperArr[2],width: hyperArr[3], borderStyle: hyperArr[4], borderColor: hyperArr[5]});
    return hyperLink;
}

-Manan

1 reply

Community Expert
January 31, 2024

Hi @Kelly28445812ys36,

Can you share a document or screenshot to explain it better. The first question is how would the script identify that lets say Part-1 is to be hyperlinked to which url?

-Manan

-Manan
Participant
January 31, 2024

Ok. Sorry. First time posting and I should have given more info. I am using InDesign 2024 and am on a Windows 11 Pro.

 

Last night I was able to find an old script from a previous job that added the hyperlink when I would highlight the part# and run the script, but I can't get it to work properly and I am not familiar enough with scripts to understand the language so that if I change it, it will still work. Here is the script I found. It asks for a document to be selected when I run it but I want that removed because it's not neccessary for the script I don't think and I can't do that without breaking it. Any help would be greatly appreciated. Thank You.

 

var myDoc = app.documents[0];

var obj = app.selection[0];

var part = obj.contents;

part = removenonalphanums(part); (Does this mean it's removing the part #? If so, I don't want it to do that)

var ItemText = obj.texts[0];

var url = "https://www.nextind.com/itemdetail/" + part; (this is the URL that would be the beginning and then after the "/" would be the part# in the chart)

var currentdoc = app.activeDocument.name;

 

addHyperURL(myDoc, ItemText, url, url, url, url);

function addHyperURL(myDoc, pageItemSource, url, hyperName, destiName, sourceName){

 

var source = myDoc.hyperlinkTextSources.add(pageItemSource); (When I run the script it is asking for a doc on my desktop but there isn't one and I don't know why we would have had this to begin with so I would like for the script to not ask for a doc to be opened when I run the script and I don't know how to remove this without breaking it.)

 

if(sourceName == undefined){var sourceName = "sourceName";}

if(destiName == undefined){var destiName = "destiName";}

 

var destination = myDoc.hyperlinkURLDestinations.add();

with(destination){

try{name = destiName;}catch(e){destiName = destiName+ "_" + objID; name = destiName;}

destinationURL = url;

}

if(hyperName == undefined){var hyperName = "HyperLink" + sourceName +"_"+ destiName;}

try{var hyperLink = myDoc.hyperlinks.add(source, destination, {visible: false});}catch(e){return;}

try{

hyperLink.name = hyperName;

}catch(e){

hyperLink.name = hyperName + "_" + hyperLink;

}

//{highlight: hyperArr[2],width: hyperArr[3], borderStyle: hyperArr[4], borderColor: hyperArr[5]});

return hyperLink;

}

function removenonalphanums(part) {

var prtlength, lastchar;

do

{

prtlength = part.length;

lastchar = part.substring(prtlength-1);

if (lastchar < "-")

part = part.substring(0,prtlength-1);

else if ((lastchar > "9") && (lastchar < "A"))

part = part.substring(0,prtlength-1);

else if (lastchar > "Z") part =

part.substring(0,prtlength-1); }

while (prtlength != part.length)

return part;

}

Manan JoshiCommunity ExpertCorrect answer
Community Expert
January 31, 2024

Try the following modification of your code. You need to have a text selection for it to work. It does not check anything on what text you have selected, it just appends that text to the base URL you have and creates a hyperlink. I tested it with a part no 11886-02500320 and it worked.

var obj = app.selection[0];
var part = obj.contents;
var ItemText = obj.texts[0];
var url = "https://www.nextind.com/itemdetail/" + part;

addHyperURL(app.documents[0], ItemText, url, url, url, url);

function addHyperURL(myDoc, pageItemSource, url, hyperName, destiName, sourceName) {

    var source = myDoc.hyperlinkTextSources.add(pageItemSource);

    if (sourceName == undefined) { var sourceName = "sourceName"; }
    if (destiName == undefined) { var destiName = "destiName"; }

    var destination = myDoc.hyperlinkURLDestinations.add();
    with (destination) {
        try { name = destiName; } catch (e) { destiName = destiName + "_" + objID; name = destiName; }
        destinationURL = url;
    }
    if (hyperName == undefined) { var hyperName = "HyperLink" + sourceName + "_" + destiName; }
    try { var hyperLink = myDoc.hyperlinks.add(source, destination, { visible: false }); } catch (e) { return; }
    try {
        hyperLink.name = hyperName;
    } catch (e) {
        hyperLink.name = hyperName + "_" + hyperLink;
    }
    //{highlight: hyperArr[2],width: hyperArr[3], borderStyle: hyperArr[4], borderColor: hyperArr[5]});
    return hyperLink;
}

-Manan

-Manan