Indesign脚本:将(https)的所有情况替换为(http),不重复

Indesign Script: replacing all cases of (https) with (http) without duplication

本文关键字:http 替换 情况 脚本 https Indesign      更新时间:2023-09-26

我正在尝试修复我的indsign文件中的所有超链接,并将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。。。

查找/替换的菜单

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://",从而解决了问题。

我的问题是,如何使它成为一个单一的脚本,将第一次工作。

**注意:**如果第一个脚本没有运行,第二个脚本就会出现这个错误,这也让我感到困惑。

任何帮助都将不胜感激。

在第一个脚本中,您会得到http://duplicate,因为您将其添加到自己的引用中,即"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();
}

低音

我检查了所有的配置,除了一个空的url目的地确实抛出了一个错误之外,我无法重现您所面临的情况。也许试试这个新片段?如果仍然失败,您是否有机会共享该文件?如果您愿意,请访问ozalto.com的联系页面。

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;
			url!="" && 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();
}