How to send an osascript command to a Mac via SSH - Dealing with single quotes

I know how to send a command via SSH. For example, I have an old Canon scanner hanging off of a Raspberry Pi. To initiate a scan I can do this:

ssh pi@raspsky 'scanimage -x 105 -y 149 --resolution 300 > scan.ppm'

However, now I want to send a command from the Pi to the Mac. The command contains single quotes:

osascript -e 'display notification "Image capture failed!"'

But, I need to put single quotes around the entire command. I don't know how to do that. I've tried escaping the single quotes, but that doesn't work.


Here is an example I tested: zsh -c 'osascript -e "display dialog \"hi\""'

osascript -e doesn't need single-quotes, and it works fine with double-quotes. Osascript also has good escaping, so I escaped the quotes inside osascript and used single-quotes for the enclosing command. For you it would be:

ssh <username>@<Mac-host> 'osascript -e "display notification \"Image capture failed!\"'

Testing it on my pi, it works. If you want to make a single command, I'd recommend creating a shell script which contains

scanimage -x 105 -y 149 --resolution 300 > scan.ppm || ssh <username>@<Mac-host> 'osascript -e "display notification \"Image capture failed!\"'

and running that via SSH on your Mac, as I don't think this would work if you put this inside another set of '', which would occur if you send this entire command over ssh.