3

我们导入过程的一部分是从 Word 文档中删除超链接。我们现有的脚本非常简单、直接并且在某种程度上可以工作。

我们遇到的问题是,即使我们使用下面的代码删除链接,超链接符号仍会显示在故事编辑器中,这会阻止我们将来手动将链接添加到该文本块。

我还从 inDesign 手动添加了一个超链接,以显示两个超链接之间的区别,请参见下图。话虽如此,即使我在从 inDesign 添加链接之后运行脚本,结果也与上述相同。

剧本

var activeDocument = app.activeDocument;

trace("There are " + activeDocument.hyperlinks.length + " link(s) in the document.");

for(var i=(activeDocument.hyperlinks.length - 1); i >= 0; i--)
{
    trace("Removing hyperlink: " + activeDocument.hyperlinks.item(i).destination.name);
    activeDocument.hyperlinks.item(i).remove();
}

trace("There are " + activeDocument.hyperlinks.length + " link(s) in the document.");

故事编辑器和超链接面板

故事编辑器显示超链接面板中不存在的超链接

导入输出

There are 1 link(s) in the document.
Removing hyperlink: http://www.google.com
There are 0 links(s) in the document.
4

2 回答 2

2

Hyperlinks consist of several different parts, and you have to take care to remove every one of them. A single 'hyperlink' object is only sort of an abstract container object; it contains references to the hyperlinked item and to its destination.

Try this:

app.activeDocument.hyperlinkTextDestinations.everyItem().remove();
app.activeDocument.hyperlinkTextSources.everyItem().remove();
app.activeDocument.hyperlinks.everyItem().remove();
于 2013-07-09T11:21:45.073 回答
0

I came here with similar questions and was very grateful for usr2564301's answer, since it shed much light on the nature of hyperlinks in InDesign. But after some things in my own script didn't work as expected, I explored some more and discovered that the answer isn't completely accurate.

To test the relationships between hyperlink, hyperlinkTextSource and hyperlinkURLDestination, I wrote a script that removed just one of the three objects, and I tested it with both shared and non-shared hyperlinks. Here's what I found:

With non-shared hyperlink destinations:

Remove hyperlink:
Hyperlink removed?: YES
Source removed?: NO
Destination removed?: YES

Remove source:
Hyperlink removed?: YES
Source removed?: YES
Destination removed?: YES

Remove destination:
Hyperlink removed?: NO
Source removed?: NO
Destination removed?: YES

With shared hyperlink destinations:

Remove hyperlink:
Hyperlink removed?: YES
Source removed?: NO
Destination removed?: NO

Remove source:
Hyperlink removed?: YES
Source removed?: YES
Destination removed?: NO

Remove destination:
Hyperlink removed?: NO
Source removed?: NO
Destination removed?: YES

Removing a destination does exactly what you might expect—it just deletes the destination and leaves the hyperlink and source in place. It's like having an empty destination field in your hyperlink.

The result of removing the containing hyperlink is a little less intuitive, as you discovered. You might expect that it would delete everything, but it only deletes the destination (if it's non-shared).

The real surprise for me was that removing the source alone is enough to delete everything (for non-shared destinations). I suppose this sort of makes sense—a hyperlink without a source is kind of meaningless.

所以你去。在我的脚本中,我需要删除单个超链接,这种行为实际上简化了事情。删除该hyperlinkTextSource工作,而不必担心它是否是共享目标(可能被其他超链接使用)。

于 2018-09-10T08:25:51.110 回答