如何用FileMaker Pro脚本将FM记录添加至macOS日历?报错求助
FileMaker Pro与macOS日历双向同步解决方案
一、修复FM到日历同步的报错(错误-1728)
当前报错的核心问题是参数传递逻辑完全错误:原AppleScript里直接写"& $Title &"是把字符串字面量当成变量,没有实际获取FileMaker的变量值;同时混淆了字典和列表的用法,导致无法通过item索引取值。
修正后的FileMaker脚本
需要把变量打包成列表,作为脚本参数传给AppleScript:
Set variable [ $Title; Value:"Customer" & Log::X & Customers::CustomerID letters ] Set variable [ $StartDate; Value:Log::Date of project start duration ] Set variable [ $EndDate; Value:Log::Project end date or end of duration ] Set variable [ $Location; Value:Log::Location ] Set variable [ $Description; Value:Log::Description ] -- 将变量打包为列表,传递给AppleScript Set variable [ $ScriptParams; Value:List($Title; $StartDate; $EndDate; $Location; $Description) ] Run AppleScript [ Native AppleScript: Get(ScriptParameter) ]
修正后的AppleScript
直接接收FileMaker传递的参数列表,提取后创建日历事件:
-- 获取FileMaker传来的参数列表 set fmScriptParams to the result -- 从列表中提取各参数 set eventTitle to item 1 of fmScriptParams set startDate to item 2 of fmScriptParams set endDate to item 3 of fmScriptParams set eventLocation to item 4 of fmScriptParams set eventDescription to item 5 of fmScriptParams -- (可选)调试用弹窗,确认参数是否正确 -- display dialog "Event Title: " & eventTitle & return & ¬ -- "Start Date: " & startDate & return & ¬ -- "End Date: " & endDate & return & ¬ -- "Location: " & eventLocation & return & ¬ -- "Description: " & eventDescription -- 找到目标iCloud日历并创建事件 set targetCalendarName to "FM" tell application "Calendar" set targetCalendar to calendar targetCalendarName set newEvent to make new event at targetCalendar with properties ¬ {summary:eventTitle, start date:startDate, end date:endDate, location:eventLocation, description:eventDescription} activate end tell
二、实现双向同步
要做到双向同步,需要给FM记录添加一个CalendarEventID字段,用来存储日历事件的唯一ID,作为两边关联的标识。
1. FM记录修改时同步更新日历事件
改进后的FileMaker脚本支持新建事件和更新已有事件:
# 检查当前记录是否已有对应的日历事件ID If [ IsEmpty(Log::CalendarEventID) ] # 新建事件流程 Set variable [ $Title; Value:"Customer" & Log::X & Customers::CustomerID letters ] Set variable [ $StartDate; Value:Log::Date of project start duration ] Set variable [ $EndDate; Value:Log::Project end date or end of duration ] Set variable [ $Location; Value:Log::Location ] Set variable [ $Description; Value:Log::Description ] Set variable [ $ScriptParams; Value:List($Title; $StartDate; $EndDate; $Location; $Description) ] Run AppleScript [ Native AppleScript: Get(ScriptParameter) ] # 将AppleScript返回的事件ID存入FM字段 Set field [ Log::CalendarEventID; Get(ScriptResult) ] Else # 更新已有事件流程 Set variable [ $EventID; Value:Log::CalendarEventID ] Set variable [ $Title; Value:"Customer" & Log::X & Customers::CustomerID letters ] Set variable [ $StartDate; Value:Log::Date of project start duration ] Set variable [ $EndDate; Value:Log::Project end date or end of duration ] Set variable [ $Location; Value:Log::Location ] Set variable [ $Description; Value:Log::Description ] Set variable [ $ScriptParams; Value:List($EventID; $Title; $StartDate; $EndDate; $Location; $Description) ] Run AppleScript [ Native AppleScript: Get(ScriptParameter) ] End If
对应的AppleScript(支持新建+更新):
set fmScriptParams to the result if length of fmScriptParams is 5 then -- 新建事件:返回事件ID给FM存储 set eventTitle to item 1 of fmScriptParams set startDate to item 2 of fmScriptParams set endDate to item 3 of fmScriptParams set eventLocation to item 4 of fmScriptParams set eventDescription to item 5 of fmScriptParams tell application "Calendar" set targetCalendar to calendar "FM" set newEvent to make new event at targetCalendar with properties ¬ {summary:eventTitle, start date:startDate, end date:endDate, location:eventLocation, description:eventDescription} return id of newEvent end tell else if length of fmScriptParams is 6 then -- 更新事件:根据ID找到对应事件修改属性 set targetEventID to item 1 of fmScriptParams set newTitle to item 2 of fmScriptParams set newStartDate to item 3 of fmScriptParams set newEndDate to item 4 of fmScriptParams set newLocation to item 5 of fmScriptParams set newDescription to item 6 of fmScriptParams tell application "Calendar" set targetEvent to event id targetEventID set summary of targetEvent to newTitle set start date of targetEvent to newStartDate set end date of targetEvent to newEndDate set location of targetEvent to newLocation set description of targetEvent to newDescription end tell end if
2. macOS日历事件修改时同步更新FM记录
通过定时运行AppleScript检查日历变化,调用FM脚本更新对应记录:
定时运行的AppleScript
set targetCalendarName to "FM" tell application "Calendar" -- 检查最近30分钟内修改的事件 set modifiedEvents to every event in calendar targetCalendarName where modified date > (current date) - 30 minutes repeat with anEvent in modifiedEvents set eventID to id of anEvent set eventTitle to summary of anEvent set eventStart to start date of anEvent set eventEnd to end date of anEvent set eventLocation to location of anEvent set eventDesc to description of anEvent -- 调用FM脚本更新记录 tell application "FileMaker Pro Advanced" run script "UpdateRecordFromCalendar" with parameters {eventID, eventTitle, eventStart, eventEnd, eventLocation, eventDesc} end tell end repeat end tell
FM对应的更新脚本(命名为UpdateRecordFromCalendar)
# 获取AppleScript传递的参数 Set variable [ $EventID; Value:Get(ScriptParameter)[1] ] Set variable [ $NewTitle; Value:Get(ScriptParameter)[2] ] Set variable [ $NewStartDate; Value:Get(ScriptParameter)[3] ] Set variable [ $NewEndDate; Value:Get(ScriptParameter)[4] ] Set variable [ $NewLocation; Value:Get(ScriptParameter)[5] ] Set variable [ $NewDescription; Value:Get(ScriptParameter)[6] ] # 找到对应的FM记录 Go to Layout [ "Log" (Log) ] Perform Find [ Restore; Criteria: Log::CalendarEventID = $EventID ] If [ Get(FoundCount) > 0 ] # 根据标题格式提取Log::X的值(需根据你的实际标题规则调整) Set field [ Log::X; Middle($NewTitle; 9; Length($NewTitle) - 9 - Length(Customers::CustomerID letters)) ] Set field [ Log::Date of project start duration; $NewStartDate ] Set field [ Log::Project end date or end of duration; $NewEndDate ] Set field [ Log::Location; $NewLocation ] Set field [ Log::Description; $NewDescription ] Commit Records/Requests [ With dialog: Off ] End If
内容的提问来源于stack exchange,提问作者harold
相关产品推荐
相关产品推荐

