Idle and CPU usage

When this script is in idle handle, it takes about 2-7 % of CPU when Safari is not front. I would like to get that percentage to lower.

It should check once a second or faster whether Safari is frontmost app.

global safaripath
on run
	set safaripath to path to application "Safari" as string
	mainloop()
end run

on mainloop()
	beep
end mainloop

on idle
	if (path to «constant afdregfp» as Unicode text) = safaripath then mainloop()
	return 1
end idle

You can marginally improve the text handling performance by making ‘safaripath’ Unicode text too (so that no coercion’s needed for the comparison in the idle handler) and by making the comparison case sensitive (so that it doesn’t have to check for possible case differences).

global safaripath
on run
	set safaripath to path to application "Safari" as Unicode text -- Not 'as string'. Probably no difference in Leopard.
	mainloop()
end run

on mainloop()
	beep
end mainloop

on idle
	considering case
		if (path to frontmost application as Unicode text) = safaripath then mainloop()
	end considering
	return 1
end idle