You can change the command target mid-command using the "Perform a Window Function", by having it switch focus to the target window (which is necessary anyway, in order for the target window to receive normal keyboard input) and enabling the "Set command to target this window" option.