Hey guys, hope someone here can help as I dont know where to look.
Im trying to create a script to give me a dialog to choose which raspberry pi i want to connect to but for the life of me i cant figure out what im doing wrong.
Im only just starting learning to code, so forgive me if its blatantly obvious but here is my code up to now…
on ActivateTerminal()
tell application "Terminal"
activate
do script command
end tell
end ActivateTerminal
on SetChoice()
set choice to choose from list {"RPi 1", "RPi 2", "RPi 4", "RPi 5", "RPi 6", "Server"}
if choice is "RPi 1" then
set logon to "root"
set sshhost to "192.168.0.200"
end if
if choice is "RPi 2" then
set logon to "root"
set sshhost to "192.168.1.2"
end if
set command to "ssh " & logon & "@" & sshhost
end SetChoice
on run
SetChoice()
ActivateTerminal()
end run
Whenever i run it, it just says “variable logon not defined”
Cheers in advance
choose from list {“RPi 1”, “RPi 2”, “RPi 4”, “RPi 5”, “RPi 6”, “Server”}
will return a list. (i.e. {“RPi 1”} not a string such as “RPi 1”)
you need to change this line to
set choice to item 1 of (choose from list {"RPi 1", "RPi 2", "RPi 4", "RPi 5", "RPi 6", "Server"})
or coerce to text like this
set choice to (choose from list {"RPi 1", "RPi 2", "RPi 4", "RPi 5", "RPi 6", "Server"}) as text
Thank you for your response robertfern, but substituting your line into the script results in the same error…
Ok so substituting your line into the script and simplifying it has made it work, thanks again.
set choice to item 1 of (choose from list {"RPi 1", "RPi 2", "RPi 4", "RPi 5", "RPi 6", "Server"})
if choice is "RPi 1" then
set logon to "root"
set sshhost to "192.168.0.200"
end if
if choice is "RPi 2" then
set logon to "root"
set sshhost to "192.168.1.2"
end if
set command to "ssh " & logon & "@" & sshhost
tell application "Terminal"
activate
do script command
end tell
For anyone whos interested heres the final script…
set logon to "pi"
set choice to item 1 of (choose from list {"RPi 1", "RPi 2", "RPi 3", "RPi 4", "RPi 5", "RPi 6", "Server"})
if choice is "RPi 1" then
set logon to "root"
set sshhost to "192.168.0.200"
else if choice is "RPi 2" then
set logon to "root"
set sshhost to "192.168.1.2"
else if choice is "RPi 3" then
set sshhost to "192.168.1.3"
else if choice is "RPi 4" then
set sshhost to "192.168.1.4"
else if choice is "RPi 5" then
set sshhost to "192.168.1.5"
else if choice is "RPi 6" then
set logon to "root"
set sshhost to "192.168.0.129"
else if choice is "Server" then
set precommand to "wakeonlan E0:CB:4E:DE:D0:87"
set logon to "iainstott"
set sshhost to "192.168.0.110"
end if
set command to "ssh " & logon & "@" & sshhost
tell application "Terminal"
reopen
activate
if choice is "Server" then
do script precommand in window 1
delay 5
do script command in window 1
else
do script command in window 1
end if
end tell
This config file is used for just this purpose. You might want to check it out as well.