How can you shutdown a computer from within Unity?

I’m creating a simple program for a museum and I want to have a code for the staff to enter to shutdown the computer from within Unity as the physical machine will be hidden away. I’ve tried a number of things but I’m not sure if I’m messing up the syntax or missing the concept of how to use System.Diagnostics.Process.Start(). Here’s my attempts.

    		//shutdown code
    		if (GetComponent(TextMesh).text == "5555"){
    			//attempt 1	
    			System.Diagnostics.Process.Start("osascript -e 'tell app \"System Events\" to shut down'");
    
    			//attempt 2
    			System.Diagnostics.Process.Start("osascript -e 'tell application \"Finder\"' -e 'shut down' -e 'end tell'");
    
    			//attempt 3 - using applescript in same folder
    			System.Diagnostics.Process.Start("osascript ShutDownMac.scpt");
    		}

Any help would be appreciated.

Took a break and hoped I could solve the problem when the rest of the programming/filming was complete but unfortunately no. Here’s a list of my failed attempts.

System.Diagnostics.Process.Start("osascript -e 'tell application "System Events" to shut down'");
System.Diagnostics.Process.Start("osascript -e 'tell application \"System Events\" to shut down'");
System.Diagnostics.Process.Start("sudo shutdown -h now");
System.Diagnostics.Process.Start("osascript -e 'tell application \"Finder\"' -e 'shut down' -e 'end tell'");
System.Diagnostics.Process.Start("osascript ShutDownMac.scpt");
System.Diagnostics.Process.Start("osascript /tmp/ShutDownMac.scpt");

The first line produces an error which is corrected when I add in the backslashes (line 2). With the third line, I’m not sure how to call the sudo line. I also tried putting the actually Applescript in a number of locations with no luck. And yes I’m running the build as admin.

Any suggestions on how to fix my current attempts or a new approach would be greatly appreciated.

You can use StreamingAssets folder to store your applescript:

1.Create folder StreamingAssets in Assets folder,

2.Place the applescript into this folder

3.Call System.Diagnostics.Process.Start("osascript " + Application.streamingAssetsPath + “/ShutDownMac.scpt”);

Hope this helps,
Paul

If you’re on Windows, Windows Reboot and Shutdown is an asset that exactly that.
It’s as simple as calling

WindowsRebootAndShutdown.DoWindowsShutdown();