This script activates automatic windows grouping. The grouping is only done if there is already a proper group on the current desktop. A window is never moved to another desktop. Instead, a new group is created in such a case. KWin's setting "Automatically group similar windows" is not relevant anymore, if this script is active.
This script is created for KDE 4.8 and might work with slightly older versions. It will probably not work out-of-the-box with KDE 4.9 due to changes in the scripting API:
To install the script, copy it to ~/kde/share/apps/kwin/scripts/. Set the execute bit and restart KWin.