Home > Software design >  Get calendar event list by category in applescript
Get calendar event list by category in applescript

Time:01-26

enter image description hereI'm trying to get a list of all events in my calendar with a particular category, but cannot get the syntax

tell application "Microsoft Outlook"
    with timeout of 600 seconds
        set thisAccount to exchange account 1
        log thisAccount
        
        set theCategory to "Task" as string
        set theEventList to every calendar event whose category is theCategory
        display dialog "There were " & (count of theEventList) & " Tasks."
        --  set theEvent to item 1 of items of theConferenceList
    end timeout
end tell

I've tried various forms of the above, but end up with an error, the latest being

error "Microsoft Outlook got an error: Can’t make category into type specifier." number -1700 from category to specifier

Thanks in advance for direction!

CodePudding user response:

Thanks for clarifying that. I think there are two issues. First, 'Task' is the name of the category, so you would need to specify that you are working with the name:

tell application "Microsoft Outlook" to name of categories

You can also refer to categories by index or by id:

tell application "Microsoft Outlook" to properties of category 2
tell application "Microsoft Outlook" to properties of category id 3

Since you know the name, you can grab the id with:

tell application "Microsoft Outlook" to id of category named "Task"

You can see all of the properties of a category like this:

tell application "Microsoft Outlook" to properties of category id 6
--> {name:"Family", color:{41377, 17219, 52428}, show in navigation pane:true, id:6, class:category}

The second issue is that for each calendar event, categories are a list as you can assign multiple categories to any event.

set eventCatList to categories of item -3 of calendar events
--> {category id 4 of application "Microsoft Outlook", category id 6 of application "Microsoft Outlook"}

I'm not sure whether it's possible to do that kind of filtering (i.e. list of lists) so here is how you might get the list with a repeat loop.

tell application "Microsoft Outlook"
    with timeout of 6 seconds
        
        set tCat to category id 6 as list
        set eList to every calendar event
        
        set taskEventList to {}
        repeat with e in eList
            if category of e contains tCat then
                set end of taskEventList to contents of e
            end if
        end repeat
        
    end timeout
    count of taskEventList
    --> 2
    taskEventList
    --> {calendar event id 27 of application "Microsoft Outlook", calendar event id 29 of application "Microsoft Outlook"}
end tell

CodePudding user response:

I ended up having to do a shell script to get at the exact events with the categories of interest. Works!

tell application "Microsoft Outlook"
set theCategoryTask to first category whose name is "Task"
set theTaskComplete to first category whose name is "Complete"

set currentIdentityFolder to quoted form of POSIX path of (current identity folder as string)
set cmd to "mdfind -onlyin " & currentIdentityFolder & "  'kMDItemContentType == com.microsoft.outlook15.event && com_microsoft_outlook_categories == " & id of theCategoryTask & " && com_microsoft_outlook_categories != " & id of theTaskComplete & "' | xargs -I % mdls -name com_microsoft_outlook_recordID '%' | cut -d'=' -f2 | sort -u | paste -s -"

set theEventIDs to words of (do shell script cmd)

set theTaskList to {}
repeat with thisEventID in theEventIDs
    set end of theTaskList to calendar event id thisEventID
end repeat

set Taskcount to (count of theTaskList) as integer
set x to 1
repeat Taskcount times
    set TaskSubj to subject of (item x of theTaskList)
    set start time of item x of theTaskList to (current date) - (1 * days)
    set end time of item x of theTaskList to current date
    set x to (x   1)
end repeat

display dialog "All Done!"
end tell

Thanks @mockman for giving me some ideas!

  • Related