How can I identify title of each next iCal event and open specifix tex

I am new t this so I need your suggestions. The concept is this:

I have input already lots of events in iCal. I want to identify the name (ex. “Chapter 1”) of the next scheduled event, COPY that title and SEARCH for the text file with the same name of the ical event. Then OPEN the Chapter 1.txt, COPY the content of that file and PASTE it in a new mail. SEND this mail to recipient of the specific event. The hole procedure must be done the alarm time of the iCal event.

I used Automator to do that but I didn’t succeed to copy the title and search for it in finder, cause the title every time is different. I am not sure if I must try to write the hole procedure only in AppleScript editor and call the script directly from iCal. Any suggestions?

Model: iMac
AppleScript: 2.3
Browser: Safari 534.50
Operating System: Mac OS X (10.7)

I have done the whole procedure in Automator. The only thing is missing is to identify the TITLE of the next event and tell finder to open the .txt file with the same name. Should I write a script for this? Don’t know how. The rest of the procedure works fine. Any help?

Hi, stamjoe. Welcome to MacScripter.

Here’s a start on a script for you. It identifies the event(s) which have the earliest start date/time after the current moment, uses their summaries to reference the corresponding text files, and reads the files. It doesn’t as yet actually do anything with the text!

  1. What are you using to send the e-mails?
  2. By “recipient of the specific event”, do you mean an invitee?
  3. When you say “The hole procedure must be done the alarm time of the iCal event,” does that mean the script will be run by an alarm? And if so, how long before each event will the alarm go off?
  4. What sort of text do the text files contain? (eg. Mac Roman? UTF-8? UTF-16?)
property textFileFolderPath : (path to desktop as text) & "Text files:" -- Your folder path here.
property calendarName : "My calendar" -- Your calendar name here.

set now to (current date)

-- Get the start dates and UIDs of all the events in the calendar.
tell application "iCal" to set {startDates, UIDs} to {start date, uid} of every event of calendar calendarName
set eventCount to (count startDates)

-- Identify the earliest start date which comes after the present moment.
tell (now + 0) to set {year, month, day, time, nextStartDate} to {9999, December, 31, days - 1, it}
repeat with i from 1 to eventCount
	set thisDate to item i of startDates
	if ((thisDate comes after now) and (thisDate comes before nextStartDate)) then set nextStartDate to thisDate
end repeat

-- Go through the date list again to find the date(s) which match the date found above.
repeat with i from 1 to eventCount
	if (item i of startDates is nextStartDate) then
		--  For each matching date, use the corresponding UID to identify the relevant event. Get the event's summary.
		set thisUID to item i of UIDs
		tell application "iCal" to set thisSummary to (summary of event id thisUID of calendar calendarName)
		-- Use the summary in a path to the corresponding text file and read the file.
		set thisFilePath to textFileFolderPath & thisSummary & ".txt"
		set thisText to (read file thisFilePath) -- Assuming non-Unicode text in the file.
		
		-- Make the e-mail here.
	end if
end repeat

First of all I have to say a big THANK YOU for your spend of your time and energy.

Let me now answer to your questions.

  1. I am using “Mail” application to send these mails
  2. Very often I send mails to different recipients. Not the same person. I don’t enter invitees in ical menu, if you mean that.
  3. Yes the script must be run the alarm time and start the procedure, identify the text, copy etc. and ends with sending the mail to a specific address. Imagine that this is a memo note, so I want to send these memos every morning and the recipients have all day to work on them. So I am not very interesting about how long before will be the alarm time of the event.
  4. Usually they are UTF-8 but not always.

I have tested the script and input the mail script in the line that you suggest. Script is:

[code]tell application “Mail”
activate

		set this_message to make new outgoing message at end of outgoing messages with properties {content:thisText, visible:true}
		
		tell this_message
			make new to recipient at end of to recipients with properties {address:"address@address.com"}
		end tell
		
	end tell[/code]

It’s working but the text inside mail probapbly has a problem in the Unicode. I am writing in greek language, the most of them. How can I fix this?

I thought that was probably the case, but it’s as well to check. :slight_smile:

