Hi guys,
As it proved too complicated to teach my wife to telnet, I ended up writing this script to eject my disks and shutdown my NASLite server, I thought maybe other macusers might be interested.
For beginners like me, here's what you do:
- create a new, blank script in Script Editor (that's in Applications/Applescript)
- copy the code into it and save as an application or a script (named for instance "Close Server", as it is very pragmatically called in our computer)
If you save as a script, place it in the ~/library/scripts folder (you might have to create this folder if it isn't there). You then open Applescript Utility and tick "Show Script Menu in menu bar" to, erm, access the script from the script menu in your menu bar. Your call really.
Now please beware this is my
first script. I do not pretend to know the first thing about error handling etc. It works on my setup, but extreme caution should be the norm as it is, I repeat, my very first script (took me three hours to complete). You use this at your own risk. So any improvements (such as afore mentioned error handling) are very much welcome.
A few comments:
- I have 3 disks in my NASLite. I have therefore used the "repeat with i from 1 to
3", but you should change that last value to the number of disks you have
- You will need to replace "nas" by the password you use to telnet in your server (but still in double quotes), and change the local IP
- I have included a "delay 5" (5 seconds delay) as it sometimes takes a few seconds for the server to reply to the telnet command. If you get error messages or the terminal mashes up the admin and password entries, try a bigger value here (or clean your system with Doktor Kleanor, worked wonders on mine)
Code:
if button returned of (display dialog "Eject disks & shutdown NASLite server?" buttons {"Yes", "No"} default button 2 with icon caution with title "Close Server") is "yes" then
set MyUsrName to "admin" as string
set MyPass to "nas" as string
tell application "Finder"
repeat with i from 1 to 3
set MyDisk to "Disk-" & i as string
if disk MyDisk exists then
eject disk MyDisk
end if
end repeat
end tell
tell application "Terminal"
activate
tell application "Finder" to set visible of process "Terminal" to false
do script "telnet 192.168.0.20" in front window
delay 5
do script MyUsrName in front window
do script MyPass in front window
do script "8" in front window
do script "y" in front window
delay 2
quit
end tell
end if
[edit] I have just bought an AppleScript book
I'll probably post improvements myself as I learn, I'm thinking first of a dialog to confirm actions, and maybe later script an entire interface to replace the terminal.