Parsing text file (Flight Schedule) for iCal

Hi to you all from Holland,
A while ago this newbie posted a topic on GUI scripting in order to find out how I would be able to download my flightschedule from my company’s website without wearing out my fingers (a hell of a lot of clicking…) . It turned out that GUI scripting wasn’t the best choice because the website’s links change a lot.
(do) Javascript did a better job at that. Thanks to some of you guys my script works!
Anyway, my next project is even harder: with the other script I can produce a .txt and/or .html file with my flightschedule, as you can see down here, and I would want to get this stuff into iCal using Applescript.
It’s never exactly the same format, as you may understand. However, shorter flights produce the output as the one on MO 30 (Accra), longer flights (Montreal) produce the output as the one on MO 7. The rest are all single lines. I’ve been scripting away like h…, even plunged myself into Python programming (learned a lot about regular expressions), but I’m lost.
Anyone out there who can help me? (sorry for the “novellish” length of this posting :rolleyes: )

Schedule from 23 apr 07 to 20 May 07

MO 23 KL0567 RETURN
TU 24 FLIGHT LEAVE
WO 25 FLIGHT LEAVE
TH 26 FLIGHT LEAVE
FR 27 EXTRA FLIGHT LEAVE 80
SA 28 FLIGHT LEAVE
SU 29 PROFCHECK TK/PF/MI LICENSE! 0845 1345
MO 30 REPORTING 1220 LOCAL TIME AMS
FUNCTION 12
POINTS 6500 FLYING HOURS 1315
1135 KL0589 AMS ACC 1810 M11
TU 1 2110 KL0590 ACC AMS 0350 M11
WO 2 OFF DUTY 0620 LOCAL TIME AMS
CYCLE 6.25 TOT.POINTS 6450
TOT.FLYING HOURS 1315 CUM.CALENDAR 4200
TH 3 FLIGHT LEAVE
FR 4 FLIGHT LEAVE
SA 5 EXTRA FLIGHT LEAVE 80
SU 6 FLIGHT LEAVE EXTRA
MO 7 REPORTING 1410 LOCAL TIME AMS
FUNCTION 12
CYCLE 7.50 FLYING HOURS 1355
1325 KL0671 AMS YUL 2040 M11
TU 8 2255 KL0672 YUL AMS 0535 M11
WO 9 OFF DUTY 0805 LOCAL TIME AMS
TH 10 FLIGHT LEAVE
FR 11 FLIGHT LEAVE
SA 12 EXTRA FLIGHT LEAVE 80
SU 13 FLIGHT LEAVE
MO 14 RESERVE HOME 0700 1900
TU 15 RESERVE HOME 0700 1700
WO 16 REPORTING 1410 LOCAL TIME AMS
FUNCTION 12
CYCLE 7.50 FLYING HOURS 1355
1325 KL0671 AMS YUL 2040 M11
TH 17 2255 KL0672 YUL AMS 0535 M11
FR 18 OFF DUTY 0805 LOCAL TIME AMS
SA 19 FLIGHT LEAVE
SU 20 FLIGHT LEAVE

This tests out ok, But it makes a lot of assumptions.
1, the line Schedule from to will always be where it is.
2, the days all run in order. I.E 23, 24, 25,26,…


set sched to "Schedule from    23 apr 07 to  20 May 07
MO 23       KL0567   RETURN                       
TU 24  FLIGHT LEAVE                                 
WO 25  FLIGHT LEAVE                                 
TH 26  FLIGHT LEAVE                                 
FR 27  EXTRA FLIGHT LEAVE 80                        
SA 28  FLIGHT LEAVE                                 
SU 29  PROFCHECK TK/PF/MI LICENSE!   0845 1345           
MO 30 REPORTING  1220 LOCAL TIME AMS              
      FUNCTION  12                                 
      POINTS  6500     FLYING HOURS 1315         
       1135 KL0589   AMS   ACC     1810 M11       
TU  1  2110 KL0590   ACC   AMS     0350 M11       
WO  2 OFF DUTY   0620 LOCAL TIME AMS              
      CYCLE  6.25   TOT.POINTS   6450  
      TOT.FLYING HOURS  1315  CUM.CALENDAR     4200  
