Run script if it's a certain time

Hi,

I have a script that uploads files in a folder starting at 10 Pm. However, it’s still often running when I come in at 9 AM. For every file that the script prepares to upload, I want it to check the time, and if it’s between 9 AM and 10 PM, stop the script. Otherwise, proceed.

In attempting to get this time piece working, I’m just concentrating on that. But I don’t seem to be going about it correctly. I found a function that gets the time in hours and minutes, but I am not sure that’s the right way to go about this. This simple test is failing. Here’s what I have:

set a to getTimeInHoursAndMinutes()

if a > "9:00 AM" and a < "10:00 PM" then
	
	return true
else
	return false
end if

on getTimeInHoursAndMinutes()
	-- Get the "hour"
	set timeStr to time string of (current date)
	set Pos to offset of ":" in timeStr
	set theHour to characters 1 thru (Pos - 1) of timeStr as string
	set timeStr to characters (Pos + 1) through end of timeStr as string
	
	-- Get the "minute"
	set Pos to offset of ":" in timeStr
	set theMin to characters 1 thru (Pos - 1) of timeStr as string
	set timeStr to characters (Pos + 1) through end of timeStr as string
	
	--Get "AM or PM"
	set Pos to offset of " " in timeStr
	set theSfx to characters (Pos + 1) through end of timeStr as string
	
	return (theHour & ":" & theMin & " " & theSfx) as string
end getTimeInHoursAndMinutes

Hi,

this does the same as your script :wink:

tell (current date) to return its hours = 9

To run a script at the same time, take a look at this article in Code Exchange:
Install / uninstall a launchd agent to execute scheduled scripts

Hi.

I believe the aim is to find out if the script’s running between 9 am and 10 pm. This does strictly what jsocks1’s script attempts, returning ‘true’ if the current time’s between those times and ‘false’ at exactly those times or outside them:

tell time of (current date) to return (it comes after 9 * hours) and (it comes before 22 * hours)

oops, sorry.
I’m not very familiar with this am/pm stuff :wink:

Heh heh. Thanks for your suggestion, Stefan.

Nigel’s script worked perfectly for my application! Thanks much!

Nah. Real men use 24-hour clocks. :wink: