Get and Set a link to a local file or folder in the iCal URL field

While reading, a macworld tip
Which informed the users of iCal, How they could use the url field for local file urls as well as web. ( which I did know already ;))
I thought I would write a script to make it less tedious.
Any comments or ideas welcome (well nearly any :D)

Use this script to help add a link to a local file or folder in iCal url field.
Place this script in you iCal script menu, or the Applescript menu.
When in iCal: Select the url field, for the event you want to add a link to a local file or folder.
Now go to the iTunes script menu or applscript menu,
which ever you place the script in, and selected the script. The script will prompt you to choose the typ of item for you link, File or Folder. It will then place a time stamp in the url.
(YOU MUST HAVE THE URL FIELD SELECTED FIRST).
The script will now search iCal for the event with the timestamp, once found it will replace the timestamp with the url of the item you chose.

(*Script by Mark hunte October 2007*)
(*Use this script to help add a link to a local file or folder in iCal  url field.
Place this script in you iCal script menu, or the Applescript menu.
When in iCal: Select the url field, for the event you want to add a link to a local file or folder.
Now go to the iTunes script menu or applscript menu,
which ever you place the script in, and selected the script. The script will prompt you to choose the typ of item for you link, File or Folder. It will then place a time stamp in the url.
 (YOU MUST HAVE THE URL FIELD SELECTED FIRST).
The script will now search iCal for the event with the timestamp, once found it will replace the timestamp with the url of the item you chose.*)
global ud, the_event, the_eventProps
tell application "iCal"
	activate
	(*get a time stamp  code to enter in the selected url address. This will be used to search the events*)
	set timestamp to "http://" & (do shell script "date +'%H%M%S'" as string)
	(*choose what type of item you want to select, file of folder *)
	display dialog "Get URL of ?" buttons {"Cancel", "File", "Folder"} default button 3
	if the button returned of the result is "Folder" then
		set mychoice to "Folder"
		my myUrl(mychoice, timestamp)
	else if the button returned of the result is "File" then
		set mychoice to "File"
		my myUrl(mychoice, timestamp)
	end if
	
end tell
on myUrl(mychoice, timestamp)
	(*set and Run the choose dialog *)
	set chosen to "tell application \"ical\" to  (choose " & mychoice & " without invisibles and  multiple selections allowed as alias)" as string
	set the_item to run script chosen
	(*get the url of the file or folder *)
	tell application "Finder" to set theFileURL to (URL of the_item)
	tell application "System Events"
		(*stamp the timestamp into the url field*)
		keystroke timestamp
		delay 1
		(*hit escape button so the url field is set, or ical will not see the change*)
		keystroke (key code 53)
		tell application "iCal"
			set the_event to ""
			repeat with i from 1 to (count of calendars)
				tell calendar i
					try
						(*look for the event url with the timestamp*)
						set the_event to (first event whose url contains timestamp)
						(*set the event url to the file/folder url*)
						set url of the_event to theFileURL
						(*exit, the is no need to carry on *)
						if the_event is not "" then
							exit repeat
						end if
						
					end try
				end tell
			end repeat
		end tell
	end tell
end myUrl

Nice script!

I was going to post something about how it didn’t seem like you should have to paste something into a field in order to access the currently-being-viewed event, then I looked at the dictionary. It appears that iCal has nothing in the dictionary about the ‘selection’ property - application doesn’t have one. That’s just sad.

Good job working around yet another example of where Apple doesn’t bother to support their own excellent technology: AppleScript.

By the way, since Contextual Menus work in iCal fields, it looks like you could install OnMyCommand and make a module that works with it to do this in a (somewhat) more elegant way.

Thanks,

Yes the no selection in iCal is a real pain.

Here is part of my original idea of how to get the selected event. But it relies on the event summery being unique.
you have to selected the event (double click)

global ud
tell application "System Events"
	tell process "iCal"
		set cE to (get value of text area 1 of scroll area 1 of window "iCal")
	end tell
	tell application "iCal"
		--activate
		set ud to ""
		repeat with i from 1 to (count of calendars)
			tell calendar i
				try
					set the_event to (first event whose summary is cE)
					set ud to uid of the_event
					if ud is not "" then
						exit repeat
					end if
					
				end try
			end tell
		end repeat
	end tell
end tell

{ud, the_event, cE}

Actually I am glad I worked out some small means of getting the selected event.
But I think I was being too clever by half, when All I needed to do was select the url field and paste the file or folder url into it,
foregoing the timestamp and search in iCal. :confused:

(*Script by Mark hunte October 2007*)
(*Use this script to help add a link to a local file or folder's url in iCal.
Place this script in you iCal script menu, or the Applescript menu.
When in iCal: Select the url field, for the event you want to add a link to a local file or folder.
Now go to the iTunes script menu or applscript menu,
which ever you place the script in, and selected the script. The script will prompt you to choose the typ of item for you link, File or Folder. It will then place file or folder url in the url field.
 (YOU MUST HAVE THE URL FIELD SELECTED FIRST).
.*)
global ud, the_event, the_eventProps
tell application "iCal"
	activate
	
	(*choose what type of item you want to select, file of folder *)
	display dialog "Get URL of ?" buttons {"Cancel", "File", "Folder"} default button 3
	if the button returned of the result is "Folder" then
		set mychoice to "Folder"
		my myUrl(mychoice)
	else if the button returned of the result is "File" then
		set mychoice to "File"
		my myUrl(mychoice)
	end if
	
end tell
on myUrl(mychoice)
	(*set and Run the choose dialog *)
	set chosen to "tell application \"ical\" to  (choose " & mychoice & " without invisibles and  multiple selections allowed as alias)" as string
	set the_item to run script chosen
	(*get the url of the file or folder *)
	tell application "Finder" to set theFileURL to (URL of the_item)
	tell application "iCal"
		activate
	end tell
	tell application "System Events"
		(*put the url into the url field*)
		keystroke theFileURL
		delay 1
		(*hit escape button so the url field is set, or ical will not see the change*)
		keystroke (key code 53)
	end tell
end myUrl