TH  3  FLIGHT LEAVE                                 
FR  4  FLIGHT LEAVE                                 
SA  5  EXTRA FLIGHT LEAVE 80                        
SU  6  FLIGHT LEAVE EXTRA                           
MO  7 REPORTING  1410 LOCAL TIME AMS              
      FUNCTION  12                                 
      CYCLE  7.50   FLYING HOURS 1355         
       1325 KL0671   AMS   YUL     2040 M11       
TU  8  2255 KL0672   YUL   AMS     0535 M11       
WO  9 OFF DUTY   0805 LOCAL TIME AMS              
TH 10  FLIGHT LEAVE                                 
FR 11  FLIGHT LEAVE                                 
SA 12  EXTRA FLIGHT LEAVE 80                        
SU 13  FLIGHT LEAVE                                 
MO 14  RESERVE HOME          0700 1900           
TU 15  RESERVE HOME          0700 1700           
WO 16 REPORTING  1410 LOCAL TIME AMS              
      FUNCTION  12                                 
      CYCLE  7.50   FLYING HOURS 1355         
       1325 KL0671   AMS   YUL     2040 M11       
TH 17  2255 KL0672   YUL   AMS     0535 M11       
FR 18 OFF DUTY   0805 LOCAL TIME AMS              
SA 19  FLIGHT LEAVE                                 
SU 20  FLIGHT LEAVE"
set date_list to {""}
set cal_name to "your_calendar" -- NAME OF CALENDAR TO USE

set scheds to paragraphs of sched as list
(* put the list together *)
---
repeat with i from 1 to count of items in scheds
	set this_para to item i of scheds
	if this_para contains "MO " or this_para contains "TU " or this_para contains "WO " or this_para contains "TH " or this_para contains "FR " or this_para contains "SA " or this_para contains "SU " then
		copy this_para to end of date_list
	else
		set last item of date_list to last item of date_list & this_para as string
	end if
	
end repeat
---
---
(* get the first line of the schedule , which holds the start date *)
set sched_dates to item 1 of date_list
set AppleScript's text item delimiters to "from"
set Sched_date_ to text item 2 of sched_dates -- 
set AppleScript's text item delimiters to "to"
set date_text1 to text item 1 of Sched_date_
set AppleScript's text item delimiters to ""

set thedate to (date date_text1)
---

(* write to the cal *)
--
repeat with i from 2 to number of items in date_list
	set this_item to item i of date_list
	set theSummary to this_item
	tell application "iCal"
		activate
		set theEvent to (make event at end of events of calendar cal_name with properties {start date:thedate, summary:theSummary})
	end tell
	set thedate to (thedate) + days (* adds 1 day to the start date for the next day *)
end repeat
--

:slight_smile:
Many thanks, Mark.
I am going to give it a try!

:confused:
Incredible, Mark, it all seems to be so easy.
I didn’t want to ask for too much the first time around. Forgive me for asking now.

  1. The days, ie MO 23, are reproduced in iCal. Is there any way to prevent that?
  2. Is there a way to filter out more or less unimportant information?
    For instance, on MO 23, I would like iCal to show:
    ‘Reporting 1220 KL 589 AMS ACC’ (or something similar) and on WO 2 just : OFF DUTY 0620
    On TU 1 I’m in Accra and in the evening flying back from ACC to AMS ; a statement like ‘ENROUTE’ would be sufficient.
  3. SU 29 shows a proficiency check from 0845 until 1345. I’d like an actual timed event in iCal. Any way to do that?
    Thanks for the help.

Most of that can probably be done.

can you do me a favour and post a txt file or post back clearly here with how each line should end up.
I.E
Again I will assume the wording and date format is always true.

MO 23 KL0567 RETURN ----->KL0567 RETURN
TU 24 FLIGHT LEAVE -----> FLIGHT LEAVE

MO 30 REPORTING 1220 LOCAL TIME AMS
FUNCTION 12
POINTS 6500 FLYING HOURS 1315
1135 KL0589 AMS ACC 1810 M11 -----> REPORTING 1220 LOCAL TIME AMS
SU 29 PROFCHECK TK/PF/MI LICENSE! 0845 1345 -----> PROFCHECK (set to actual time )

