Repeat until keystroke

Some background info: I am using the IRED software so that I can control Front Row on my Imac with the Remote from my Stereo System as well, not just with the Apple remote.

IRED basicly connects each IR signal from the remote to an Apple Script. E.g to start playing music I would push space on the keyboard, the apple script that IRED calls up that does this looks like this

tell application "System Events"
try
if process "Front Row" is frontmost then
keystroke " " -- space
end if
end try
end tell

So in anutshell, each time I push a button on the remote IRED callls up an applescript to simulate pushing the respective button on the keyboard.

So far so good. For scrolling through my music collection I would like to starts crolling down until another button is pushed and another apple script is launched.

I am thinking along the line

set message to " "
repeat until message contains "Stop"
tell application "System Events"
key code 125
end tell
end repeat

Question is, what do I have to put in the other apple apple script that the variable message will equal "stop". I tried the ovious but that wasn't flying.

What also may work is, get another applescript to push some sort of keycombination that forces the looped apple script to stop but I doubt that is possible.

Any input is appreciated.

JK

Powerbook G4/Intel Imac 20", Mac OS X (10.4.6)

Posted on Apr 27, 2006 11:04 PM

Reply
3 replies

Apr 28, 2006 11:32 AM in response to JK1002

This is actually harder than you might think.

Due to AppleScript's single-threaded nature you can't have two handlers running at once - that is if you have a on doSomething() handler that's running a continuous loop (such as simulate a key press) you can't call another handler until it finishes.

The only thing I can think of right now is to use an idle handler to periodically press the keys and another handler (which is called by IRED) to change the key. something like:

global theKey

on changeKey (newKey)
set theKey to newKey
end changeKey

on run
set theKey to ""
end run

on idle
if theKey ≠ "" then
tell application "System Events" to keystroke theKey
end if
return 1
end idle


The idea here is that theKey is a global variable indicating the key to press. The idle handler periodically (once per second) presses that key. Since it's an idle handler, other handlers such as changeKey can be called - the trick now is getting IRED to call the changeKey() handler to change the key to use. Off hand I can't see an easy way to do that.

Apr 28, 2006 2:07 PM in response to JK1002

that's running a continuous loop (such as simulate a key press) you can't call another handler until it finishes<</div>

See that is the funny thing. I was calling with one applescript 50 or so times key code 125 in a row. then IRED launched another, that called Keystroke "space" and that does stop the initial script which used the key code command. This works only though with keystroke. Using key code would not stop the intial launched apple script. I don't think though I can pass content of variables between both scripts, which may break it.

If I launch a script in an endless loop, is there some sort of keystroke that stops it? Right now I switch of the mac and reboot but I am guessing there may be some key combination for this.

Thanks
JK

Apr 29, 2006 5:40 PM in response to JK1002

Try this one which uses Jon's Commands OSAX "keys pressed" routine to detect keyboard activity. The OSAX is available here:

http://osaxen.com/files/jonscommandsx3.0d3.html

-- begin script

set theKeyPressed to "" -- initialize variable

repeat until theKeyPressed is "Caps Lock" -- do until Caps Lock key is pressed

tell application "System Events"
key code 125 -- send the
end tell

set theKeyPressed to (keys pressed) as string -- watch for keys being pressed

end repeat

-- end script


PowerBook 12" Mac OS X (10.4.6)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Repeat until keystroke

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.