In addition to using
ssh to run commands on the remote host, you can use Remote Apple Events, which also have a "say" command, as well as others that may be useful to you, like "display alert".
On the target machine, enable:
System Preferences > Sharing > Remote Apple Events
Then from your machine run the following script (in AppleScript Editor, or via
osascript in Terminal):
tell application "Finder" of machine "eppc://machine-name.local"
It will ask you to authenticate when you compile or run this. If you do so in AppleScript Editor, it will remember the authentication as long as you leave AppleScript Editor open, so you won't have to authenticate each time you run this. (The authentication dialog also has an option to save your credentials on the Keychain to avoid being repeatedly pestered.)
You can do this from the command-line in Terminal with:
osascript -e 'tell application "Finder" of machine "eppc://machine-name.local" to say "Hello"'
How to Display a Message on a Remote Machine
You could also display a message by logging into the remote machine with
ssh† and running this command in the remote shell (this doesn't make use of Remote Apple Events, just Remote Login for
osascript -e 'say "Hello" without waiting until completion' -e 'tell application "System Events"' -e 'activate' -e 'display alert "Hello!"' -e 'end'
say command starts the speech asynchronously, then this brings System Events to the front to display the message while the speech is playing.
† Commands that display UI, like
display alert and
display dialog, are not allowed via Remote Apple Events. To send them to a process on a remote machine, you must first go through