I don't know any way to get the second frontmost application or window with AppleScript, apart from something like this:
delay 0.3 -- time to release modifier keys if the script is run with a shortcut
tell application "System Events"
set p to process 1 where it is frontmost
set visible of p to false
delay 0.01
key code 121 -- 121 = page down, 116 = page up
set frontmost of p to true
end tell
It makes the hidden and activated windows flash though.
With browsers you could use window.scrollBy():
tell application "Safari" to tell document 1
do JavaScript "window.scrollBy(0,(window.innerHeight-20))"
end tell
tell application "Google Chrome" to tell active tab of window 1
execute javascript "window.scrollBy(0,(window.innerHeight-20))"
end tell
Another option would be to simulate pressing ⌃F4, page up or page down, and ⌃⇧F4 by for example using KeyRemap4MacBook:
<autogen>__KeyToKey__ KeyCode::CURSOR_UP, ModifierFlag::FN | ModifierFlag::CONTROL_L | ModifierFlag::NONE, KeyCode::F4, ModifierFlag::CONTROL_L, KeyCode::PAGEUP, KeyCode::F4, ModifierFlag::CONTROL_L | ModifierFlag::SHIFT_L</autogen>
<autogen>__KeyToKey__ KeyCode::CURSOR_DOWN, ModifierFlag::FN | ModifierFlag::CONTROL_L | ModifierFlag::NONE, KeyCode::F4, ModifierFlag::CONTROL_L, KeyCode::PAGEDOWN, KeyCode::F4, ModifierFlag::CONTROL_L | ModifierFlag::SHIFT_L</autogen>
It also causes a visual glitch, and ⌃F4 sometimes selects windows from hidden applications.
Best Answer
You'll find that shortcut in
System Preferences > Keyboard > Shortcuts > (left side of window:) Keyboard > Focus on next window
.With my (German) keyboard default is
Cmd-<
, but that may be different with other system languages. Just look there!BTW: I changed this shortcut to a more convenient
Ctrl-<
which (on my keyboard) is easier to reach.