Copy link to clipboard
Copied
Hi Guys,
First time Poster here. I have been scratching my head over this for a few days now.
Im trying to create a script to add hyperlinks (from a text box) to a grouped object (Square with text on).
I can read thurl from the Text Box but cannot work out how to add this copied URL to the Group Box, I receive 'Object does not support this method...'
Any Help would be much appreciated.
Below is a snippet of what I have so far:
var document = app.activeDocument;
var selection = document.selection;
var myDocument = app.activeDocument;
var myFrames = document.allPageItems;
var myUrlLabel = 0;
var thisUrl;
//Cycle through ALL items and Pick out those with the URL and the BUTTON
for (var i=1; i< myFrames.length; i++) {
if (myFrames[i].label == 'url'){
myUrlLabel ++;
thisUrl = myFrames[i].contents;
thisItem = myFrames[i];
// alert (myFrames[i].constructor.name);
//alert( thisItem +" - "+ thisUrl);
//gotoURLBehaviors.add({ url: t.contents })
} // END IF LABEL URL
// FIND BUTTON to Add the url from the variable thisUrl ( from text box)
if (myFrames[i].label == 'BTN'){
var group = myFrames[i];
var mySelectedFrame = myFrames[i];
alert( myFrames +" BTN - "+thisUrl);
addHyperlink(thisUrl, mySelectedFrame)
}
}
function addHyperlink(thisUrl, mySelectedFrame){
mySelectedFrame.hyperlinks.add( thisUrl , thisUrl);
}
I'm a bit rusty with hyperlinks, but they can be tricky. Hyperlinks are added through the document object, and you need specific objects for them to point to. So, you should pass myDocument to your addHyperlink function then try something like this:
var hypeSource = myDocument.hyperlinkPageItemSources.add(mySelectedFrame);
var hypeUrl = myDocument.hyperlinkURLDesitnations.add(thisUrl);
myDocument.hyperlinks.add(hypeSource, hypeUrl);
(Side note: I'd avoid naming variables "this"anything, as "this"
...Copy link to clipboard
Copied
Yeah I will try a couple of things I have in mind.
Your ggrouping of the TextFrames might be the right way to go about it.
Will give it a try tomorrow and let you know.
Thanks again
Copy link to clipboard
Copied
No they are stand alone groups and to answer your previous question the Text Frames are not part of any group.
Even if I try to add the link to a none grouped object ( such as a rectangle) I still get the same error.
This is all I have on my page.
Copy link to clipboard
Copied
Hi Adriano,
how can you be sure that no hyperlink was added?
Does app.documents[0].hyperlinks.length return 0 ?
Hm. Did you also check the document's hyperlinkPageItemSources array?
Maybe you already added an item to this array without knowing?
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Uwe,
Yes app.documents[0].hyperlinks.length == 0;
hyperlinkPageItemSources returns obabject but no urls in the panel.
Still getting "The object you have chosen is already in use by another hyperlink"
Copy link to clipboard
Copied
Hi Adriano,
I think the error message is misleading. Or it could be misleading.
Loop the hyperlinkPageItemSources array and check:
if( app.documents[0].hyperlinkPageItemSources[n].sourcePageItem == itemInMyGroup )
If yes, you should be able to remove that specific hyperlinkPageItemSource with method remove().
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Uwe,
I cant seem to get your suggetion working :-PI've included it at the top of the below script>
Any chance you can take a look at it for me?
Much appreciated
var groups = app.activeDocument.groups;
var document = app.activeDocument;
var selection = document.selection;
var page = document.pages;
var myFrames = document.allPageItems;
var myItems = document.pageItems;
for (var n = 0; n < myFrames.length; n++) {
if( app.documents[0].hyperlinkPageItemSources[n].sourcePageItem == myFrames ){
myFrames.hyperlinkPageItemSources[n].remove;
}else {
myFrames.hyperlinkPageItemSources[n].remove;
}
}
for (var i = 0; i < groups.length; i++) {
$.writeln("---- GROUPS ---------------------" + groups[i]);
for (var j = 0; j < groups[i].pageItems.length; j++) {
if (groups[i].label == "btn") {
$.writeln("---- FOUND BUTTON GROUP ---------------------");
var thisUrl = "http://mysite.net.au";
}
}
addHyperlink (thisUrl, groups[i], i);
}
function addHyperlink(thisUrl, mySelectedFrame, i){
$.writeln("---- INSIDE FUNCTION ---------------------");
$.writeln("Hyperlinks Length: " + document.hyperlinks.length );
var selectedItem= document.selection[0];
$.writeln("SELECTED DONE" + groups[i]);
thisUrl = "http://mysite.net.au";
urlDestString = "http://mysite.net.au";
var selectedItem= app.selection[0];
var selectedItem= mySelectedFrame;
var hypeSource = document.hyperlinkPageItemSources.add(selectedItem);
var hypeUrl = document.hyperlinkURLDesitnations.add(urlDestString);
document.hyperlinks.add(hypeSource, hypeUrl);
}
Copy link to clipboard
Copied
Hi Adriano,
sorry, no time to look into your code this week.
Also not before Wednesday next week. I'm swamped in work.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Thats Ok,
we figured most of it out so no problem ( by we I mean mosty brianp311 lol )
Copy link to clipboard
Copied
So here is the Final Code I used - incase anyone is interested.
The script supposes that there is a textFrame with a label of 'url'
and A group with a with if 'btn'.
When a url is found it copies it and then looks for the first btn Group and applies then copied url, renames both url and btn and moves on to find the next set.
var document = app.activeDocument;
var document = app.activeDocument;
var myFrames = document.allPageItems;
var groups = app.activeDocument.groups;
var urlDestString;
//FIND URL IN TEXT FRAME AND COPY READY TO PASS ON TO GROUPED OBJECT
for (var i=0; i< myFrames.length; i++) {
if ( myFrames[i].label == "url" ) {
urlDestString ='';
urlDestString = myFrames[i].contents;
myItem = myFrames[i];
myFrames[i].label = "url Copied" ;
checkBTN (urlDestString, i, myItem, myFrames);
}
}
//FIND GROUPED OBJECT TO ADD URL TO
function checkBTN (urlDestString, i, myItem, myFrames){
for (var i = 0; i < groups.length; i++) {
if (groups[i].label == "btn") {
var thisUrl = urlDestString;
groups[i].label = "btn DOne"+i;
addHyperlink (thisUrl, groups[i], app.activeDocument);
break;
}
}
}
//ADD URL
function addHyperlink(urlDestString, mySelectedFrame, document){
try {
var hypeSource = document.hyperlinkPageItemSources.add(mySelectedFrame);
} catch(e) {
$.writeln( e);
return;
}
var hypeUrl = document.hyperlinkURLDestinations.add(urlDestString);
document.hyperlinks.add(hypeSource, hypeUrl);
}
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more