gpt4 book ai didi

javascript - Indesign脚本: replacing all cases of (https) with (http) without duplication

转载 作者:行者123 更新时间:2023-12-02 15:21:18 26 4
gpt4 key购买 nike

我正在尝试修复 indesign 文件中的所有超链接,并将 https 替换为 http。现在,为了让它工作,我运行这个脚本..

var
i;
hls = app.activeDocument.hyperlinkURLDestinations;
for (i = 0; i < hls.length; i++) {
if (!hls[i].destinationURL.match('http://')) {
hls[i].destinationURL = 'http://' + hls[i].destinationURL;
}
}

接下来是这个脚本,选择 https 替换为 http...

Menu for find/replace

main();
function main(){
var d = app.dialogs.add({name:"Replace Hyperlink URL Values"});
var col1 = d.dialogColumns.add();
var col2 = d.dialogColumns.add();
col1.staticTexts.add({staticLabel:"Find (GREP):"});
col1.staticTexts.add({staticLabel:"Replace:"});
var find = col2.textEditboxes.add({minWidth:100});
var change = col2.textEditboxes.add({minWidth:100});
var result = d.show();
if(!result){
d.destroy();
return;
}
var grepForFind = RegExp(find.editContents,"g");
var grepForReplace = change.editContents;
d.destroy();
var dests = app.documents[0].hyperlinkURLDestinations.everyItem().getElements();
for(var i=0;i<dests.length;i++){
dests[i].destinationURL = dests[i].destinationURL.replace(grepForFind,grepForReplace);
}
}

运行这两个命令后,我注意到“http://”已在已包含“http://”的超链接上重复。

因此,我再次运行第二个脚本,将 (http://+ http://) 替换为“http://”,这解决了问题。

我的问题是如何将其制作成一个可以第一次运行的脚本。

**注意:**第二个脚本显示 this error如果第一个没有运行,这也让我感到困惑。

任何和所有帮助将不胜感激。

最佳答案

在第一个脚本中,您会得到 http://重复,因为您将其添加到其自己的引用中,即“http://”+“http://…”。您必须替换字符串,而不是添加它:

var
i;
hls = app.activeDocument.hyperlinkURLDestinations;
for (i = 0; i < hls.length; i++) {
if (!hls[i].destinationURL.match('http://')) {
hls[i].destinationURL = hls[i].destinationURL.replace(/^https/,"http");
}
}

另一种方法:

Hyperlink.prototype.grep = function(findString,repString, specifiers){
var r, dests = this.destination, url, dest, n = dests.length;

if ( !n
|| !findString
|| !repString
|| typeof (findString) != "string"
|| typeof (repString) != "string"
|| ( specifiers && typeof ( specifiers )!="string" )
) return;

r = new RegExp ( findString, specifiers? specifiers:"gi" );

while (n-- ) {
dest = dests[n];
if ( dest instanceof HyperlinkURLDestination ) {
url = dest.destinationURL;
dest.destinationURL = url.replace ( r, repString );
}
}
}

main();
function main(){
var d = app.dialogs.add({name:"Replace Hyperlink URL Values"});
var col1 = d.dialogColumns.add();
var col2 = d.dialogColumns.add();
col1.staticTexts.add({staticLabel:"Find (GREP):"});
col1.staticTexts.add({staticLabel:"Replace:"});
var find = col2.textEditboxes.add({minWidth:100, editContents:"^https"});
var change = col2.textEditboxes.add({minWidth:100, editContents:"http"});
var result = d.show();
if(!result){
d.destroy();
return;
}

var grepForFind = RegExp(find.editContents,"g");
var grepForReplace = change.editContents;
app.documents[0].hyperlinks.everyItem().grep(find.editContents, change.editContents, "g");
d.destroy();
}

关于javascript - Indesign脚本: replacing all cases of (https) with (http) without duplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34033528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com