-- Get Tasks from Custom Perspective in OmniFocus using AppleScript -- This script provides true perspective access, unlike the OmniJS limitation -- Parameters will be injected by the Node.js execution layer -- Default values (will be replaced): -- perspectiveName: the name of the perspective to access -- hideCompleted: whether to hide completed tasks -- taskLimit: maximum number of tasks to return tell application "OmniFocus" activate -- Get the front document tell front document -- Get the front document window tell front document window try -- Switch to the specified perspective if perspectiveName is not "" then set perspective name to perspectiveName delay 0.5 -- Give time for the perspective to load end if -- Initialize result structure set taskList to {} set taskCount to 0 -- Get all tasks in the current perspective view set allTasks to every task of content -- Process each task repeat with currentTask in allTasks if taskCount >= taskLimit then exit repeat -- Check if we should include this task set shouldInclude to true if hideCompleted then set taskCompleted to completed of currentTask if taskCompleted then set shouldInclude to false end if end if if shouldInclude then -- Extract task information set taskName to name of currentTask as string set taskNote to note of currentTask as string set taskId to id of currentTask as string set taskCompleted to completed of currentTask set taskFlagged to flagged of currentTask -- Get dates (handle null values) try set taskDueDate to due date of currentTask if taskDueDate is not missing value then set taskDueDateString to (taskDueDate as string) else set taskDueDateString to "" end if on error set taskDueDateString to "" end try try set taskDeferDate to defer date of currentTask if taskDeferDate is not missing value then set taskDeferDateString to (taskDeferDate as string) else set taskDeferDateString to "" end if on error set taskDeferDateString to "" end try try set taskCompletionDate to completion date of currentTask if taskCompletionDate is not missing value then set taskCompletionDateString to (taskCompletionDate as string) else set taskCompletionDateString to "" end if on error set taskCompletionDateString to "" end try -- Get project information try set taskProject to containing project of currentTask if taskProject is not missing value then set projectName to name of taskProject as string set projectId to id of taskProject as string else set projectName to "" set projectId to "" end if on error set projectName to "" set projectId to "" end try -- Get estimated minutes try set estimatedMinutes to estimated minutes of currentTask if estimatedMinutes is missing value then set estimatedMinutes to 0 end if on error set estimatedMinutes to 0 end try -- Get tags (this might not work in all OmniFocus versions) set tagNames to {} try set taskTags to every tag of currentTask repeat with currentTag in taskTags set tagName to name of currentTag as string set end of tagNames to tagName end repeat on error -- Tags might not be available in this context end try -- Create task object (as a record for JSON conversion) set taskObject to {id:taskId, name:taskName, note:taskNote, completed:taskCompleted, flagged:taskFlagged, dueDate:taskDueDateString, deferDate:taskDeferDateString, completionDate:taskCompletionDateString, projectName:projectName, projectId:projectId, estimatedMinutes:estimatedMinutes, tags:tagNames} -- Add to task list set end of taskList to taskObject set taskCount to taskCount + 1 end if end repeat -- Create the result structure set resultRecord to {success:true, perspectiveName:perspectiveName, tasks:taskList, taskCount:taskCount, isRealPerspective:true} -- Convert to JSON-like string format -- Note: AppleScript doesn't have native JSON support, so we'll format it manually set jsonResult to "{" set jsonResult to jsonResult & "\"success\": true," set jsonResult to jsonResult & "\"perspectiveName\": \"" & perspectiveName & "\"," set jsonResult to jsonResult & "\"taskCount\": " & taskCount & "," set jsonResult to jsonResult & "\"isRealPerspective\": true," set jsonResult to jsonResult & "\"tasks\": [" set taskIndex to 1 repeat with currentTask in taskList set jsonResult to jsonResult & "{" set jsonResult to jsonResult & "\"id\": \"" & (id of currentTask) & "\"," set jsonResult to jsonResult & "\"name\": \"" & (my escapeJSON(name of currentTask)) & "\"," set jsonResult to jsonResult & "\"note\": \"" & (my escapeJSON(note of currentTask)) & "\"," set jsonResult to jsonResult & "\"completed\": " & (completed of currentTask) & "," set jsonResult to jsonResult & "\"flagged\": " & (flagged of currentTask) & "," set jsonResult to jsonResult & "\"dueDate\": \"" & (dueDate of currentTask) & "\"," set jsonResult to jsonResult & "\"deferDate\": \"" & (deferDate of currentTask) & "\"," set jsonResult to jsonResult & "\"completionDate\": \"" & (completionDate of currentTask) & "\"," set jsonResult to jsonResult & "\"projectName\": \"" & (my escapeJSON(projectName of currentTask)) & "\"," set jsonResult to jsonResult & "\"projectId\": \"" & (projectId of currentTask) & "\"," set jsonResult to jsonResult & "\"estimatedMinutes\": " & (estimatedMinutes of currentTask) set jsonResult to jsonResult & "}" if taskIndex < taskCount then set jsonResult to jsonResult & "," end if set taskIndex to taskIndex + 1 end repeat set jsonResult to jsonResult & "]" set jsonResult to jsonResult & "}" return jsonResult on error errorMessage -- Return error information set errorResult to "{\"success\": false, \"error\": \"" & (my escapeJSON(errorMessage)) & "\", \"perspectiveName\": \"" & perspectiveName & "\"}" return errorResult end try end tell end tell end tell -- Helper function to escape JSON strings on escapeJSON(inputString) set inputString to my replaceText(inputString, "\\", "\\\\") set inputString to my replaceText(inputString, "\"", "\\\"") set inputString to my replaceText(inputString, (ASCII character 13), "\\n") set inputString to my replaceText(inputString, (ASCII character 10), "\\n") set inputString to my replaceText(inputString, (ASCII character 9), "\\t") return inputString end escapeJSON -- Helper function to replace text on replaceText(originalText, searchString, replacementString) set AppleScript's text item delimiters to searchString set textItems to text items of originalText set AppleScript's text item delimiters to replacementString set newText to textItems as string set AppleScript's text item delimiters to "" return newText end replaceText