AppleScript to start VM with additional notifications

Hi there,

first of all - I’m no dev or scripter. Additionally I’m not an english nativ speaker.
So please be patient with me :slight_smile:

Background:
I’ve got an unRAID server running a gaming VM. Due to daily demand I was looking for a easy and efficient way to start the VM and get rid of the “start safari - open unRAID web gui - logging in - start vm”-Way :smiley:

After some googling I was able to “build” (just crawling infos via Google :slight_smile: ) a simple start up script with the script editor on my mac.

And here it is …

do shell script "/usr/bin/ssh root@192.168.1.248 virsh start Windows_10q35_5_own_vBIOS"

But after some days of using I was thinking about to make it even better - with some macOS notifications to show up when the script was started and when (or if) the VM started successfully.
But that was also the point where I need to accept, that googling can’t help me anymore due to the lack of knowledge :expressionless:

I did find some terminal commands that seems to be the solution - but I can’t get it into a running script. And with “I can’t” I mean, that

  1. I cant translate the command, which runs perfectly within terminal, into my “script” above (script editor shows me an error message but I dont know how to solve)
  2. I can’t combine the following commandsin a logic that a script needs to work

In my logic the script should run like the following (wrote & thought like a human - not as a dev :stuck_out_tongue: )

START action → “ssh root@192.168.1.248 virsh start Windows_10q35_5_own_vBIOS”
THEN notification → osascript -e ‘display notification “Script started” with title “Gaming VM”’
IF result = “Domain Windows_10q35_5_own_vBIOS started” (that is the feedback I get the VM was started successfully)
THEN Notification → osascript -e ‘display notification “VM started successfully” with title “Gaming VM”’
IF result = “whatever the terminal outputs when it cant start the VM”
THEN notification → osascript -e ‘display notification “VM couldn’t started” with title “Gaming VM”’

Thanks in advance for any help regarding my challenge.
Have a nice day

Model: MacMini 2018 - i7 6-Core
Browser: Safari 605.1.15
Operating System: macOS 10.14

You yourself answered your own question. Only, I think terminal and osascript are not needed here:


try
	do shell script "/usr/bin/ssh root@192.168.1.248 virsh start Windows_10q35_5_own_vBIOS"
	if result = "Domain Windows_10q35_5_own_vBIOS started
" then
		display notification "VM started successfully" with title "Gaming VM"
	else
		display notification "VM couldn't started" with title "Gaming VM"
	end if
on error
	display notification "VM couldn't started" with title "Gaming VM"
end try

@KniazidisR - thank you very much. I’m happy that I have understand a little bit.
But you’re awesome… this works out of the box.

But there seems to be a problem with the notification.
Despite the script startet my VM successfully I get the notificaten “VM couldn’t started” and I dont know why - because it actually works :smiley:

Another question just to understand it correctly - the second notification

on error
	display notification "VM couldn't started" with title "Gaming VM"
end try

represents the failure of the script itself - not the failure of the start of the VM - correct?

Thank you very much for your assistance

Update: I change the second notification to see what notification doesn’t work.
It’s the first - it seems that there is no “result” when running this script.
But I got a result, when just running

"/usr/bin/ssh root@192.168.1.248 virsh start Windows_10q35_5_own_vBIOS"

as you can see here in my screenshot (“Ergebnis” is = “Result” - but german :slight_smile: )

But when I run your script, there is no result - only “Events” and “Replies” (“Antworten” in german)


Update: Solved — the “if” condition got a typo

it must be

"Domain Windows_10q35_5_own_vBIOS started
"

not

"Domain Windows_10q35_5_own_vBIOS started"