AppleScript prints out keypresses, doesn't execute them
I've not used it inside a shellscript, only in 'pure' Applescript, but why not just 

tell application "myApp" to activate


..it would prevent the randomness of which particular app was next in line.