Devonthink/Add-selected-file-to-Calend.scpt

76 lines
2.9 KiB
AppleScript

-- Prompt for date and time
set inputDate to text returned of (display dialog "Enter date (D/M):" default answer "")
set inputTime to text returned of (display dialog "Enter time (H-M):" default answer "")
-- Prompt for location
set locationOptions to {"18 Tran Huu Duc
My Dinh 2, Nam Tu Liem, Ha Noi, Vietnam", "2 Lang Ha
Thanh Cong, Ba Dinh, Ha Noi, Vietnam", "27 Dai Co Viet
Le Dai Hanh, Hai Ba Trung, Ha Noi, Vietnam"}
set chosenLocation to choose from list locationOptions with prompt "Select a location:" default items {"18 Tran Huu Duc
My Dinh 2, Nam Tu Liem, Ha Noi, Vietnam"}
-- Prompt for note
set noteOptions to {"TB", "PB. Thư", "PB. Hoa", "PB. Quý", "PB. Thuỷ", "PB. Dân", "PB. Hải"}
set chosenNote to choose from list noteOptions with prompt "Select a note:" default items {"TB"}
-- Parse the input date and time
set {dayStr, monthStr} to my parseDate(inputDate)
set {hourStr, minuteStr} to my parseTime(inputTime)
-- Set the year to 2025
set yearInt to 2025
-- Create the start and end date-time objects
set startDateTime to my createDate(yearInt, monthStr, dayStr, hourStr, minuteStr)
set endDateTime to my createDate(yearInt, monthStr, dayStr, (hourStr + 2), minuteStr)
-- Get the selected DEVONthink record
tell application id "DNtp"
set theRecord to content record
set theName to name of theRecord
set theURL to reference URL of theRecord
end tell
-- Extract the event name from the file name
set eventName to my extractEventName(theName)
-- Create the event in Apple Calendar
tell application "Calendar"
tell calendar "Agribank" -- Calendar name set to "Agribank"
set newEvent to make new event with properties {summary:eventName, start date:startDateTime, end date:endDateTime, location:item 1 of chosenLocation, url:theURL, description:item 1 of chosenNote}
make new display alarm at newEvent with properties {trigger interval:-1 * (24 * 60) + (20 * 60)} -- Alert 1 day before at 8 PM
end tell
end tell
-- Handlers
on parseDate(inputDate)
set {TID, text item delimiters} to {text item delimiters, "/"}
set {dayStr, monthStr} to text items of inputDate
set text item delimiters to TID
return {dayStr, monthStr}
end parseDate
on parseTime(inputTime)
set {TID, text item delimiters} to {text item delimiters, "-"}
set {hourStr, minuteStr} to text items of inputTime
set text item delimiters to TID
return {hourStr, minuteStr}
end parseTime
on createDate(yearInt, monthStr, dayStr, hourStr, minuteStr)
set theDate to current date
set year of theDate to yearInt
set month of theDate to monthStr as integer
set day of theDate to dayStr as integer
set time of theDate to (hourStr as integer) * hours + (minuteStr as integer) * minutes
return theDate
end createDate
on extractEventName(fileName)
set {TID, text item delimiters} to {text item delimiters, "_"}
set eventName to text item 3 of fileName
set text item delimiters to TID
return eventName
end extractEventName