Script & speed

Hello!

I’ve written a little script to change my iBook’s screen brightness:

tell application "System Events"
	key code 113 --or 107
end tell

But it’s not working well. I have to run 5-10 times the script, to simulate one F1 or F2 push. I think, the script is too fast. The brightness change object (sun :-)) is coming up, but nothing happens.

Can somebody tell me, how can I slow it down? Eg. like this:

key down option
delay 0.3
key up option

key down (key code 113) --this not working

Thanks a lot! (sorry for my English)

This does the trick here, Bachman

tell application "System Events" to repeat 16 times
	key code 113 -- or 107
	delay 0.3
end repeat

Your English is great. I hope you never have to suffer my attempts at Hungarian. :wink:

:expressionless: Thanks, but maybe You didn’t understand the problem. If I run this script:

tell application "System Events" to repeat 16 times
   key code 113 -- or 107
end repeat

sometimes nothing happen, only the sun come up. Sometimes changes the brightness with 1 step, sometimes 2 or 3 steps. If I using the click at {x,y} command, I have the same problems: Sometimes working, sometimes not.

I want to do this but it’s not working:

tell application "System Events"
   key down (key code 113) -- or 107
   delay 0.3
   key up (key code 113)
end repeat

Thanks!

Take out “end repeat” (I also decreased the delay.)

tell application "System Events"
	key down (key code 113) -- or 107
	delay 0.03
	key up (key code 113)
end tell

Each click increases brightness by two settings. The script will keep working until the sun icon fades out - just keep clicking.

j

If I use this:

tell application "System Events"
   key down (key code 113) -- or 107
   delay 0.03 -- or 0.3
   key up (key code 113)
end tell

I always got this error message:

System Events got an error: NSCannotCreateScriptCommandError

But I solved the original problem. I’ve founded a great app. named Salling Clicker and it’s Daemon is very scriptable.

Thanks!

I’m pretty sure I did, Bachman - although our machines may differ slightly in their responses.

Without the delay that I suggested, the key code commands are almost certainly being sent too quickly for them all to register. Try re-inserting the delay, and then experimenting with the length of it.

Much as I dislike being defeatist about this, I don’t think you’re going to be able to make this work.

The ‘key down’ and ‘key up’ commands are part of System Events’ Hidden Suite, and don’t show up in the AppleScript dictionary. But they generally expect parameters like a string or, more commonly, a modifier key (shift, command, control & option). By contrast, ‘key code’ is a command in its own right. The net result of this is that the ‘key down/up’ commands are issued with the current application as a parameter - and, since this doesn’t make much sense, the commands are ignored. (They may also error on some systems.)

However, if there’s no error, the ‘key code’ part of the statements could still be executed.

So the net result would really be equivalent to:

tell application "System Events"
	key code 113
	delay 0.3
	key code 113
end tell

As you can see, this means that the brightness level will be changed by only a couple of steps at most.

As far as I’m aware, the bottom line is that you’re pretty much stuck with three options:

Edit: I see that, while I was typing this, you came up with the latter - which is great. However, I hope that the above explanation still helps to clarify the nature of the problems that you were experiencing. :slight_smile: