newbie script problem... sending keystrokes to multiple applications

Hi everyone,

I’m trying to write a script that will:

a) be fired by a keyboard shortcut
b) refresh the page of several browsers at once

In order to achieve a) I have installed Fastscripts Lite, so I can call the script no matter which application is currently active.

To achieve b) is a little harder. When I fire the script from the Script Editor it runs fine. When I try and run it via the shortcut, it flicks between the windows, makes a lot of event sounds, but doesn’t actually refresh any of the pages.

Any ideas what might be wrong? Here is the script:

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		keystroke "r" using {command down}
	end tell
end tell

tell application "Firefox" to activate

tell application "System Events"
	tell process "Firefox"
		keystroke "r" using {command down}
	end tell
end tell


tell application "Opera" to activate
tell application "System Events"
	tell process "Opera"
		keystroke "r" using {command down}
	end tell
end tell

Model: Macbook 2007
AppleScript: 1.10.7
Browser: Firefox 3.0.9
Operating System: Mac OS X (10.4)

Hi Tom_x,

That works great, thanks. I’ve actually set all 3 delays to ‘1’ because I plan to have all the browsers open before I use this command.

Just to clarify… why was the original script not working? I would have assumed that each command would be carried out in turn, one after another, and the next one would not be fired until the previous one had completed, no? And even if the next app was ‘activated’, the previous app could finish it’s task in the background, couldn’t it?

Hi,

I prefer controlled delays, which are more effective and more reliable like


activate application "Safari"
tell application "System Events" to tell process "Safari"
	repeat until frontmost is true
		delay 0.1
	end repeat
	keystroke "r" using command down
end tell

but for the purpose of the OP there’s no delay needed at all


property browserProcessList : {"Safari", "Camino", "firefox-bin"}

tell application "System Events"
	repeat with oneBrowser in browserProcessList
		tell process oneBrowser
			set frontmost to true
			keystroke "r" using command down
		end tell
	end repeat
end tell