Article View: adobe.scripting.indesign
Article #9633Re: Script in InDesign CS2 - Can I convert VB to VBScript or JavaScript?
From: Olav_Kvern
Date: Tue, 15 Aug 2006 17:20
Date: Tue, 15 Aug 2006 17:20
100 lines
4500 bytes
4500 bytes
Hi Katie,
Try something like this:
//MakeHyperlinks.jsx
//An InDesign CS2 JavaScript
//
//Finds a string in text and creates a hyperlink based on the
//contents of the string. This script employs a hybrid approach to
//finding and changing text, using both JavaScript regular expressions
//and InDesign's search method.
//
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;
var myHyperlinkTextSource, myTextCounter, myStringCounter;
var myFoundStrings = new Array;
var myDocument = app.documents.item(0);
//Construct regular expression to find hyperlink sources.
var myRegExp = /ABC(-|\/)[\d]*-[\d]*/g;
//String to hold hyperlink URL destination prefix:
var myURLPrefix = "http:\/\/www.abc.com\/";
var myURLSuffix = ".html"
for(var myCounter = 0; myCounter < myDocument.stories.length; myCounter ++){
//Get the text of the story as a string.
myStory = myDocument.stories.item(myCounter);
myStoryText = myStory.contents;
myFoundStrings = myFoundStrings.concat(myStoryText.match(myRegExp));
}
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] + myURLSuffix);
for(myTextCounter = 0; myTextCounter < myFoundItems.length; myTextCounter++){
myHyperlinkTextSource = myDocument.hyperlinkTextSources.add(myFoundItems[myTextCounter]);
myHyperlink = myDocument.hyperlinks.add(myHyperlinkTextSource, myHyperlinkDestination);
}
}
myClearFindChange();
}
}
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;
}
Copy this script to the ExtendScript Toolkit (or any text editor) and save it as a text file with the file extension .jsx. Put the file in the Scripts folder inside the Presets folder in your InDesign folder, and it'll appear in the Scripts palette. To run the script, double-click the script name in the Scripts palette.
Obviously, you shouldn't try this on a "live" document--test it on a few example documents and see how it works for you. In addition, you might want to change the URL prefix, URL suffix, and/or the regular expression.
Thanks,
Ole
Technical Details
Message-ID:
<3bc1426b.3@webcrossing.la2eafNXanI>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!news.ecp.fr!news.stben.net!webcrossingReferences:
<3bc1426b.-1@webcrossing.la2eafNXanI>Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,4856d139041796fa,start X-Google-Attributes: gidf939a,public X-Google-Language: ENGLISH,ASCII-7-bit From: Olav_Kvern@adobeforums.com Newsgroups: adobe.scripting.indesign Subject: Re: Script in InDesign CS2 - Can I convert VB to VBScript or JavaScript? Message-ID: <3bc1426b.3@webcrossing.la2eafNXanI> Date: Tue, 15 Aug 2006 10:20:45 -0700 References: <3bc1426b.-1@webcrossing.la2eafNXanI> Lines: 113 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!news.ecp.fr!news.stben.net!webcrossing