Indesign脚本:将(https)的所有情况替换为(http),不重复
Indesign Script: replacing all cases of (https) with (http) without duplication
我正在尝试修复我的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();
}
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 我如何找到一个句子中的所有空格并替换忽略它们
- Meteor如何接收HTTP请求
- 如何用更合适的内容替换document.write
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 在DOM中查找一个模式并替换它's的内容使用jquery
- 如果img src以http://img.youtube.com/vi/并在任何字符后替换为youtube视频ifram
- 为什么将 ng-select 替换为自定义指令会导致$http请求不会每隔一段时间发送一次
- 替换 Base64 - http/https 通信是否 8 位干净
- 如何使用http.post方法替换或更新文本区域中的现有内容
- 在文档根服务器上的任何CSS、JS、HTML、PHP和TPL文件上,将http替换为相对url//
- Indesign脚本:将(https)的所有情况替换为(http),不重复
- 如何通过javascript将http替换为https
- URI Regex:如果URL有效,则用空字符串替换http://、https://、ftp://
- 搜索'www.'替换为'http://'使用regex javascript
- 将整个DOM(页面)替换为AngularJS中$http的响应
- 将JSON编码的feed value's image src协议http:替换为https:
- 通过$http将angular应用替换为另一个应用
- 用//替换href http://的脚本
- 如何在AngularJS中将$resource替换为$http