Hotkey to scroll other window without switching to it

keyboardscrollingwindow-manager

Emacs has a nice feature in the C-M-v keybind (scroll-other-window in Emacs Lisp), which causes the other window in a frame with two windows to scroll (I haven't tried it with more than two windows) without switching to the other window. Is there a way to get equivalent functionality with Mac OS X toplevel windows (i.e. non-Emacs windows)?

In particular I'm interested in scrolling Chrome and iTerm windows, but a more general solution would be great. I assume I would have to install a hotkey app of some sort, but I'm not averse to that.

Best Answer

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.