我正在尝试修改一个脚本(如下)以创建表列表,其中每个条目都充当pdf中的超链接,单击该链接时,您会将您带到相应表的页面。当前,以下脚本通过标识一种字符样式(“目录号”)起作用,该字符样式仅适用于每个条目[= paragraph]包含实际数字的那部分;该脚本将根据字符样式读取数字,然后将其转换为指向InDesign文件中该编号页面的超链接。
(请注意,InDesign中的实际页面是空白占位符。其中有数百个表是在Excel中创建的,并使用“替换页面...”作为插入的最后一步插入到Acrobat中。所有这些都是要说的:如果涉及InDesign的自动TOC功能,请保存您的建议;我们已经广泛使用了这些建议,但是对于涉及Excel表pdf的过程,这不是一个选择-我们想要修改下面的脚本,该脚本已经可以运行,并且(几乎)可以满足需求。)
为说明起见,这是脚本当前将其变成超链接的表列表中每个条目/段落的部分(红色圆圈):
这就是我们要做的:我们希望将页码之前的整个条目/段落(再次用红色圈出)制成可点击的超链接,该超链接引用相应的页码。
我想最简单的方法是更改脚本,使其定位页码,然后找到上一段的GREP,然后将所有到该段的内容变成链接的一部分?另外,我还为“表列表”条目设计了段落样式,以便每个部分(表格编号,标题,椭圆和页码)每个都有不同的字符样式,由制表符分隔。因此,这可能是在脚本中定义每个页码之前的哪些文本应成为链接一部分的另一种方式。
任何人都可以通过编辑此脚本来演示解决方案吗?它已经(几乎)起作用了!
/* Copyright 2016, Kasyan Servetsky
October 3, 2016
Written by Kasyan Servetsky
http://www.kasyan.ho.com.ua
e-mail: askoldich@yahoo.com */
//======================================================================================
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 = "TOC 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].insertionPoints[0].paragraphs[0].texts[0];
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, 0, 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();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
答案 0 :(得分:0)
下面是我自己制定的解决方案。我的问题是我试图对一个变量(sourceTextRef
)做太多事情。一旦定义了仅包含页码的第二个变量(tabPnum
)(与sourceTextRef
不同,我将其重新定义为保存找到每个页码的整个段落),并传递了新的MakeHyperlink
函数的参数,脚本可以正常工作:
//======================================================================================
var scriptName = "TOC links",
set, doc, swatchOK,
count = 0;
PreCheck();
//===================================== FUNCTIONS ======================================
function Main() {
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = "\\d+";
app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.item("publications").paragraphStyleGroups.item("TOC & Acknowledgements").paragraphStyles.item("TOC Table/Figure entry");
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 tabPnum = foundItems[f]; // foundItems[f] returns the page number .
var sourceTextRef = foundItems[f].words[0].lines[0].paragraphs[0]; // returns the full paragraph.
$.writeln(sourceTextRef.contents);
if (sourceTextRef.fillColor != swatchOK) {
MakeHyperlink(sourceTextRef, tabPnum); // added tabPnum here (necessary for passing parameter to function below).
}
}
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, tabPnum) { //added parameter tabPnum as argument here)
var source, destination, hyperlink,
pageNum = tabPnum.contents, // changed this line so that pageNum is defined by tabPnum.
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.CMYK, colorValue : [0, 0, 0, 100]});
}
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();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------