This will give me a clearer idea of what you want a how to possible go about it.

Mark,
I have sent you an email, because I’m wouldn’t know how to post a .txt file.
The wording in my schedule is not the same as the one I posted, because the whole thing is in Dutch.
However, if you could point me in the right direction, I can always change the script to the correct wording (I think…)
The date format however is always true.
Thanks a great deal sofar. (a nice bottle of wine perhaps?)

I’ve been trying out a way of stripping my schedule from statements I would rather not have in iCAL. So far the result looks promising.
(actually, the string Mark called “sched” is identical to the contents of my file “Schedule.txt”)
Only " LOCAL TIME AMS" AND “M11” still need to be removed.
Any comments on the script below would be appreciated.


set the_final_string to {}
set new_file_path to (path to desktop as Unicode text) & "Schedule.txt"
set new_file_path_ref to open for access file new_file_path
set dataFromFile to read new_file_path_ref as string
set ASCII_10 to (ASCII character 10)

tell application "TextCommands"
	set the_string to search dataFromFile for "^(.*\\b)(FUNCTION|POINTS|CYCLE|TOT.FLYING HOURS)(\\b.*$)" replacing with "" with regex and individual line matching
	set all_strings to split the_string using {ASCII_10}
	repeat with loopvar in all_strings
		set the_string to strip loopvar
		if the_string is not equal to "" then
			set the_final_string to the_final_string & (the_string) & return as string
		end if
	end repeat
	return the_final_string
end tell

I’m somewhat stunned: the script Mark wrote worked fine before, but now I constantly get a NSContainerSpecifierError on the statement :
set theEvent to make event at end of events of calendar cal_name with properties {start date:thedate, summary:theSummary}
Please help me out here!

Phewww…

Not going to say this was easy. (Not for me at least)
Seems to work.
Not sure how easy it will break. But give it ago.
It will give you an idea at least where to go from here if it does break. the main thing was setting up the times.

property PROFCHECK_ : "PROFCHECK"
property REPORTING_ : "REPORTING"
property OFF_DUTY : "OFF DUTY"
property RESERVE_HOME : "RESERVE HOME"
property M11 : "M11"
--global pfc, pfcSub, pfcCalTimes, rpcCalTimes, rpcSub, rpcSub1, wd1, wdx, odyCalTimes, startdate, endDate
set check_List to {PROFCHECK_, REPORTING_, OFF_DUTY, RESERVE_HOME, M11}
set new_file_path to (path to desktop as Unicode text) & "Sched.txt"
set dat_list to read file new_file_path
set sched to do shell script "echo " & quoted form of dat_list & "| sed '/CYCLE/d'|sed '/FUNCTION/d'|sed '/POINTS/d' |sed '/HOURS/d' |sed 's/LOCAL\\ TIME\\ AMS//g'"

set date_list to {""}
set edit_1 to {}
set cal_name to "your_calendar" -- NAME OF CALENDAR TO USE

set scheds to paragraphs of sched as list
(* put the list together *)
---
repeat with i from 1 to count of items in scheds
	set this_para to item i of scheds
	if this_para contains "MO " or this_para contains "TU " or this_para contains "WO " or this_para contains "TH " or this_para contains "FR " or this_para contains "SA " or this_para contains "SU " then
		copy this_para to end of date_list
	else
		set last item of date_list to last item of date_list & this_para as string
	end if
	
end repeat

(* get the first line of the schedule , which holds the start date *)
set sched_dates to item 1 of date_list
set AppleScript's text item delimiters to "from"
set Sched_date_ to text item 2 of sched_dates -- 
set AppleScript's text item delimiters to "to"
set date_text1 to text item 1 of Sched_date_
set AppleScript's text item delimiters to ""