Yes. The script needs to know who the recipients are when it creates the ‘recipients’ in Mail. I was wondering if their details would be in the iCal event.

The reason I asked is that a script which is run by an iCal alarm doesn’t automatically know which event’s alarm triggered it. This can be a problem if the distance between an alarm and its event time is greater than the distance between events. For instance, if two events have alarms which trigger 15 minutes before the event times, but the events are only 10 minutes apart, the script run by the second event’s second alarm will identify the first event as the “next” one. The script will need more code to deal with that. It’ll also need adjustment if the alarms are triggered exactly at the event time.

The ‘read’ command has to be told what kind of data to expect so that it can interpret them properly. In my script above, it’s assuming 8-bit (non-Unicode) text by default. I may be able to get it to make an intelligent deduction…

You are right Nigel. I make a test. Create 2 events.

1st event: at 3.00 with title “Text A” and alarm time 2 minutes ahead from current time
2nd event: at 3.30 with title "Text B"and alarm time 2 minutes ahead from current time, same as the first event.

The script sends the mails successfully, but both in the mails attached the file “Text A”.

Texts A & B were valid files in the correct path. I guess that happened cause of the distance alarm and event that you mention right? Xm! Weird. I can solve for now this by putting the events in different time and every alarm has exactly the same time with the event. The only main problem for now is the Unicode. Can you help me on this? I have no idea how to write about the unicode, especially for greek language.

OK.

This version checks the first two bytes of the text file to see if they’re a UTF-16 byte-order mark. If not, UTF-8 is assumed. We may need to change this assumption if it doesn’t work.

Since the alarms are triggered at the start times of the events, I’ve brought the time of ‘now’ forward by fifteen seconds in case the moment has passed by the time the script actually gets going.

There’s still a potential duplication problem if two alarmed events have the same start time, but I’m not sure what to do about it at the moment.

property textFileFolderPath : (path to desktop as text) & "Text files:" -- Your folder path here.
property calendarName : "My calendar" -- Your calendar name here.

set now to (current date) - 15 -- Allow 15 seconds in case the exact second of the event has passed.

-- Get the start dates and UIDs of all the events in the calendar.
tell application "iCal" to set {startDates, UIDs} to {start date, uid} of every event of calendar calendarName
set eventCount to (count startDates)

-- Identify the earliest start date which comes after the present moment.
tell (now + 0) to set {year, month, day, time, nextStartDate} to {9999, December, 31, days - 1, it}
repeat with i from 1 to eventCount
	set thisDate to item i of startDates
	if ((thisDate comes after now) and (thisDate comes before nextStartDate)) then set nextStartDate to thisDate
end repeat

-- Go through the date list again to find the date(s) which match the date found above.
repeat with i from 1 to eventCount
	if (item i of startDates is nextStartDate) then
		--  For each matching date, use the corresponding UID to identify the relevant event. Get the event's summary.
		set thisUID to item i of UIDs
		tell application "iCal" to set thisSummary to (summary of event id thisUID of calendar calendarName)
		-- Use the summary in a path to the corresponding text file and read the file.
		set thisFilePath to textFileFolderPath & thisSummary & ".txt"
		set fRef to (open for access file thisFilePath)
		try
			set sample to (read fRef for 2 as data) -- Read the first 2 bytes in the file to test for a UTF-16 BOM.
			if ((sample is «data rdatFFFE») or (sample is «data rdatFEFF»)) then -- UTF-16.
				set thisText to (read fRef from 1 as Unicode text)
			else -- Assume UTF-8.
				set thisText to (read fRef from 1 as «class utf8»)
			end if
		end try
		close access fRef
		
		-- Make the e-mail here.
	end if
end repeat

YOU ARE GREAT!

I run the new script and it works perfectly. I am not worrying about the duplications cause I can manage them easily. The greek text looks perfect inside e-mails.

You can’t imagine how much time I will save by this script. Thank you very much Nigel.

Model: iMac
AppleScript: 2.3
Browser: Safari 534.50
Operating System: Mac OS X (10.7)