Dertmining a blank location field in iCal

Hi new to AppleScript so I am probably missing something obvious. But I have written a script that imports an iCal event into filemaker. With a lot of code snipets from here. (Thanks All). It is working well except if the Location or Description fields in iCal are blank or empty. It gives me an error message when I try to put a blank or empty value into the field. I have tried various If/Then’s to script around this with no success. Any help would be appreciated. Here is the script:

[NOTE: I added the AppleScript tags so folks could easily copy this to their own Script Editor - Adam Bell]


tell application "iCal" to activate
tell application "System Events"
	tell process "iCal"
		keystroke return
		keystroke "c" using {command down}
		keystroke return
	end tell
end tell

set TheLocation to " "
set TheDescription to " "

tell application "iCal"
	activate
	set cb to the clipboard
	set the_cal to calendar "Exchange"
	set the_event to first event of the_cal whose summary is cb
	set TheStartDate to start date of the_event
	set TheSummary to the summary of the_event
	set TheEndDate to the end date of the_event
	set TheDescription to the description of the_event
	set TheLocation to the location of the_event
	set TheUID to the (uid of the_event as string)
	set start_time_string to (time string of TheStartDate)
	set end_time_string to (time string of TheEndDate)
	tell start_time_string
		set start_time_without_sec to (word 1 & ":" & word 2 & space & word 4)
	end tell
	tell end_time_string
		set end_time_without_sec to (word 1 & ":" & word 2 & space & word 4)
	end tell
end tell



tell application "FileMaker Pro"
	activate
	tell database "GTD"
		tell table "Meeting"
			set theRecord to create new record
			set data of field "MeetingSubject" of theRecord to TheSummary
			set data of field "MeetingLocation" of theRecord to TheLocation
			set data of field "iCalUID" of theRecord to TheUID
			set data of field "MeetingDate" of theRecord to my formatDate(TheStartDate)
			set data of field "MeetingTime" of theRecord to start_time_without_sec
			set data of field "MeetingEndTime" of theRecord to end_time_without_sec
			set data of field "IniCal" of theRecord to "Yes"
			set data of field "Status" of theRecord to "Confirmed"
			set data of field "Agenda" of theRecord to TheDescription
		end tell
	end tell
end tell

on formatDate(aDate)
	try
		set fmDateString to ((month of aDate as integer) as string) & "/" & ((day of aDate as integer) as string) & "/" & ((year of aDate as integer) as string)
		return fmDateString
	on error errMsg number errNum
		error ("formatDate error: " & errMsg) number errNum
	end try
end formatDate

Thanks in Advance,

George

Model: MacBook Pro
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi, George;

Quite a script for a professed n00b! I’m uncertain what the opening Return and Copy accomplish for you - When I try it, it just returns the name of the frontmost Calendar (I have several).

Thanks alot.

I must admit I had a lot of help from posts seen on this board and I guy named Nik that is good at this stuff.

The Opening Return and Copy came from a post here (somewhere). It is a work around for not beeing able to operate on the currently selected event in ical. You must have an event selected.

Any one have ideas on how to get around a potentially blank field in ical? I’ve sliced a smaller version of the issue below:

tell application “iCal”
set TheDescription to the description of the_event
set TheLocation to the location of the_event
end tell

tell application “FileMaker Pro”
activate
tell database DBNAME
tell table TABLENAME
set theRecord to create new record
set data of field “MeetingLocation” of theRecord to TheLocation
set data of field “Agenda” of theRecord to TheDescription
end tell
end tell
end tell

Thanks in advance, again,

George

Hi George,

You can check if a varible is undefined with an error handler. e.g.


tell application "iCal"
	set the_cal to first calendar whose title is "Work"
	set the_event to last event of the_cal
	set event_loc to location of the_event
end tell
try
	get event_loc
on error
	beep 2
	set event_loc to "none" -- or "" or whatever
end try
return event_loc

If the variable event_loc is undefined, the script errors and the variable is set to whatever you want.

gl,

Hi George,

It’s better to work with the properties of the event:


tell application "iCal"
	set c to first calendar whose title is "Work"
	set e to last event of c
	set p to properties of e
end tell
p

Then you can get the values from the record. undefined properties are set as missing value, since a record field has to be defined.

gl,

Kel,

Thanks. The Try command was exactly what I was looking for and solved my immediate problem. I’ll work on the properties too…

Thanks again,

George