每次在脚本编辑器中运行此脚本时都会收到"错误:AppleEvent 处理程序失败"



一直在努力让这个脚本工作。它的目的是批量导出笔记从苹果笔记。脚本如下:


// set things up
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var notesApp = Application('Notes');
notesApp.includeStandardAdditions = true;
// choose which notes
var notes = notesApp.notes;
var whichNotes = app.chooseFromList(notes.name(), { withPrompt: "Which Notes?", multipleSelectionsAllowed: true });

if (whichNotes) {
// choose save location
var saveWhere = app.chooseFolder().toString();

if (saveWhere) {

// loop through all notes
for(var i=0; i<notes.length; i++) {

// is this note one to be exported?
if (whichNotes.indexOf(notes[i].name()) > -1) {

// save file as html
var filename = saveWhere+"/"+notes[i].name()+".html";
var file = app.openForAccess(Path(filename), { writePermission: true });
app.setEof(file, { to: 0 });
app.write(notes[i].body(), {to: file});
app.closeAccess(file);
}
}
}
}

很多人都用过,没有问题。

我在10.15.7上使用相同的脚本有相同的问题。这个问题是在notes.name()上提出的。

我认为这与太多的笔记(它曾经工作,但我创建了很多笔记)或笔记标题中的一些特殊字符有关。但我没能用笔记把它修好。

我在下面复制了我的版本。(注意,如果注释标题包含"/",则替换为构建有效的文件名)

// set things up
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var notesApp = Application('Notes');
notesApp.includeStandardAdditions = true;

// choose which notes
var notes = notesApp.notes;
this.console.log("before notes.name()")
var whichNotes = app.chooseFromList(notes.name(), { withPrompt: "Which Notes?", multipleSelectionsAllowed: true });
this.console.log("After notes.name()")
this.console.log("Let's do it") // view / show log / message tab
if (whichNotes) {
// choose save location
var saveWhere = app.chooseFolder().toString();

if (saveWhere) {

this.console.log("note count:"+notes.length) 
// loop through all notes
for(var i=0; i<notes.length; i++) {

// is this note one to be exported?
if (whichNotes.indexOf(notes[i].name()) > -1) {

// save file as html

var notename = notes[i].name().replace(///gi,'-')
this.console.log("next:"+notename) // view / show log / message tab
var filename = saveWhere+"/"+ notename +".html";
var file = app.openForAccess(Path(filename), { writePermission: true });
app.setEof(file, { to: 0 });
app.write(notes[i].body(), {to: file});
app.closeAccess(file);
}
}
}
}

最新更新