UNPKG

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

180 lines (155 loc) 5.53 kB
// 正确的透视访问 - 使用验证过的OmniJS API (() => { try { // 参数会被自动注入 let perspectiveName = null; let hideCompleted = true; let limit = 0; // 0表示无限制 console.log(`使用正确方法获取透视: ${perspectiveName}`); // 使用正确的document全局变量 const doc = document; // Helper functions function formatDate(date) { if (!date) return null; return date.toISOString(); } function getTaskStatus(task) { if (task.completed) return "Completed"; if (task.dropped) return "Dropped"; return "Available"; } // 1. 使用正确的API查找透视 let targetPerspective = null; try { // 方法1: 直接通过名称查找自定义透视 targetPerspective = Perspective.Custom.byName(perspectiveName); console.log(`通过byName找到透视: ${perspectiveName}`); } catch (byNameError) { console.log(`byName失败: ${byNameError.message}`); // 方法2: 遍历所有自定义透视 try { const allCustomPerspectives = Perspective.Custom.all; for (let perspective of allCustomPerspectives) { if (perspective.name === perspectiveName) { targetPerspective = perspective; console.log(`通过遍历找到透视: ${perspectiveName}`); break; } } } catch (allError) { console.log(`遍历自定义透视失败: ${allError.message}`); } } // 如果在自定义透视中没找到,尝试内置透视 if (!targetPerspective) { try { const allBuiltInPerspectives = Perspective.BuiltIn.all; for (let perspective of allBuiltInPerspectives) { if (perspective.name === perspectiveName) { targetPerspective = perspective; console.log(`在内置透视中找到: ${perspectiveName}`); break; } } } catch (builtInError) { console.log(`内置透视查找失败: ${builtInError.message}`); } } if (!targetPerspective) { return JSON.stringify({ success: false, error: `透视 "${perspectiveName}" 未找到` }); } console.log(`成功找到透视: ${perspectiveName}`); // 2. 获取透视信息 let perspectiveInfo = { name: targetPerspective.name, rulesCount: 1, aggregation: "perspective_native" }; // 3. 切换到透视 - 使用正确的方法 let window = doc.windows[0]; if (!window) { console.log("没有窗口可用"); return JSON.stringify({ success: false, error: "没有可用窗口" }); } // 切换透视 window.perspective = targetPerspective; console.log(`已切换到透视: ${perspectiveName}`); // 4. 使用正确的方法收集任务 const tasks = []; function collectTasks(node) { try { const obj = node.object; if (obj && obj instanceof Task) { const taskStatus = getTaskStatus(obj); // 应用 hideCompleted 筛选 if (hideCompleted && (taskStatus === "Completed" || taskStatus === "Dropped")) { return; } const taskData = { id: obj.id.primaryKey, name: obj.name, note: obj.note || "", completed: taskStatus === "Completed", dropped: taskStatus === "Dropped", flagged: obj.flagged || false, dueDate: formatDate(obj.dueDate), deferDate: formatDate(obj.deferDate), completedDate: formatDate(obj.completionDate), estimatedMinutes: obj.estimatedMinutes || 0, projectName: obj.containingProject ? obj.containingProject.name : null, tags: obj.tags ? obj.tags.map(tag => ({ id: tag.id.primaryKey, name: tag.name })) : [], containingProjectInfo: obj.containingProject ? { name: obj.containingProject.name, id: obj.containingProject.id.primaryKey, status: "active status" } : null, parentTaskInfo: null }; tasks.push(taskData); // 应用限制 if (limit > 0 && tasks.length >= limit) { return false; // 停止收集 } } } catch (e) { console.log(`处理节点时出错: ${e.message}`); } // 递归处理子节点 if (node.children && node.children.length > 0) { for (let child of node.children) { if (collectTasks(child) === false) { return false; // 传播停止信号 } } } } // 从根节点开始收集 const rootNode = window.content.rootNode; collectTasks(rootNode); const result = { success: true, perspectiveInfo: perspectiveInfo, tasks: tasks, isReal: true, apiVersion: "OmniFocus 4.2+ 正确的Perspective API" }; console.log(`正确方法获取透视 "${perspectiveName}" 返回 ${tasks.length} 个任务`); return JSON.stringify(result); } catch (error) { console.error(`透视访问失败: ${error}`); return JSON.stringify({ success: false, error: `透视访问失败: ${error}`, apiVersion: "OmniFocus 4.2+ 正确的Perspective API" }); } })();