set thedate to (date date_text1)
--
repeat with i from 2 to number of items in date_list
	set edit_item to item i of date_list
	set wd1 to characters 6 thru -1 of edit_item as string
	repeat with a from 1 to number of items in check_List
		set check_item to item a of check_List
		if wd1 contains check_item then
			if check_item is PROFCHECK_ then
				set pfcCalTimesa to word -2 of wd1
				set pfcCalTimesb to word -1 of wd1 -- actual ical times
				
				set pfcSub to characters 1 thru word -3 of wd1 as string -- subject for ical
				set wd1 to {pfcSub, pfcCalTimesa, pfcCalTimesb}
			else if check_item is REPORTING_ then
				set rpcCalTimes to word 2 of wd1  -- actual ical time
				set rpcSub to REPORTING_ & " " & rpcCalTimes & " " & (characters -14 thru -1 of (characters 1 thru word -2 of edit_item as string) as string) -- subject for ical
				set wd1 to {rpcSub, (rpcCalTimes)}
				
			else if check_item is OFF_DUTY then
				set odyCalTimes to word -1 of wd1 -- actual ical time
				set wd1 to {wd1, (odyCalTimes)}
			else if check_item is RESERVE_HOME then
				set rsvhCalTimes to word 3 of wd1
				set rsvhCalTimesz to word 4 of wd1  -- actual ical time
				set wd1 to {RESERVE_HOME, rsvhCalTimes, rsvhCalTimesz}
			else if check_item is M11 then
				set wd1 to {characters -10 thru word 2 of wd1 as string}
				
			end if
		end if
	end repeat
	copy wd1 to end of edit_1
end repeat



repeat with c from 1 to number of items in edit_1
	
	set startdate to thedate
	set endDate to thedate
	
	set cal_item to item c of edit_1 as list
	
	
	if (count of items of cal_item) ≥ 2 then
		
		set theSummary to item 1 of cal_item
		set startdate's hours to (characters 1 thru 2 of item 2 of cal_item as string)
		set startdate's minutes to (characters 3 thru 4 of item 2 of cal_item as string)
		set endDate to startdate as string
		set endDate to date endDate
		try
			set endDate's hours to (characters 1 thru 2 of item 3 of cal_item as string)
			set endDate's minutes to (characters 3 thru 4 of item 3 of cal_item as string)
		end try
		
	else
		
		set theSummary to item 1 of cal_item
		
		
	end if
	
	tell application "iCal"
		activate
		
		set theEvent to (make event at end of events of calendar cal_name with properties {start date:startdate, end date:endDate, summary:theSummary})
	end tell
	set startdate's hours to "00"
	set startdate's minutes to "00"
	set endDate's hours to "00"
	set endDate's minutes to "00"
	set thedate to (thedate) + days (* adds 1 day to the start date for the next day *)
	
end repeat



As to the error you are getting from the other script, I would at a guess suspect the Name of the cal in the script does not exist in iCal. did you change a name in ical or the script with out changing the other.

:slight_smile:
Mark, many, many thanks for your trouble.
:frowning:
However, I still cannot figure out what causes the NSContainerSpecifierError (shows up in your last script as well). I haven’t changed the calendar name, the error just showed up, out of the blue…
I read a posting where someone was having the same problem, and inserting a script line with a filter solved the problem there, but no joy here.
I’ll try the script on my Tiger-machine (iMac), see if it works there.
Frustrating!


set cal_name to (first calendar whose title contains "Calendar")
set theEvent to (make event at end of events of calendar cal_name with properties {start date:startdate, end date:endDate, summary:theSummary})

Hi any news on this…???

Hi there, Mark. Actually, yes.
You’re script works fine on my iMac (Tiger). Of course I had to modify it slightly (schedule is in Dutch, different profchecks, type recurrents, medical etc).
However on my Powerbook (Panther) I’m still having problems. First of all the NSContainerSpecifierError keeps showing up and now, after extending the properties-listing (as I said above, diff. profchecks, type recurrents etc.) and adding extra script lines to deal with these, I am getting a “Stack Overflow” on the line :
set startdate’s hours to (characters 1 thru 2 of item 2 of cal_item as string)
To deal with that first of all I changed your script into a main part and several handlers, secondly I deleted the properties-list (thought that would use up a lot of memory) and changed the coding to handle your “check_list”. No joy, however.
Ah, you won’t believe it, but my Powerbook’s harddrive died on me. I’ll post the changed script as soon as I put in a new one.

Ah I did not write it for panther? I am in tiger, panther will be using a different applescript version to tiger and so not recognise some of the commands or give unexpected results.

But post it when you can. I think I have a Panther install on my old G3, but I suspect i upgraded it to tiger??, so may not be able to test it in panther.

maybe someone else can help with that.

I am glad that it works on your tiger though, It was a bit of a bugger to write. :wink:

Hi there Mark,
Thanks for your patience (and your effort, for that matter).
My Powerbook is purring again with a Tiger installed on it… :smiley:
I’ll post your script with the changes I had to make as soon as I have it working as well as the original script did.
One of the problems I ran into after translating from English to Dutch : in your script it says "if this_para contains “MO” or “TU” …“FR”…"etc. “FR” in my Dutch schedule reads “VR”. However the airport code for Vancouver is “YVR”, so running the script gave some strange results. It took me hours to figure out what was wrong. Finally I figured out that I could change “contains” into “begins with”.
The other “nuisance” is that I prefer OS in English, but my (date)formats are Dutch. This causes some weird behavior from AS. F.i. “27 Mar 07”(=mrt in Dutch), (with “May”(=mei) and “Oct” (=okt) the same) kept turning out as “27 juli 2007”.
Phewww… You can say that again…
Just one more thing I cannot figure out:
TH 24 REPORTING 1415 LOCAL TIME AMS
FUNKTION 12
CYCLE LENGTH 8.75 FLYING HOURS 1910
1330 KL0681 AMS YVR 2315 M11
SA 26 0155 KL0682 YVR AMS 1120 M11
OFF DUTY 1350 LOCAL TIME AMS
As you can see, Friday is gone because the actual return flight from Vancouver is scheduled after midnight GMT, so on the Saterday. The script as it is puts the OFF DUTY on Friday instead of Saterday, so after that every event in iCal is one day too early.
This is because of the fact the script strips the days from the schedule first and then loops them back in going through the calendar days one by one, I believe.
Would you or anyone else have a solution for this?
Thanks a million.

Hi again.

Can you post/email me the script as you have it so far. plus the new txt with Vancouver.
cheers

Hi Mark,
To begin with, I read a posting of “Jobu” on the subject “Macscripter Survey”. I believe he is right in many respects and I do feel a little embarrassed. I wish I could give you more credit for the hard work you’re putting into this. (not forgetting the rest of the forum-contributors ; I’ve learnt a lot and taken a lot from this forum).
The only thing I can do in return is promise you a safe flight on one of our aircraft. But on the other hand and not as an excuse : I’ve bought 4 books on Applescript and a Unix-“bible”, so give me a couple of years and I’ll contribute with one of my own scripts. The script and schedule you were asking for: (I’ve made up a fake schedule with all the possibilities, translated it into English; the script is very similar to your original, except for a few add-ons I got from this forum :rolleyes:)


set fakesched to "Schedule from 25 may 07 to 23 jun 07
FR 25  LINE FL. UNDER SUPERVISION                  
          REPORTING TIME 0955 LOCAL TIME AMS              
          FUNCTION  12                                 
          CYCLELENGTH  7.50   FLYINGHOURS 1640         
          0910 KL0871   AMS   DEL     1720 M11       
SA 26  1920 KL0872   DEL   AMS     0350 M11       
SU 27  OFF DUTY   0620 LOCAL TIME AMS 
MO 28  FLIGHT LEAVE
TU 29   EXTRA FLIGHT LEAVE 80
WE 30   FLIGHT LEAVE EXTRA
TH 31   LINE CHECK                                  
           REPORTING TIME 0955 LOCAL TIME AMS              
           FUNCTION  12                                 
           CYCLELENGTH  7.50   FLYINGHOURS 1640         
           ADD UV+2                             
           0910 KL0871   AMS   DEL     1720 M11       
FR  1    1920 KL0872   DEL   AMS     0350 M11       
SA  2   OFF DUTY   0620 LOCAL TIME AMS
SU  3    RESERVE HOME          0700 1900           
MO  4   RESERVE HOME          1100 2300           
TU  5    RESERVE HOME          0900 2100  
WE  6   STANDBY HOME          0900 1000 
TH  7   REPORTING TIME 1910 LOCAL TIME AMS              
           FUNCTION  12                                 
           POINTS  6100     FLYINGHOURS 1345         
           1925 KL0429   AMS   DXB     0155 M11       
