Repeat loops & Tell blocks

Hi,

In this first script the tell block is in the repeat loop:

repeat
	tell application "ASObjC Runner"
		if caps lock key down of modifier keys then
			exit repeat
		end if
	end tell
end repeat
beep 1

In this second script the tell block is outside:

tell application "ASObjC Runner"
	repeat
		if caps lock key down of modifier keys then
			exit repeat
		end if
	end repeat
end tell
beep 1

I’ve always thought that the second way was better. Is this true? I could make the first script more graceful with:

set do_exit to false
repeat
	tell application "ASObjC Runner"
		if caps lock key down of modifier keys then
			set do_exit to true
		end if
	end tell
	if do_exit then exit repeat
end repeat
beep 1

I was just wondering which is the best.

Thanks,
kel

I don’t know that it makes any difference.

But I’d use either the first or last because I’d add always add a delay in a repeat loop like that, and those forms would let me add the delay outside the tell block.

Hi Shane,

I see what you’re saying. It depends on the purpose. Script 2 might be used to force the user to press the caps lock key.

BTW, after I erased and reinstalled my system, I had no problem with your Scripting Addition app. Maybe one thing I did wrong the last time was to install ASObjC Runner in the Applications folder. This time it’s in ScriptingAdditions.

Thanks,
kel

It should be fine in Applications – that’s where I run it. One thing, though, is that you can have multiple instances running at once, and because it doesn’t appear in the Dock, if you don’t have the menu showing, it’s hard to tell.

Ok, I didn’t know the menu item had some purpose other than running ASObjC.

Thanks,
kel

It’s good for trouble-shooting, and when testing stuff out.

May I suggest a 4th solution in this case? I think for this particular situation this is the best looking and also the same.


tell application "ASObjC Runner"
	repeat until caps lock key down of modifier keys
		
	end repeat
end tell
beep 1

Like Shane already mentioned, if you need to target different applications it will it will become prettier when you target inside the repeat block instead of outside. But there is no problem targeting another application or script object, like current application or me, when inside an tell application block. Repeat instructions are core AppleScript (not using standard addition commands or any other osax) so the code of your first and second example would (should) look either way the same after compiling.

To make code prettier or targeting your code less complex I prefer to use handlers. I almost never use tell applications in or outside repeat loops. The code in the run handler or outside the handlers is run as current application code, everything else is wrapped in handlers most of the time. It’s just something I prefer. It’s one of the beauties but also confusing things in AppleScript, there are many ways/styles you can write your code.

So there is no best for all situations. The different approaches can make it easier to target application(s).

Hello.

I surmise you are having the delay statement there, so that your script doesn’t block, that is you are letting the system schedule and run other tasks, while you are waiting for the keypressed “event”. (I put event into quotation marks since I have no idea of how you implemented it.)

I wonder how long a delay you use. Personally I think I’d settle for 0.10 seconds, so the user shouldn’t even notice any delay, but I’m curious. :slight_smile:

I suppose it’s more to give the app more time to handle calls from any other apps, and avoid having the underlying Cocoa code polling more than necessary. I guess in truth it just doesn’t feel right to ask it do something more often than will make any noticeable difference.

I’d probably pick something similar, maybe 0.2 – it’s arbitrary. Although with all the talk about new processors and power-saving modes, it’s probably the sort of decision that’s being generally encouraged.

Hahaha (about power saving mode) I have some real heretics for you Shane:

I’d wish that Apple buys Microsoft’s Windows 7 kernel, and revamps it to fit nicely into XNU, because the Windows 7 kernel, (or whatever the real name is, is an impressive piece of work, with good scheduling on threads and what not: moving threads over to 1 core, to save power.

Maybe there has come some news from WWDC that I should look into. (The post was mostly to show you what true heretics can be like). :smiley:

I know, it is never going to happen, maybe they roll their own, they will have to do it, some time in the near future, hopefully in a way that makes no difference for the callers of the kernel today, because the model is really all right. (Not too complicated.)

Hi DJ Bazzie Wazzie,

I had no idea that the compiled code would look about the same. What I’ve learned is that there is nothing wrong with placing tell blocks in repeat loops. I’ve had suspicions that the opposite way was better, but I guess not.

Thanks a lot everybody,
kel