🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #392

a script to make all page references into hyperlinks

#392
From: Ola_Khrapatin
Date: Wed, 25 Mar 2009 15:42
68 lines
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;
}
Technical Details
Message-ID: <59b867c7.-1@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.glorb.com!news2.glorb.com!newshub.sdsu.edu!webcrossing

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,7425f2a7a39d73ea,start
X-Google-Attributes: gidf939a,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: Ola_Khrapatin@adobeforums.com
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.glorb.com!news2.glorb.com!newshub.sdsu.edu!webcrossing
Newsgroups: adobe.scripting.indesign
Subject: a script to make all page references into hyperlinks
Message-ID: <59b867c7.-1@webcrossing.la2eafNXanI>
Date: Wed, 25 Mar 2009 08:42:05 -0700
Lines: 88
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit