Advertisement
Guest User

Untitled

a guest
Oct 17th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use application "OmniFocus"
  2.  
  3. use scripting additions
  4.  
  5. tell application "OmniOutliner"
  6.    
  7.     if version5 then
  8.        
  9.         set template to ((path to application support from user domain as string) & "The Omni Group:OmniOutliner:Pro Templates:" & "Blank.otemplate:") --- template path for OO5
  10.        
  11.     else
  12.        
  13.         set template to ((path to application support from user domain as string) & "The Omni Group:OmniOutliner:Templates:" & "Blank.oo3template:") --- template for OO3 or OO4
  14.        
  15.     end if
  16.    
  17.     set currentDate to current date
  18.    
  19.     activate
  20.    
  21.     open template
  22.    
  23.     tell front document
  24.        
  25.         tell application "OmniFocus"
  26.            
  27.             if version < 3 then
  28.                
  29.                 error "You need to have OmniFocus version 3 or later. Early versions are not supported!"
  30.                
  31.             end if
  32.            
  33.         end tell
  34.        
  35.         (*
  36.        
  37.         ask user whether to display current projects or on-hold
  38.        
  39.         *)
  40.        
  41.         set userChoice to display dialog "Which projects to display?
  42. (will self-close in 5 seconds)" buttons {"Active", "On-Hold", "All"} default button 1 giving up after 5
  43.        
  44.         if gave up of userChoice is true then
  45.            
  46.             return
  47.            
  48.         end if
  49.        
  50.         (*
  51.        
  52.         create custom named styles
  53.        
  54.         *)
  55.        
  56.         delete named styles
  57.        
  58.         set _green to make named style with properties {name:"Green highlight"}
  59.        
  60.         set value of attribute "text-background-color" of _green to {r:0.470589, g:0.768627, b:0.270588, a:0.25}
  61.        
  62.         set _yellow to make named style with properties {name:"Yellow highlight"}
  63.        
  64.         set value of attribute "text-background-color" of _yellow to {r:1.0, g:0.774726, b:0.0, a:0.25}
  65.        
  66.         set _red to make named style with properties {name:"Red highlight"}
  67.        
  68.         set value of attribute "text-background-color" of _red to {r:0.968049, g:0.472297, b:0.539657, a:0.25}
  69.        
  70.         (*
  71.        
  72.         create columns in OmniOutliner
  73.        
  74.         *)
  75.        
  76.         set title of second column to "Project Name"
  77.        
  78.         make new column with properties {title:"Project Status", sort order:ascending}
  79.        
  80.         make new column with properties {title:"Project Due Date", column type:datetime}
  81.        
  82.         make new column with properties {title:"Root Folder"}
  83.        
  84.         (*
  85.        
  86.         iterate through projects which are (must match ALL conditions):
  87.        
  88.         - active
  89.        
  90.         - on hold
  91.        
  92.         - not dropped
  93.        
  94.         - not (effectively) dropped
  95.        
  96.         *)
  97.        
  98.         if button returned of userChoice is "Active" then
  99.            
  100.             set _projects to flattened projects of default document whose effective status is active status
  101.            
  102.         else if button returned of userChoice is "On-Hold" then
  103.            
  104.             set _projects to flattened projects of default document whose effective status is on hold status
  105.            
  106.         else if button returned of userChoice is "All" then
  107.            
  108.             set _projects to flattened projects of default document whose (effective status is active status or effective status is on hold status)
  109.            
  110.         else
  111.            
  112.             error "Reached primary loop but user choice is undetermined!"
  113.            
  114.         end if
  115.        
  116.         repeat with thisProject in _projects
  117.            
  118.             using terms from application "OmniFocus" --- workaround for "folder" term collision (future use)   
  119.                
  120.                 (*
  121.                
  122.                 find root folder of the project (start section)
  123.                
  124.                 *)
  125.                
  126.                 set rootFolder to folder of thisProject
  127.                
  128.                 set rootFolderName to missing value
  129.                
  130.                 if folder of thisProject is not missing value then
  131.                    
  132.                     set folderName to name of folder of thisProject
  133.                    
  134.                 else
  135.                    
  136.                     set folderName to missing value
  137.                    
  138.                 end if
  139.                
  140.                 if rootFolder is missing value then --- checking for root level projects
  141.                    
  142.                     set rootFolderName to "n/a"
  143.                    
  144.                     set foundRootFolderName to true
  145.                    
  146.                 else
  147.                    
  148.                     set foundRootFolderName to false
  149.                    
  150.                 end if
  151.                
  152.                 repeat until foundRootFolderName is true
  153.                    
  154.                     set upperFolder to container of rootFolder
  155.                    
  156.                     set upperFolderName to name of upperFolder
  157.                    
  158.                     if upperFolderName is equal to "OmniFocus" then
  159.                        
  160.                         set rootFolderName to name of rootFolder as string
  161.                        
  162.                         if folderName is not equal to rootFolderName then
  163.                            
  164.                             set rootFolderName to folderName & " (" & rootFolderName & ")" as string
  165.                            
  166.                         end if
  167.                        
  168.                         set foundRootFolderName to true
  169.                        
  170.                     else
  171.                        
  172.                         set rootFolder to upperFolder
  173.                        
  174.                     end if
  175.                    
  176.                 end repeat
  177.                
  178.             end using terms from
  179.            
  180.             (*
  181.            
  182.             create new row in OmniOutliner
  183.            
  184.             *)
  185.            
  186.             if text of second cell of first row is equal to "" then
  187.                
  188.                 set newRow to first row
  189.                
  190.             else
  191.                
  192.                 set newRow to make new row at end of (parent of last row)
  193.                
  194.             end if
  195.            
  196.             (*
  197.            
  198.             add OmniFocus project name and create deep link to it
  199.            
  200.             *)
  201.            
  202.             set text of second cell of newRow to name of thisProject
  203.            
  204.             set value of attribute named "link" of style of text of second cell of newRow to "omnifocus:///task/" & id of thisProject
  205.            
  206.             (*
  207.            
  208.             add OmniFocus project status
  209.            
  210.             *)
  211.            
  212.             set projectStatus to status of thisProject as string
  213.            
  214.             --- singleton projects are not given granularity as they simply hold a list of tasks (actions)
  215.            
  216.             if singleton action holder of thisProject as boolean is true then
  217.                
  218.                 set projectStatus to "action list"
  219.                
  220.             else
  221.                
  222.                 --- regular (active) projects (sequential or parallel) are given high level granurality
  223.                
  224.                 if thisProject's status is active status then
  225.                    
  226.                     --- internal variables used to determine project's status
  227.                    
  228.                     set cAvailTasks to 0
  229.                    
  230.                     set cAvailTasksTags to 0
  231.                    
  232.                     set cRemainTasks to 0
  233.                    
  234.                     set cRemainTasksTags to 0
  235.                    
  236.                     set cWaitingTasks to 0
  237.                    
  238.                     set cDeferredTasks to 0
  239.                    
  240.                     set bFirstAvailTaskIsWaiting to false
  241.                    
  242.                     --- iterate through every remaining task of the project
  243.                    
  244.                     repeat with thisTask in (flattened tasks of thisProject whose effectively completed is false and effectively dropped is false)
  245.                        
  246.                         set cRemainTasks to cRemainTasks + 1
  247.                        
  248.                         if thisTask's primary tag is not missing value then
  249.                            
  250.                             set cRemainTasksTags to cRemainTasksTags + 1
  251.                            
  252.                             if thisTask's primary tag's name contains "wait" then
  253.                                
  254.                                 set cWaitingTasks to cWaitingTasks + 1
  255.                                
  256.                             end if
  257.                            
  258.                         end if
  259.                        
  260.                         if thisTask's defer date ≤ currentDate and thisTask's blocked is false then
  261.                            
  262.                             set cAvailTasks to cAvailTasks + 1
  263.                            
  264.                             if thisTask's primary tag is not missing value then
  265.                                
  266.                                 set cAvailTasksTags to cAvailTasksTags + 1
  267.                                
  268.                             end if
  269.                            
  270.                         else
  271.                            
  272.                             set cDeferredTasks to cDeferredTasks + 1
  273.                            
  274.                         end if
  275.                        
  276.                     end repeat
  277.                    
  278.                     --- check if first available task is "wait-for" for sequential projects
  279.                    
  280.                     set tmpVar to missing value
  281.                    
  282.                     if thisProject is sequential and next task of thisProject is not missing value then
  283.                        
  284.                         if primary tag of next task of thisProject is not missing value then
  285.                            
  286.                             if name of primary tag of next task of thisProject is not missing value then
  287.                                
  288.                                 set tmpVar to name of primary tag of next task of thisProject
  289.                                
  290.                             end if
  291.                            
  292.                         end if
  293.                        
  294.                     end if
  295.                    
  296.                     if tmpVar contains "wait" then
  297.                        
  298.                         set bFirstAvailTaskIsWaiting to true
  299.                        
  300.                     end if
  301.                    
  302.                     --- project has no remaining tasks
  303.                    
  304.                     if cRemainTasks = 0 then
  305.                        
  306.                         set projectStatus to "stalled (no tasks)"
  307.                        
  308.                     end if
  309.                    
  310.                     --- project has remaining tasks with no tags
  311.                    
  312.                     if cRemainTasks > 0 and cRemainTasksTags = 0 then
  313.                        
  314.                         set projectStatus to "stalled (no tags)"
  315.                        
  316.                     end if
  317.                    
  318.                     --- all project's available tasks are "wait-for" context
  319.                    
  320.                     if cWaitingTasks > 0 and (cAvailTasksTags = cWaitingTasks or bFirstAvailTaskIsWaiting is true) then
  321.                        
  322.                         set projectStatus to "waiting"
  323.                        
  324.                     end if
  325.                    
  326.                     --- project has no available tasks but has remaining tasks
  327.                    
  328.                     if thisProject's defer date is missing value and cAvailTasksTags = 0 and cRemainTasksTags > 0 then
  329.                        
  330.                         set projectStatus to "deferred (tasks)"
  331.                        
  332.                     end if
  333.                    
  334.                     --- project is deferred by project's defer date
  335.                    
  336.                     if thisProject's defer date is not missing value and thisProject's defer date ≥ currentDate then
  337.                        
  338.                         set projectStatus to "deferred (project)"
  339.                        
  340.                     end if
  341.                    
  342.                     --- rename project's status from "active status" to "active"
  343.                    
  344.                     if projectStatus is "active status" then
  345.                        
  346.                         set projectStatus to "active"
  347.                        
  348.                     end if
  349.                    
  350.                     --- project is on hold
  351.                    
  352.                 else if thisProject's status is on hold status then
  353.                    
  354.                     set projectStatus to "on hold"
  355.                    
  356.                 end if
  357.                
  358.             end if
  359.            
  360.             set text of third cell of newRow to projectStatus
  361.            
  362.             (*
  363.            
  364.             add OmniFocus project due date
  365.            
  366.             *)
  367.            
  368.             if due date of thisProject is not equal to missing value then
  369.                
  370.                 set value of fourth cell of newRow to due date of thisProject as date
  371.                
  372.             end if
  373.            
  374.             (*
  375.            
  376.             add OmniFocus root folder name of the project
  377.            
  378.             *)
  379.            
  380.             set text of fifth cell of newRow to rootFolderName as string
  381.            
  382.         end repeat
  383.        
  384.     end tell
  385.    
  386.     set thePath to (choose folder) as string
  387.    
  388.     set opmlPath to thePath & "test.opml"
  389.    
  390.     set posixPath to (POSIX path of opmlPath)
  391.    
  392.     display dialog posixPath
  393.    
  394.     export front document to posixPath
  395.    
  396. end tell
  397.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement