Article View: adobe.scripting.indesign
Article #301Re: a script to make all page references into hyperlinks
From: Ola_Khrapatin
Date: Wed, 25 Mar 2009 17:51
Date: Wed, 25 Mar 2009 17:51
70 lines
3269 bytes
3269 bytes
well, after some more digging into it, I found the bugs and corrected, here is the corrected version (now I only need to figure out how to do it on an entire book...):
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var myHyperlinkStyle = myDocument.characterStyles.item("hyperlink"); // replace hyperlink with name of your char style for links
//alert(myHyperlinkStyle);
main();
} else {
alert("Please open a document");
}
function main() {
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//the value to find
myFindVal = "page (\\d+)";
doSearchAndReplace(myFindVal, app.activeDocument);
// reset search
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
}
function doSearchAndReplace(stringfind, searchin) {
app.findGrepPreferences.findWhat = stringfind;
//Set the find options.
app.findChangeGrepOptions.includeFootnotes = true;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;
var myFoundItems = searchin.findGrep();
for (i = 0; i < myFoundItems.length; i++) { var item = myFoundItems[i].contents; //the destination page var destPageNumber = item.slice(5);
var destPage = searchin.pages.itemByName(destPageNumber);
var myHyperlinkDestination = myMakeHyperlinkDestination(destPage);
myMakeHyperlink(myFoundItems[i], myHyperlinkDestination);
myFoundItems[i].applyCharacterStyle(myHyperlinkStyle, false);
}
}
function myMakeHyperlink(myFoundItem, myHyperlinkDestination){
try {
var myHyperlinkTextSource = myDocument.hyperlinkTextSources.add(myFoundItem);
var myHyperlink = myDocument.hyperlinks.add(myHyperlinkTextSource, myHyperlinkDestination);
myHyperlink.visible = false;
}
catch(myError){
}
}
function myMakeHyperlinkDestination(myDestPage){
//If the hyperlink destination already exists, use it;
//if it doesn't, then create it.
try{
var myHyperlinkDestination = myDocument.hyperlinkDestinations.item(myDestPage);
myHyperlinkDestination.name;
}
catch(myError){
myHyperlinkDestination = myDocument.hyperlinkPageDestinations.add(myDestPage);
myHyperlinkDestination.destinationPage = myDestPage;
}
//myHyperlinkDestination.name = myDestPage.name;
//Set other hyperlink properties here, if necessary.
return myHyperlinkDestination;
}
Technical Details
Message-ID:
<59b867c7.0@webcrossing.la2eafNXanI>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!newshub.sdsu.edu!webcrossingReferences:
<59b867c7.-1@webcrossing.la2eafNXanI>Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,7425f2a7a39d73ea X-Google-Attributes: gidf939a,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: Ola_Khrapatin@adobeforums.com Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!newshub.sdsu.edu!webcrossing Newsgroups: adobe.scripting.indesign Subject: Re: a script to make all page references into hyperlinks Message-ID: <59b867c7.0@webcrossing.la2eafNXanI> Date: Wed, 25 Mar 2009 10:51:35 -0700 References: <59b867c7.-1@webcrossing.la2eafNXanI> Lines: 80 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit