New to Applescript, is this possible

Hi All,

First post so please be gentle :wink:

I’m trying to coax iCal to become a basic “Electronic Program Guide”. My idea is to download my local TV schedule, and then decompress it. Inside the archive are text files for each channel for the entire month. the format is:

Filename: HBO.txt

UBC Chanel : HBO
Date : 01/07/2006
Start time Program

00:45 Fahrenheit 9/11
03:00 The American President
05:00 The Molly Maguires
07:00 The Land Before Time X: The Great Longneck Migration

etc etc - then it repeats for the following day.

I’ve already figured i need create a new calendar for each channel (which is no hassle)
One thing i need to do is to subtract 30 minutes from each “Start Time” as i’m in a slightly different time zone.

So there it is…Is Applescript capable of doing what i want? If it can’t automate the download and decompression thats ok, i just need an easy way to get the .txt data correctly into iCal…

Thanks in advance,

Karl

Got it almost. See test script. I assume that you can script iCal event yourself (you may search for it on this board)
Only thing that goes wrong is when start time is between 00:00 ans 00:30, I don’t get the date set back then.


set theR to "UBC Chanel : HBO
Date : 01/07/2006
Start time      Program
------------------------------------------------------------------------------------
00:45            Fahrenheit 9/11
03:00            The American President
05:00            The Molly Maguires
07:00            The Land Before Time X: The Great Longneck Migration"

set {theChannel, theDate, TitlStr} to {"", "", ""}
repeat with i from 1 to number of paragraphs of theR
	set r to (paragraph i of theR) as string
	if r starts with "UBC Chanel :" then set theChannel to my substitute(r, "UBC Chanel : ", "")
	if r starts with "Date :" then set theDate to my substitute(r, "Date : ", "")
	if number of words of r > 1 and number of characters of word 1 of r = 2 and character 3 of r is ":" then
		set timeStr to (text from word 1 to word 2 of r) as string
		set TitlStr to my substitute(r, timeStr, "")
		set TitlStr to my substitute(TitlStr, "  ", "")
		set hrs to word 1 of r as number
		set mins to word 2 of r as number
		set mins to mins - 30
		if mins < 0 then
			set hrs to (hrs - 1) as string
			if number of characters of hrs < 2 then set hrs to ("0" & hrs) as string
			set mins to mins + 60
		end if
		
		if hrs < 0 then
			set hrs to 23
			set theDate to theDate as date
		end if
		
		set timeStr to (hrs & ":" & mins) as string
		
		display dialog (theDate & ", " & theChannel & ", " & timeStr & ", " & TitlStr) as string
		--> insert iCal event creation here with arguments (theDate, theChannel, timeStr, TitlStr)
	end if
end repeat

on substitute(theText, toReplace, newText)
	set AppleScript's text item delimiters to the toReplace
	set the allTheText to every text item of theText
	set AppleScript's text item delimiters to the newText
	set theText to the allTheText as string
	set AppleScript's text item delimiters to ""
	return theText
end substitute

Hi Eelco,

Nice one, thanks for that.

I’ve actually been reading up on different methods, and have started adapting Doug's AppleScripts » Import Track Info From Text Files this script for importing. I change days by encountering a “Date :” statement in the txt file - then just insert calendar,time,event" into iCal…i think (still have not got it working quite right).

still need a lot more automation, i’ve 50 seperate .txt files to parse monthly…

I’ll hammer on with this and post results accordingly…

many thanks for your effort and input :wink: (still need more ideas readers!)

Cheers,

Karl

Oops!

just had a play with this - came up with a few oddities.

if i replaced your header with a fileselect segment, it complained that it could not get every paragraph.

if i replaced the header with the full channel listing, it borked when the date went to 02/07/2006…

i’ll try to figure this one…

Cheers

Karl

Hi,

further input required i’m afraid. I’ve successfully modified the script above to do the following:

  1. open txt file from location and read.

  2. insert events into iCal

“whoopee!” - not quite. there are a few problems.

It works fine when i work with a text file like the one i pasted above. The real files repeat for every day of the month…is there a filesize restriction with applescript?

iCal is either ignoring my date and inserting the program at the current time, or i’ve got the date format wrong - can anyone give this to me in laymans please ? Currently i have dd/mm/yyyy * hh:mm -

tell application "iCal"
			activate
			set theCal to (first calendar whose title is theChannel)
			tell theCal
				make new event at beginning of theCal with properties {start date:timeStr & theDate, summary:TitlStr}
			end tell
		end tell

thanks in advance,

Karl

Bah,

an update, it is the date format.

in my txt i have 01/07/2006

if i run this through “date” i get:

Wednesday January 7, 0060

:frowning:

Ciao phazey,
I guess it has to do with the date and time settings of your Mac: if you are using the American date format the system will read your datestring as “mm/dd/yyyy”.
So before creating new events in iCal you will have to manipulate your datestrings to make them compatible with your system format.

Good scripting
Farid