Indesign脚本:用(http)替换所有(https)的情况而不重复

时间:2015-12-02 01:43:29

标签: javascript adobe-indesign

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

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,这也让我感到困惑。

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

2 个答案:

答案 0 :(得分:2)

在第一个脚本上,你得到http://重复,因为你将它添加到它自己的引用中,即#34; http://&#34; +&#34; http:// ...&#34; 。你必须替换字符串,而不是添加它:

&#13;
&#13;
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");
  }
}
&#13;
&#13;
&#13;

另一种方法:

&#13;
&#13;
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();
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

低音

我跑过所有的配置,除了一个确实抛出错误的空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();
}