Problem with handler

Hi all!

Applescript newbie here, so this is probably rudimentary:

I’ve done an iTunes script which involves frequent timechecks to determine if it is time to switch playlists. The script is working as I want, but I now want to replace all the repeated time checks with handlers (one for each “time profile”), so that if I want to change the intervals, I need only to change the handler. My handlers are simple and look like this:

on checkTime()
	if (hours of (current date)) ≤ 8 or (hours of (current date)) ≥ 15 then
		exit repeat
	end if
end checkTime

I then call that handler within my script with a:

checkTime() of me

command (the “of me” part being because it is inside a “tell application” script)

When the if-statment is true, Applescript returns an error about the closing message not being a repeated loop (unsure of the exact english phrasing, I’m on a foreign version). My layman theory is that this is because the handler involves the exiting of a repeat-loop that is defined outside the handler? I would need to have it this way though, because that repeat-loop also consists of a number of other commands and instructions that I don’t want confined in my handler.

Basically, all I need is a way to have Applescript read those three lines of code everytime I enter a simple replacing command. I thought that a handler would be the way to do this, but I can’t get it to work. Any help is greatly appreciated!

Thanks,
Erik

Hi Erik,

a repeat - exit repeat - end repeat loop works only within the same scope.
Every handler has its own scope.
A solution is to return a boolean value from the handler and check this value in the repeat loop

repeat 10 times
	if checkTime() then
		exit repeat
	end if
end repeat

on checkTime()
	set hr to hours of (current date)
	return (hr ≤ 8 or hr ≥ 15)
end checkTime

Thanks Stefan, that did the trick! And I would never have figured it out on m own. Much appreciated!

Best regards
Erik