scripts that run on application events and that access current time?

I have some scripts that are run at different times using CronniX. They run on a normal basis. However, I want to know if scripts can be run with these sorts of trigger:

  1. can a script be triggered to run when another app is closed?
  2. cronnix only plays scripts at their designated time when the computer is on. There are no exceptions. I have a script that needs to run upon startup, but only if it is in a certin time range. Is there acript that can get the computers time? That way, I can save my script as a startup app that will run if it is in its time range.

The circumstances aren’t quite clear here, but I use something like this when I have started an application from a script and have to wait until it finishes and quits automatically (without informing me):

tell application "System Events"
		if exists process "WhateverYouStarted" then
			delay 2
			exit repeat -- at which point my script continues knowing that the other has quit.
		end if
	end repeat
end tell

If you want a standalone script then you need an idle handler that starts when the other application does and waits for it to quit before going on to whatever else is to happen.

What do you mean by the “computer’s” time? How long it has been running? [or do you mean time of day?] If the latter, it can be extracted from set theTime to (current date) as string. —> “Saturday, January 14, 2006 7:31:12 PM”

If you want to know how long an application has been running, this was posted some time ago. I don’t recall who posted it and I haven’t used it either:

on idle
	set app_uptime to my get_app_uptime("Safari")
	if app_uptime's total_up_in_seconds > (12 * hours) then
		set the_button to button returned of (display dialog "Safari must quit!" buttons {"yes", "don't"} default button 2 with icon 1)
		if the_button = "yes" then tell application "Safari" to quit
	end if
	return (10 * minutes) --check every 10 minutes
end idle

on get_app_uptime(app_name)
		set the_PID to word -1 of (do shell script "ps -xco command,pid | grep " & (quoted form of app_name))
		set app_uptime to text 18 thru -5 of paragraph -1 of (do shell script "ps -xco command,lstart -p " & the_PID)
		set {wd, m, d, h, min, S, y} to app_uptime's words
		set app_uptime to date (m & " " & d & " " & y & " " & h & ":" & min & ":" & S)
		set app_uptime to {date_launched:app_uptime, total_up_in_seconds:(current date) - app_uptime}
	on error
		set app_uptime to {date_launched:"n/a", total_up_in_seconds:0}
	end try
	return app_uptime
end get_app_uptime

Well, i want the script to get the current time. If it is between 8 and 18, then it will run one app, and if it is else, it will run another. This is what I have now. It works, but when the app is done running, I get the error below. Any ideas why? These apps below are apps that I made, and they run fine otherwise. When I put the apps iChat and iTunes in below, there are no problems.

 property sunRise : 8 * hours + 0 * minutes (* in case you want to consider minutes *)
property sunSet : 18 * hours + 0 * minutes

tell (current date)'s time to if (it > sunRise and it < sunSet) then
	activate application "Daylight scc"
	activate application "nightime scc"
end if

tell current application
	current date
		date "Saturday, January 14, 2006 6:02:00 PM"
end tell
tell application "nightime scc"
		current application
		"nightime scc got an error: Connection is invalid."

Thanks again!!!