FR  8    0325 KL0430   DXB   AMS     1040 M11       
           OFF DUTY   1210 LOCAL TIME AMS              
           CYCLELENGTH  6.25   TOT.POINTS   6105  
           TOT.FLYING HOURS  1345  CUM.CALENDAR     4100  
SA  9    TYPE RECURRENT 1       0745 1345   
SU 10   YEARLY LEAVE UNTIL 15 JUNI 07
           ADD PL+2
SA 16   PREMIUM DAY 
SU 17   TYPE RECURRENT 2       1045 1715           
MO 18   MEDICAL SPL-EAST B133   1515 1715
TU 19    OPS.CH. BOM/FRG        0845 1345  
WE 20    FCL TK/PF/MI LICENCE!   0845 1345
TH 21    REPORTING TIME 1415 LOCAL TIME AMS              
            FUNCTION  12                                 
           CYCLELENGTH  8.75   FLYINGHOURS 1910         
           1330 KL0681   AMS   YVR     2315 M11       
SA 23   0155 KL0682   YVR   AMS     1120 M11       
           OFF DUTY   1350 LOCAL TIME AMS
SU 24   COURSE GENERAL	0900 1700"

property FCL_CHECK : "FCL"
property OPS_CHECK : "OPS"
property REPORTING_ : "REPORTING"
property OFF_DUTY : "OFF DUTY"
property RESERVE_HOME : "RESERVE HOME"
property STANDBY_HOME : "STANDBY HOME"
property MEDICAL_ : "MEDICAL"
property TYPE_1 : "TYPE RECURRENT 1"
property TYPE_2 : "TYPE RECURRENT 2"
property Y_LEAVE : "YEARLY LEAVE"
property COURSE : "COURSE"
(*property desktop_path : path to desktop folder
property sched_dl : "Schedule.html"*)

global fakesched
global scheds
global date_list
global check_List
global edit_1
global thedate
global this_calendar
set edit_1 to {}

set date_list to {""}

(*tell application "iCal"
	if "KLM" is not in title of calendars then
		set this_calendar to make new calendar at end of calendars with properties {title:"KLM"}
	else
		set this_calendar to first calendar whose title is "KLM"
		end if
end tell*)

(*--I download my schedule as a html file, so I need to convert "Schedule.html" to "Schedule.txt" with textutil(Unix-utility)
set sched_html to POSIX path of ((desktop_path as string) & (sched_dl as string))
do shell script "/usr/bin/textutil -convert txt " & sched_html
set sched_txt to (desktop_path as string) & "Schedule.txt"
--read "Schedule.txt" 
set sched to read file sched_txt*)

(* put the list together *)
sched_to_list()


(*get the first line of the schedule  which holds the start date*)
set sched_temp to item 1 of date_list
set AppleScript's text item delimiters to "from"
set sched_period to text item 2 of sched_temp
set AppleScript's text item delimiters to "to"
set date_text1 to text item 1 of sched_period
(*if date_text1 contains "mar" then
	set date_text1 to frepl(date_text1, "mar", "mrt")
else if date_text1 contains "may" then
	set date_text1 to frepl(date_text1, "may", "mei")
else if date_text1 contains "oct" then
	set date_text1 to frepl(date_text1, "oct", "okt")
end if*)
set AppleScript's text item delimiters to ""
set thedate to date date_text1

--edit the list for iCal
edit_for_ical()

--times and summary to iCal
time_summary()

on sched_to_list()
	set sched_strip to do shell script "echo " & quoted form of fakesched & "| sed '/ADD/d'|sed '/CYCLELENGTH/d'|sed '/FUNCTION/d'|sed '/POINTS/d' |sed '/FLYINGHOURS/d' |sed 's/LINE\\ FL.\\ UNDER\\ SUPERVISION//'|sed 's/LINE\\ CHECK//' |sed 's/LOCAL\\ TIME\\ AMS//g'"
	set scheds to paragraphs of sched_strip as list
	repeat with i from 1 to count of items in scheds
		set this_para to item i of scheds
		if this_para begins with "MO " or this_para begins with "TU " or this_para begins with "WE " or this_para begins with "TH " or this_para begins with "FR " or this_para begins with "SA " or this_para begins with "SU " then
			copy this_para to end of date_list
		else
			set last item of date_list to last item of date_list & this_para as string
		end if
	end repeat
