🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #8657

Re: Hyperlinking Issues with InDesign Script

#8657
From: Katie_Eccleston
Date: Thu, 28 Sep 2006 15:18
49 lines
6694 bytes
Thanks so much! The hyperlink code works like a charm. I appreciate it. Here is the code that I have been using....as I said it only runs once then I receive an error. I need it to be able to be run over and over again and identify new text that needs to be hyperlinked. Any ideas would be much appreciated!

Katie

main();
function main(){
	if(app.documents.length != 0){
		if(app.documents.item(0).stories.length != 0){
			mySearchStories();
		}
		else{
			alert("The current document does not contain any text.");
		}
	}
	else{
		alert("Please open a document and try again.");
	}
}
function mySearchStories(){
	var myStory, myStoryText, myFoundItems, myFoundStrings, myHyperlinkDestination, myFootnoteCounter;
	var myStart, myEnd, myFootnote, myStringCounter, myFoundItem;
	var myHyperlinkTextSource, myTextCounter, myStringCounter;
	var myFoundStrings = new Array;
	var myDocument = app.documents.item(0);
	//Construct regular expression to find hyperlink sources.
	var myRegExp = /https?:\/\/([-\w]+\.)+\w+\b/g;;
	//String to hold hyperlink URL destination prefix:
	var myURLPrefix = "mailto:";
	for(var myCounter = 0; myCounter < myDocument.stories.length; myCounter ++){ 		//Get the text of the story as a string. 		myStory = myDocument.stories.item(myCounter); 		if(myStory.characters.length > 1){
			myStoryText = myStory.contents;
			//If the story contains only a special character (such as an auto page number marker),
			//the following will fail, so use try...catch to avoid the problem.
			try{
				if(myStoryText.match(myRegExp)!=null){
					myFoundStrings = myFoundStrings.concat(myStoryText.match(myRegExp));
				}
			}
			catch (myError){}
/*
			//Can't do it this way due to a CS2 bug.
			if(myStory.footnotes.length > 0){
				for(myFootnoteCounter = 0; myFootnoteCounter < myStory.footnotes.length; myFootnoteCounter++){ 					myStoryText = myStory.footnotes.item(myFootnoteCounter).texts.item(0).contents; 					//If the story contains only a special character (such as an auto page number marker), 					//the following will fail, so use try...catch to avoid the problem. 					try{ 						if(myStoryText.match(myRegExp)!=null){ 							myFoundStrings = myFoundStrings.concat(myStoryText.match(myRegExp)); 						} 					} 					catch (myError){} 				} 			}
* / 		}
	}
	if(myFoundStrings.length > 0){
		myFoundStrings = myRemoveDuplicates(myFoundStrings);
		for(myStringCounter = 0; myStringCounter < myFoundStrings.length; myStringCounter++){ 			myClearFindChange(); 			myFoundItems = myDocument.search(myFoundStrings[myStringCounter]); 			if(myFoundItems.length != 0){ 				myHyperlinkDestination = myMakeURLHyperlink(myURLPrefix + myFoundStrings[myStringCounter]); 				for(myTextCounter = 0; myTextCounter < myFoundItems.length; myTextCounter++){ 					myFoundItem = myFoundItems[myTextCounter]; 					try{ 						myHyperlinkTextSource = myDocument.hyperlinkTextSources.add(myFoundItems[myTextCounter]); 						myHyperlink = myDocument.hyperlinks.add(myHyperlinkTextSource, myHyperlinkDestination); 						myHyperlink.name = myURLPrefix + myFoundItems[myTextCounter].texts.item(0).contents; 					} 					catch (myError){ 						myHyperlinkDestination.remove(); 					} 				} 			} 			myClearFindChange(); 		} 	} 	myFoundString = new Array; 	//Handle the footnotes here. 	//Iterate through the stories again. 	for(var myCounter = 0; myCounter < myDocument.stories.length; myCounter ++){ 		myStory = myDocument.stories.item(myCounter); 		if(myStory.footnotes.length > 0){
			for(myFootnoteCounter = 0; myFootnoteCounter < myStory.footnotes.length; myFootnoteCounter++){ 				myFootnote = myStory.footnotes.item(myFootnoteCounter); 				myStoryText = myFootnote.texts.item(0).contents; 				try{ 					if(myStoryText.match(myRegExp)!=null){ 						myFoundStrings = myStoryText.match(myRegExp); 						//Found at least one matching string in the footnote, so use 						//itemByRange to construct a text object, then use that object to 						//create a hyperlink text source. 						for(myStringCounter = 0; myStringCounter < myFoundStrings.length; myStringCounter++){ 							myFoundString = myFoundStrings[myStringCounter]; 							myStart = myStoryText.indexOf(myFoundString); 							myEnd = myStart + myFoundString.length-1; 							myFoundItem = myFootnote.texts.itemByRange(myFootnote.characters.item(myStart), myFootnote.characters.item(myEnd)); 							myHyperlinkDestination = myMakeURLHyperlink(myURLPrefix + myFoundString); 							try{ 								myHyperlinkTextSource = myDocument.hyperlinkTextSources.add(myFoundItem); 								myHyperlink = myDocument.hyperlinks.add(myHyperlinkTextSource, myHyperlinkDestination); 								myHyperlink.name = myURLPrefix + myFoundString; 							} 							catch (myError){ 								myHyperlinkDestination.remove(); 							} 						} 					} 				} 				catch (myError){} 			} 		} 	} } function myMakeURLHyperlink(myURL){ 	var myDocument = app.documents.item(0); 	//If the hyperlink destination already exists, use it; 	//if it doesn't, then create it. 	try{ 		var myHyperlinkDestination = myDocument.hyperlinkURLDestinations.item(myURL); 		myHyperlinkDestination.name; 	} 	catch(myError){ 		myHyperlinkDestination = myDocument.hyperlinkURLDestinations.add(myURL); 	} 	myHyperlinkDestination.name = myURL; 	//Set other hyperlink properties here, if necessary. 	return myHyperlinkDestination;	 } function myRemoveDuplicates(myArray){ 	//Semi-clever method of removing duplicate array items; much faster 	//than comparing every item to every other item! 	var myNewArray = new Array; 	myArray = myArray.sort(); 	myNewArray.push(myArray[0]); 	if(myArray.length > 1){
		for(var myCounter = 1; myCounter < myArray.length; myCounter ++){ 			if(myArray[myCounter] != myNewArray[myNewArray.length -1]){ 				myNewArray.push(myArray[myCounter]); 			} 		} 	} 	return myNewArray; } function myClearFindChange(){ 	app.findPreferences = NothingEnum.nothing; 	app.findPreferences = NothingEnum.nothing;	 }
Technical Details
Message-ID: <3bc1cd8b.1@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeed.freenet.de!news.albasani.net!news.isoc.lu!webcrossing
References: <3bc1cd8b.-1@webcrossing.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,d5d2496634d5a1bb
X-Google-Attributes: gidf939a,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Katie_Eccleston@adobeforums.com
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeed.freenet.de!news.albasani.net!news.isoc.lu!webcrossing
Newsgroups: adobe.scripting.indesign
Subject: Re: Hyperlinking Issues with InDesign Script
Message-ID: <3bc1cd8b.1@webcrossing.la2eafNXanI>
Date: Thu, 28 Sep 2006 08:18:34 -0700
References: <3bc1cd8b.-1@webcrossing.la2eafNXanI>
Lines: 180
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit