首页 文章

Applescript或Shell脚本从电子邮件和日历事件中提取文本和日期

提问于
浏览
0

我需要创建一个脚本,该脚本采用一致格式化的 Session 室预订请求 - 名称,日期,时间,持续时间,房间号 - (全部在不同的行上),并创建包含相同信息的日历事件 . 它将作为Apple Mail中的规则自动运行 .

我理解AppleScript和Bash脚本的基础知识,并且已经对这个问题进行了广泛的研究,但我很难过 . 我可以使用Automator快速操作提取日期,该操作采用所有文本,提取日期,然后将结果传递给此Applescript,但只在指定日期创建日历事件,而不是开始或结束时间,以及没有名称和房间号等变量 . 通过尝试设置并获取这些变量,我无法添加该信息:


on run {input, parameters}

    set dateString to the clipboard
    set start_date to date (dateString)
    set end_date to date (dateString) 

    tell application "Calendar"
        tell calendar "Local"
            make new event at end with properties {summary:"PersonName", start date:start_date, end date:end_date}
        end tell
    end tell 

    return input
end run

任何建议都将非常感激 . 谢谢 .


18年12月10日

主持人让我以精确的格式提供示例用例数据 . 我希望我的回答正确 . 该数据显示在电子邮件正文中:

Name: [person's name]
    Date: [reservation date]
    Room Number: [requested meeting room number]
    Start Time: [start time]
    End Time: [end time]

2 回答

  • 0

    您有两个问题需要解决:1)从电子邮件文本中提取/检查数据2)从数据中创建iCal中的新事件 .

    您已经完成了第二部分:从数据创建日历事件 .

    到目前为止,最困难的部分是从电子邮件正文中的非结构化自由文本中提取数据(甚至值得,因为它是富文本!) . 这是脚本下面的第一个子例程的作用 .

    它使用Applescript的文本项分隔符和“:”,并添加其他分隔符,因为电子邮件的内容不是文本,而是富文本(包括返回,换行,...) . 我假设带有“Name”的文本行必须后跟“:”和名称(一个或几个单词) . 房间和日期相同 . 对于开始/结束时间,该行必须包含“开始”/“结束”,在“:”之后,我假设时间设置为小时:分钟 .

    例如,身体波纹是有效的:

    Name: Stackoverflow 
    Date: 21/12/18 
    Start Time: 13:30 
    End Time: 15:45
    Room Number: Board Room
    

    注意:这些行可以按任何顺序排列 . 这里的房间号是最后一行 .

    在下面的脚本中,我假设使用的电子邮件是在Mail中选择的第一封电子邮件 . 由您决定调整该部分 .

    property myCalendar : "Local"
    
    tell application "Mail" -- extract the selected email and get its content as list of paragraphs
        set mySelection to selection
        set myMail to first item of mySelection
        set myLines to every paragraph of (content of myMail)
    end tell
    
    set myEvent to ExtractfromRichText(myLines) -- parse the paragraphs to make a record {EName, ERoom,EStart,EEnd}
    if EName of myEvent is not "" then CreateNewEvent(myCalendar, myEvent)
    -- end of main
    
    -- *******************
    on ExtractfromRichText(LocalLines) -- convert the rich text with fixed format into a data set
        set AppleScript's text item delimiters to {":", return & linefeed, return, linefeed} -- to remove all rich text end lines
        repeat with aLine in LocalLines
            try
                if text item 1 of aLine contains "Name" then set LName to text item 2 of aLine
                if text item 1 of aLine contains "Date" then set LDate to date (text item 2 of aLine)
                if text item 1 of aLine contains "Room" then set LRoom to text item 2 of aLine
                if text item 1 of aLine contains "Start" then
                    copy LDate to LStart
                    set hours of LStart to ((text item 2 of aLine) as integer)
                    set minutes of LStart to (word 1 of (text item 3 of aLine) as integer)
                end if
                if text item 1 of aLine contains "End" then
                    set LEnd to LDate
                    set hours of LEnd to ((text item 2 of aLine) as integer)
                    set minutes of LEnd to (word 1 of (text item 3 of aLine) as integer)
                end if
            on error -- unexpected format
                log "error"
                return {EName:""} -- not proper email format for calendar events. return empty name
            end try
        end repeat
        return {EName:LName, ERoom:LRoom, EStart:LStart, EEnd:LEnd}
    end ExtractfromRichText
    
    -- *******************
    
    on CreateNewEvent(LCalendar, LEvent)
        tell application "Calendar" to tell calendar LCalendar
        make new event at end with properties {summary:EName of LEvent, location:ERoom of LEvent, start date:EStart of LEvent, end date:EEnd of LEvent}
        end tell
    end CreateNewEvent
    
  • 0

    因为我对Applescript一无所知,所以我会给你一个完全不同的方法 . 考虑创建.ICS文件 . 然后,您可以通过发出命令“open <.ics file>”将事件添加到日历中 .

    我使用python创建了我的.ics文件:这是我的导入:

    from ics import Calendar, Event
    

    如果python不是你的东西,你可能会发现另一种创建除python以外的.ics文件的方法 . 例如,另一种方法可能包括用bash替换here文档中的变量 . 您可以从日历中导出的.ics文件生成模板 .

    您可能会发现.ics文件也更具可移植性 . .ics是许多日历系统(当然是Apple)理解的标准格式 .

    在我的简单项目中,我生成了一个.ics文件来保存从电子表格导入的网球事件 . 然后,我能够将.ics发送给所有参与者,以便他们可以将其加载到他们正在使用的任何日历中 .

相关问题