Copy link to clipboard
Copied
Bonjour,
Existe-t-il un script pour InDesign permettant de transformer un nombre textuel (456) en un hyperlien renvoyant vers la page correspondante (456) d'un texte? De sorte que cet hyperlien soit effectif lorsqu'on exporte le texte au format .pdf.
Merci par avance.
Pascal
Copy link to clipboard
Copied
Try the following
app.findGrepPreferences = null
app.findGrepPreferences.findWhat = "\\d+"
var r = app.documents[0].findGrep()
app.findGrepPreferences = null
for(var i = 0; i < r.length; i++){
var pg = app.documents[0].pages.itemByName(r[i].contents)
if(pg.isValid){
if(r[i].findHyperlinks()[0])
continue;
var dest = app.documents[0].hyperlinkPageDestinations.add(pg)
var source = app.documents[0].hyperlinkTextSources.add(r[i], {appliedCharacterStyle:"Hyperlink"})
app.documents[0].hyperlinks.add(source, dest).name = r[i].contents
}
}
-Manan
Copy link to clipboard
Copied
Hi Pascal,
One alternative way I use to make hyperlinks is the script below.
Basically, it creates hyperlinks based on a character style ('Page Number', by default, but that can be modified).
The "only" thing you have to do is apply this specific character style to the numbers you want hyperlinks to be created. Then run the script and it will tell you how many links it has created (handy, especially when something went wrong).
I found that way best for me (us), so far, as we have more control over what to include/ exclude in the creation of hyperlinks. Formating the number like "see p. 123" is also helpful for a quick apply of character styles through a Find/change querry. Numbers in a table are alos easy to handle.
Hope it helps ^^
//======================================================================================
var scriptName = "Make hyperlinks",
set, doc, swatchOK,
count = 0;
PreCheck();
//===================================== FUNCTIONS ======================================
function Main() {
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = "\\d+";
app.findGrepPreferences.appliedCharacterStyle = "Page Number";
var foundItems = doc.findGrep(true);
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
if (foundItems.length == 0) ErrorExit("No page numbers were found in the current document.", true);
for (var f = 0; f < foundItems.length; f++) {
try {
var sourceTextRef = foundItems[f];
if (sourceTextRef.fillColor != swatchOK) {
MakeHyperlink(sourceTextRef);
}
}
catch(err) {
$.writeln(err.message + ", line: " + err.line);
}
}
var report = count + " hyperlink" + ((count == 1) ? " was" : "s were") + " created.";
alert("Finished. " + report, scriptName);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function MakeHyperlink(sourceTextRef) {
var source, destination, hyperlink,
pageNum = sourceTextRef.contents,
obj = GetPage(pageNum);
if (obj != null) {
source = doc.hyperlinkTextSources.add(sourceTextRef);
destination = obj.docDest.hyperlinkPageDestinations.add(obj.page);
var name = "Page_" + pageNum;
if (!doc.hyperlinks.itemByName(name).isValid) {
hyperlink = doc.hyperlinks.add(source, destination, {name: name});
}
else {
hyperlink = doc.hyperlinks.add(source, destination, {name: name + "_" + String(Math.random()).replace(/^0\./, "")});
}
if (hyperlink.isValid) {
count++;
sourceTextRef.fillColor = swatchOK;
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetPage(pageNum) {
var obj = null;
for (var i = 0; i < app.documents.length; i++) {
if (app.documents[i].pages.itemByName(pageNum).isValid) {
obj = {page: app.documents[i].pages.itemByName(pageNum), docDest: app.documents[i]};
break;
}
}
return obj;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function CheckSwatches() {
if (doc.swatches.itemByName("===== OK =====") == null) {
swatchOK = doc.colors.add({name : "===== OK =====", model : ColorModel.PROCESS, space : ColorSpace.RGB, colorValue : [0, 255, 0]});
}
else {
swatchOK = doc.swatches.itemByName("===== OK =====");
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function PreCheck() {
if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
doc = app.activeDocument;
if (!app.activeDocument.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
CheckSwatches();
Main();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
alert(error, scriptName, icon);
exit();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------