Droplet to create iCal event with multiple alams

Hi
I am new to Apple script and have been searching for ages to put together a droplet scripts that:

  • I can drag text onto and have it create an ical event with the current date and time
  • Placing text draged to it into the “notes” or “description” area
  • showing a dialog box asking for “event name”
  • having several preset alarms (8 actually - Spaced repetition memorization)

I have found this script to make an event

tell application “iCal”
tell calendar “Memorize”
set theCurrentDate to current date
make new event at end with properties {description:“Event Description”, summary:“Event Name”, location:“Event Location”, start date:theCurrentDate, end date:theCurrentDate + 10 * minutes}
end tell
end tell

Can any one help me?

Model: Macbook Pro
AppleScript: 1
Browser: Firefox 5.0.1
Operating System: Mac OS X (10.6)

Hi, clintonh. Welcome to MacScripter.

Unfortunately, you can’t drop text onto an AppleScript droplet. You have to use some alternative method to get it into the script. In the getNote() handler below, the selection of the frontmost application is copied to the clipboard and is read from there. (The assumption is that you’ll run the script by some means which doesn’t make the script itself the frontmost application.)

When creating an alarm, you have to specify what kind of alarm to create (‘display alarm’, ‘mail alarm’, ‘open file alarm’, or ‘sound alarm’) and give it parameters appropriate to that type. Here, I’ve used a ‘sound alarm’ (the kind called “Message with Sound” in iCal’s GUI), using the system’s “Submarine” sound and a repeat interval of 15 minutes.

This probably won’t be exactly what you want, but it should give you something on which to build. Ask again if you have further questions, of course. :slight_smile:

property alarmCount : 8 -- Number of reminder alarms.
property alarmInterval : 15 -- Alarm trigger interval in minutes.
property alarmSound : "Submarine"

on getNote()
	-- The contents of this handler should be replaced with whatever code you use to input the note text. This copies the selection of the frontmost application to the clipboard and reads it from there.
	tell application "System Events" to keystroke "c" using command down
	delay 1
	set noteText to (the clipboard)
	
	return noteText
end getNote

on getSummary()
	set summaryText to ""
	repeat while summaryText is ""
		tell application (path to frontmost application as text)
			set summaryText to text returned of (display dialog "What do you want to call the new event?" default answer summaryText default button 2)
		end tell
	end repeat
	
	return summaryText
end getSummary

on getLocation()
	tell application (path to frontmost application as text)
		set locationText to text returned of (display dialog "Enter the location (optional):" default answer "" default button 2)
	end tell
	
	return locationText
end getLocation


set noteText to getNote()
set summaryText to getSummary()
set locationText to getLocation()
set theCurrentDate to (current date)

tell application "iCal"
	tell calendar "Memorize"
		set newEvent to (make new event at end with properties {description:noteText, summary:summaryText, location:locationText, start date:theCurrentDate, end date:theCurrentDate + 10 * minutes})
		repeat with i from 1 to alarmCount
			make new sound alarm at end of newEvent with properties {trigger interval:i * alarmInterval, sound name:alarmSound}
		end repeat
	end tell
end tell

Thanks NG,
Very helpful response,
I spent most of my day trying to figure out how to make it work - I learnt a lot but still haven’t achieved what I was hoping for.

I did figure out that I was heading down the wrong track because my iPad/iPhone doesn’t support more that 2 alarms.

What I think I am after now is script that will allow me to enter: Event Name, Location & Note manually via dialog box and have it duplicate the event at intervals: 10min, 60min, 24hr, 7 days, 3 months, 6 months, 12months

I am so new at this I have gotten myself a little frustrated & you probably don’t want to spend any more time on this but I would be grateful if you could offer and assistance.

thanks for your help so far,

Kind Regards

Clinton

Model: Macbook Pro
AppleScript: ?
Browser: Firefox 5.0.1
Operating System: Mac OS X (10.6)

Hi, Clintonh.

