Your last command,
osascript -e ' tell app "System Events" to display "Hello world"'
will be executed only if you exit the remote ssh terminal. In this way you can not achieve what you are trying to do.
Hopefully there is expect script. You can work with the following script,
#!/bin/sh
expect <<'END'
set timeout 10
set login "your_username"
set addr "remote_ip"
set pw "your_password" # skip this line if you have password less login
spawn ssh $login@$addr
expect "$login@$addr's password:" # skip this line if you have password less login
send -- "$pw\r" # skip this line if you have password less login
expect "~" # use your prompt here, I have ~, replace ~ with your prompt
send -- "ls\r" # replace ls with your command
expect "~"
send -- "exit\r"
expect eof
END
See How to execute sudo commands with Expect & send commands in bash script?