Thread View: adobe.scripting.indesign
2 messages
2 total messages
Started by Ola_Khrapatin
Wed, 25 Mar 2009 15:42
a script to make all page references into hyperlinks
Author: Ola_Khrapatin
Date: Wed, 25 Mar 2009 15:42
Date: Wed, 25 Mar 2009 15:42
68 lines
3534 bytes
3534 bytes
Hi,
I have a customer request to make all pages references into hyperlinks.
For example: when you see in the text of the PDF "page 4", you can click on it and move to page 4. This of course can be done manually with InDesign. The problem starts when you have whole load of them. In addition to that, I have this project as a book, with several different documents...
I used the script "Automatic hyperlink of each word in a file" from <http://www.nobrainer.dk/automatic-hyperlink-of-each-word-in-a-file/> as a base, and modified it for my porpuse. Currently it still produces errors. Any idea what's the bug?
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 destPage = item.slice(5); 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;
//Set other hyperlink properties here, if necessary.
return myHyperlinkDestination;
}
Re: a script to make all page references into hyperlinks
Author: 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;
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads