Make apps save and close via command line on Mac

Had a unique situation recently where I needed to save and close a Microsoft Excel spreadsheet on my Mac from remote.  I did not have ssh enabled, and did not have screen sharing enabled, so I was not able to remotely control the system, or SSH into it to enable remote control.  I did previously enable remote administration though.

With remote admin enabled, I’m able to send unix commands to the system, but obviously I can’t send any interactive commands like a sudo to switch to root and enable remote desktop or SSH as those commands require root/admin access.

Since I was able to send command line commands, I was able to use the Apple “Remote Desktop” management application to send an AppleScript command to instruct Excel to save and quit.  Here’s the command:

osascript -e 'tell application "EXCEL"
 activate
 close active workbook with saving
end tell'

As far as I can tell, you should be able to use permutations of this to close other uniquely named apps, with saving of the data.  If you don’t care about saving, you can simply run the following with any app to make it exit without prompting:

osascript -e 'quit application "EXCEL" saving no'

Leave a Reply

Your email address will not be published. Required fields are marked *