keystroke on background

Hello again! I’m trying to make an script that will press a certain key from time to time and allow you to move around the computer.
I already have the script done, but it requires that the application is active (frontmost)

on idle
	tell application "System Events"
		tell application "appname" to activate
		keystroke "20"
		delay 60
	end tell
end idle

so what i want is for it to do this, but allow me to do what i want in the computer. Any ideas?

Model: Macbook
Browser: Safari 525.13
Operating System: Mac OS X (10.5)

Hi,

the target process to send the keystroke to must be frontmost.
A possible workaround is to save the current frontmost application into a variable,
send the keystroke and reactivate the former application


tell application "System Events"
	set frontProc to 1st process whose frontmost is true
	tell process "appname"
		set frontmost to true
		keystroke 20
	end tell
	set frontmost of frontProc to true
end tell

That is perfect. Since its a very small thing to type (and it types so fast) you wont even notice the window change :smiley:
Thanks a lot!

Is there a difference between

tell application "Safari"
	tell application "System Events"
		keystroke "20"
	end
end

and

tell application "System Events"
	tell application "Safari"
		keystroke "20"
	end
end

?

Yes, it is different, actually the second snippet is supposed not to compile because keystroke is not a part of the Safari dictionary.

It’s bad practice anyway to nest application tell blocks.

Nevertheless the keystroke will be sent always to the frontmost application (process)