Month-based intervals are rather a vague concept, since months aren’t all the same length. The addMonths() handler below returns the date of the same-numbered day in the calendar month so-many removed from the original date. If the day doesn’t exist in that month, the last day of the month is returned instead.

on getInput(thePrompt)
	repeat
		tell application (path to frontmost application as text)
			set inputText to text returned of (display dialog thePrompt default answer "" default button 2)
		end tell
		if ((count inputText) > 0) or (thePrompt contains "optional") then exit repeat
	end repeat
	
	return inputText
end getInput

to addMonths(oldDate, m) -- Parameters: (date, positive or negative integer)
	copy oldDate to newDate
	set {y, m} to {m div 12, m mod 12}
	if (m < 0) then set {y, m} to {y - 1, m + 12}
	set newDate's year to (newDate's year) + y
	set d to oldDate's day
	if (m > 0) then tell newDate + (32 * m - d) * days to set newDate to it + (d - (its day)) * days
	if (newDate's day is not d) then set newDate to newDate - (newDate's day) * days
	
	return newDate
end addMonths

on main()
	set summaryText to getInput("What do you want to call the new events?")
	set locationText to getInput("Enter the location (optional):")
	set noteText to getInput("What do you want to put in the note?")
	set theCurrentDate to (current date)
	set theCurrentDatePlus10 to theCurrentDate + 10 * minutes
	
	tell application "iCal"
		tell calendar "Memorize"
			repeat with t in {0, 10 * minutes, hours, days, weeks} -- The intervals from now up to a week, in seconds.
				make new event at end with properties {description:noteText, summary:summaryText, location:locationText, start date:theCurrentDate + t, end date:theCurrentDatePlus10 + t}
			end repeat
			repeat with m in {3, 6, 12} -- The "months from now" intervals, in months.
				set xMonthsTime to my addMonths(theCurrentDate, m)
				make new event at end with properties {description:noteText, summary:summaryText, location:locationText, start date:xMonthsTime, end date:xMonthsTime + 10 * minutes}
			end repeat
		end tell
	end tell
end main

main()

thanks again NG,

I cut an pasted your script solution into my script editor compiled it saved it and run it and I get this error message

error “The variable noteText is not defined.” number -2753 from “noteText”

and as far as I can tell it has not entered an “appointments” or reminder appintments

I am really showing how much of a rookie I am at scripting here but I have know idea what the heck it is returning this error for.

I do really appreciate your time helping me thought.

Thanks

Clinton

Model: Macbook Pro
AppleScript: ?
Browser: Firefox 5.0.1
Operating System: Mac OS X (10.6)

I’m so sorry, clintonh. For some reason, the getInput() handler didn’t have a ‘return’ line in it when I posted the script. I’ve now corrected it above.

By the way, you don’t have to copy/paste scripts in these fora which have the “> Open this Scriplet in your Editor:” link at the top. If you click on the link, the script should open in your default editor.

You can get this facility in your own posts either by clicking the “Applescript” button above the posting window and pasting your script between the [applescript] and [/applescript] tags it inserts, or by writing in the tags in yourself.

Nigel Garvey, Your a legend.

Thanks so much you have made me a happy man.

All kudos to you for your superior scripting.

I did like that format you put it in also. It was easy to follow.

Do you have any suggestion on how to begin the journey of learning Applescript.

Thanks again

Clinton

Just one more thing and it will be perfect: An Alarm for each interval/event!

I am wondering if it would be better to have only one event with alarm intervals at the above stated times
Or
Just a alarm at each event.

I am unsure which one would be easier to script but I know it would be less cluttered in the calendar, that doesn’t really matter as it is in its own calendar though.

If you could help me with this final step that would be awesome.

thanks

Clinton

Well. You said earlier that your iPad/iPhone doesn’t support more than two alarms (presumably per event), so it would have to be multiple events. I’ve modified my earlier script to create the events using just one repeat loop and to add immediate alarms to all but the first:

on getInput(thePrompt)
	repeat
		tell application (path to frontmost application as text)
			set inputText to text returned of (display dialog thePrompt default answer "" default button 2)
		end tell
		if ((count inputText) > 0) or (thePrompt contains "optional") then exit repeat
	end repeat
	
	return inputText
end getInput

to addMonths(oldDate, m) -- Parameters: (date, positive or negative integer)
	copy oldDate to newDate
	set {y, m} to {m div 12, m mod 12}
	if (m < 0) then set {y, m} to {y - 1, m + 12}
	set newDate's year to (newDate's year) + y
	set d to oldDate's day
	if (m > 0) then tell newDate + (32 * m - d) * days to set newDate to it + (d - (its day)) * days
	if (newDate's day is not d) then set newDate to newDate - (newDate's day) * days
	
	return newDate
end addMonths

on main()
	set summaryText to getInput("What do you want to call the new events?")
	set locationText to getInput("Enter the location (optional):")
	set noteText to getInput("What do you want to put in the note?")
	
	set theCurrentDate to (current date)
	
	set repeatDates to {theCurrentDate + 10 * minutes, theCurrentDate + hours, theCurrentDate + days, theCurrentDate + weeks, addMonths(theCurrentDate, 3), addMonths(theCurrentDate, 6), addMonths(theCurrentDate, 12)}
	
	tell application "iCal"
		tell calendar "Memorize"
			make new event at end with properties {description:noteText, summary:summaryText, location:locationText, start date:theCurrentDate, end date:theCurrentDate + 10 * minutes} -- No alarm on the first event.
			repeat with r in repeatDates
				(make new event at end with properties {description:noteText, summary:summaryText, location:locationText, start date:r, end date:r + 10 * minutes})
				make new sound alarm at end of result with properties {trigger interval:0, sound name:"Submarine"}
			end repeat
		end tell
	end tell
end main

main()

Mind you, the multiple-alarm approach works on real computers (;)) and looks like this:

on getInput(thePrompt)
	repeat
		tell application (path to frontmost application as text)
			set inputText to text returned of (display dialog thePrompt default answer "" default button 2)
		end tell
		if ((count inputText) > 0) or (thePrompt contains "optional") then exit repeat
	end repeat
	
	return inputText
end getInput

to addMonths(oldDate, m) -- Parameters: (date, positive or negative integer)
	copy oldDate to newDate
	set {y, m} to {m div 12, m mod 12}
	if (m < 0) then set {y, m} to {y - 1, m + 12}
	set newDate's year to (newDate's year) + y
	set d to oldDate's day
	if (m > 0) then tell newDate + (32 * m - d) * days to set newDate to it + (d - (its day)) * days
	if (newDate's day is not d) then set newDate to newDate - (newDate's day) * days
	
	return newDate
end addMonths

on main()
	set summaryText to getInput("What do you want to call the new event?")
	set locationText to getInput("Enter the location (optional):")
	set noteText to getInput("What do you want to put in the note?")
	
	set theCurrentDate to (current date)
	
	set triggerIntervals to {10 * minutes, hours, days, weeks, addMonths(theCurrentDate, 3) - theCurrentDate, addMonths(theCurrentDate, 6) - theCurrentDate, addMonths(theCurrentDate, 12) - theCurrentDate} -- Alarm trigger intervals in seconds.
	
	tell application "iCal"
		tell calendar "Memorize"
			set newEvent to (make new event at end with properties {description:noteText, summary:summaryText, location:locationText, start date:theCurrentDate, end date:theCurrentDate + 10 * minutes})
			repeat with t in triggerIntervals
				make new sound alarm at end of newEvent with properties {trigger interval:t div minutes, sound name:"Submarine"}
			end repeat
		end tell
	end tell
end main

main()

Nigel,
Once again how do I thank you for your help.

In the event of sounding to over the top I will just say:

THANKS

The repeated events works awesome Just what I wanted.
The multiple alarm I am testing just now.

I am a very happy man.

You have single handed’ly given me a tool that my memory improvement and goals for memorizing has been hinging on.

In my books you are a very generous, kind and inspirational man.

Thanks

Clinton