end sched_to_list

on frepl(the_string, search_string, replace_string)
	return my list_to_string((my string_to_list(the_string, search_string)), replace_string)
end frepl

on list_to_string(the_list, the_delim)
	my atid(the_delim)
	set the_string to (every text item of the_list) as string
	my atid("")
	return the_string
end list_to_string

on string_to_list(the_string, the_delim)
	my atid(the_delim)
	set the_list to (every text item of the_string) as list
	my atid("")
	return the_list
end string_to_list

on atid(the_delim)
	set AppleScript's text item delimiters to the_delim
end atid

on edit_for_ical()
	set check_List to {FCL_CHECK, OPS_CHECK, REPORTING_, OFF_DUTY, RESERVE_HOME, STANDBY_HOME, MEDICAL_, TYPE_1, TYPE_2, Y_LEAVE, COURSE}
	repeat with i from 2 to number of items in date_list
		set edit_item to item i of date_list
		set wd1 to characters 6 thru -1 of edit_item as string
		repeat with a from 1 to number of items in check_List
			set check_item to item a of check_List
			if wd1 contains check_item then
				
				if check_item is FCL_CHECK or check_item is OPS_CHECK or check_item is RESERVE_HOME or check_item is STANDBY_HOME or check_item is MEDICAL_ or check_item is TYPE_1 or check_item is TYPE_2 or check_item is COURSE then
					set pfcCalTimesa to word -2 of wd1
					set pfcCalTimesb to word -1 of wd1 -- actual ical times
					--subject for iCal
					set pfcSub to characters 1 thru word -3 of wd1 as string
					set wd1 to {pfcSub, pfcCalTimesa, pfcCalTimesb}
					
				else if check_item is REPORTING_ then
					set rpcCalTimes to word 2 of wd1
					set rpcSub to REPORTING_ & " " & (characters -28 thru -10 of (characters 1 thru word -2 of edit_item as string) as string)
					set wd1 to {rpcSub, (rpcCalTimes)}
					
				else if check_item is OFF_DUTY then
					set odyCalTimes to word -1 of wd1 -- actual ical time
					set edit_item to item (i - 1) of date_list
					set odySub to OFF_DUTY & " " & (characters -28 thru -22 of (characters 1 thru word -2 of edit_item as string) as string)
					set wd1 to {odySub, (odyCalTimes)}
					
					(*******ACTUALLY I'M LOST HERE : YEARLY LEAVE UNTIL 15TH OF JULY???? *******)
					(*else if check_item is Y-LEAVE then
					set wd1 to "?"*)
				end if
			end if
		end repeat
		copy wd1 to end of edit_1
	end repeat
end edit_for_ical

on time_summary()
	repeat with c from 1 to number of items in edit_1
		set startdate to thedate
		set endDate to thedate
		set cal_item to item c of edit_1 as list
		if (count of items of cal_item) ≥ 2 then
			set theSummary to item 1 of cal_item
			set startdate's hours to (characters 1 thru 2 of item 2 of cal_item as string)
			set startdate's minutes to (characters 3 thru 4 of item 2 of cal_item as string)
			set endDate to startdate as string
			set endDate to date endDate
			try
				set endDate's hours to (characters 1 thru 2 of item 3 of cal_item as string)
				set endDate's minutes to (characters 3 thru 4 of item 3 of cal_item as string)
			end try
		else
			set theSummary to item 1 of cal_item
		end if
		tell application "iCal"
			tell this_calendar to set theEvent to make new event at end of events with properties {start date:startdate, end date:endDate, summary:theSummary}
		end tell
		set startdate's hours to "00"
		set startdate's minutes to "00"
		set endDate's hours to "00"
		set endDate's minutes to "00"
		set thedate to (thedate) + days (* adds 1 day to the start date for the next day *)
	end repeat
end time_summary