将Python链接到AppleScript for datetim

2024-09-30 22:28:46 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我的目标是能够在AppleScript中使用Python的开始和结束日期作为参数。。在

import commands

cmd = """osascript -e 'tell application "Calendar"
    set all_calendars to title of every calendar

    if "SimpleCal" is in all_calendars then
        set primary_calendar to "SimpleCal"
    else
        create calendar with name "SimpleCal"
        set primary_calendar to "SimpleCal"
    end if

    set start_date_mod to date %s
    set end_date_mod to date "Wednesday, August 14, 2013 at 8:10:00 PM"

    tell calendar primary_calendar
        set new_event to make new event at end with properties {description:"Imported with App", summary:"event_type", location:"", start date: start_date_mod, end date:end_date_mod}
        tell new_event
            make new display alarm at end with properties {trigger interval:-5}
        end tell
    end tell

end tell
'""" % ("Wednesday, August 14, 2013 at 8:00:00 PM")
status = commands.getoutput(cmd)
print status

Tags: toeventmodnewdatewithstartcalendar
1条回答
网友
1楼 · 发布于 2024-09-30 22:28:46

使用strftime将python日期转换为Applescript可以强制转换为日期对象的合适字符串:

import datetime

>>> AS_DATE_FORMAT = "%A, %B %d, %Y %I:%M:%S %p"
>>> right_now = datetime.datetime.now()
>>> date_string = right_now.strftime(AS_DATE_FORMAT)
'Wednesday, August 14, 2013 11:01:10 AM'

然后,在AppleScript部分,只需添加日期:

^{pr2}$

相关问题 更多 >