omnifocus-mcp-enhanced
Version:
🚀 NEW: Native Custom Perspective Access! Enhanced MCP server with OmniFocus custom perspective support, hierarchical task display, AI-optimized tool selection, and comprehensive task management
154 lines (136 loc) • 6.06 kB
JavaScript
// 透视模拟脚本 - 基于透视名称的智能任务筛选
(() => {
try {
// 参数会被自动注入
let perspectiveName = null;
let hideCompleted = true;
let limit = 100;
console.log(`开始模拟透视: ${perspectiveName}`);
const doc = Document.makeDefault();
const flattenedTasks = doc.flattenedTasks;
// Helper functions
function formatDate(date) {
if (!date) return null;
return date.toISOString();
}
function getTaskStatus(status) {
const taskStatusMap = {
[Task.Status.Available]: "Available",
[Task.Status.Blocked]: "Blocked",
[Task.Status.Completed]: "Completed",
[Task.Status.Dropped]: "Dropped",
[Task.Status.DueSoon]: "DueSoon",
[Task.Status.Next]: "Next",
[Task.Status.Overdue]: "Overdue"
};
return taskStatusMap[status] || "Unknown";
}
const tasks = [];
flattenedTasks.forEach(task => {
try {
const taskStatus = getTaskStatus(task.taskStatus);
// 基础筛选:跳过已完成/已放弃的任务(如果hideCompleted为true)
if (hideCompleted && (taskStatus === "Completed" || taskStatus === "Dropped")) {
return;
}
const taskData = {
id: task.id.primaryKey,
name: task.name,
note: task.note || "",
completed: taskStatus === "Completed",
dropped: taskStatus === "Dropped",
flagged: task.flagged,
dueDate: formatDate(task.dueDate),
deferDate: formatDate(task.deferDate),
completedDate: formatDate(task.completionDate),
estimatedMinutes: task.estimatedMinutes || 0,
projectName: task.containingProject ? task.containingProject.name : null,
tags: task.tags.map(tag => ({
id: tag.id.primaryKey,
name: tag.name
})),
containingProjectInfo: task.containingProject ? {
name: task.containingProject.name,
id: task.containingProject.id.primaryKey,
status: "active status"
} : null,
parentTaskInfo: null
};
// 基于透视名称的智能筛选
let shouldInclude = false;
if (perspectiveName) {
const lowerPerspectiveName = perspectiveName.toLowerCase();
// "2min" 或 "分钟" 透视:获取有时间估算的快速任务
if (lowerPerspectiveName.includes("2min") || lowerPerspectiveName.includes("分钟")) {
shouldInclude = (taskStatus === "Available" || taskStatus === "Next") &&
(task.estimatedMinutes <= 120 || task.estimatedMinutes === 0); // 2分钟或没设时间的
}
// "今日复盘" 透视:获取已完成的任务
else if (lowerPerspectiveName.includes("今日") && lowerPerspectiveName.includes("复盘")) {
shouldInclude = taskStatus === "Completed";
}
// "今日" 透视:获取今天到期或标记的任务
else if (lowerPerspectiveName.includes("今日") || lowerPerspectiveName.includes("today")) {
const today = new Date();
const dueToday = task.dueDate &&
new Date(task.dueDate).toDateString() === today.toDateString();
shouldInclude = dueToday || (task.flagged && taskStatus === "Available");
}
// "工作" 透视:获取工作相关任务
else if (lowerPerspectiveName.includes("工作") || lowerPerspectiveName.includes("work")) {
shouldInclude = (taskStatus === "Available" || taskStatus === "Next") &&
(task.estimatedMinutes > 0 ||
(task.containingProject && task.containingProject.name.includes("工作")));
}
// "重点"或"重要" 透视:获取标记的任务
else if (lowerPerspectiveName.includes("重点") || lowerPerspectiveName.includes("重要") ||
lowerPerspectiveName.includes("flagged")) {
shouldInclude = task.flagged && (taskStatus === "Available" || taskStatus === "Next");
}
// "旗标" 透视:获取标记的任务
else if (lowerPerspectiveName.includes("旗标")) {
shouldInclude = task.flagged && taskStatus !== "Completed" && taskStatus !== "Dropped";
}
// 默认:获取可用任务
else {
shouldInclude = taskStatus === "Available" || taskStatus === "Next";
}
} else {
// 无透视名称:获取可用任务
shouldInclude = taskStatus === "Available" || taskStatus === "Next";
}
if (shouldInclude) {
tasks.push(taskData);
}
// 应用限制
if (limit > 0 && tasks.length >= limit) {
return false; // 停止遍历
}
} catch (taskError) {
console.log(`处理任务时出错: ${taskError}`);
}
});
const result = {
success: true,
perspectiveInfo: {
name: perspectiveName || "默认筛选",
rulesCount: 1,
aggregation: "perspective_simulation"
},
tasks: tasks,
isSimulated: true,
limitation: "⚠️ OmniFocus透视API在OmniJS中不可用,这是已知的API限制",
note: "本结果基于透视名称的智能筛选,可能与真实透视不完全一致。如需精确筛选,请使用 filter_tasks 工具。"
};
console.log(`模拟透视 "${perspectiveName}" 返回 ${tasks.length} 个任务`);
return JSON.stringify(result);
} catch (error) {
console.error(`透视模拟脚本错误: ${error}`);
return JSON.stringify({
success: false,
error: `透视模拟失败: ${error}`,
limitation: "⚠️ OmniFocus透视API在OmniJS中不可用,这是已知的API限制",
suggestion: "请使用 filter_tasks 工具进行精确的任务筛选"
});
}
})();