Hello everyone,
This is my first post as well as my first AppleScript. Please forgive my lack of knowledge! I am trying to write a script that uses a CSV file to provide a list of network devices a user would use telnet or ssh to access and administer. The CSV file has the following contents:
Core1,ssh,1.1.1.1
Core2,telnet,2.2.2.2
Edge1,telnet,3.3.3.3
Edge2,ssh,4.4.4.4
The script seems to work “OK” for a first attempt, but when I run it and hit cancel on the first “choose from list” windows I don’t know how to handle that exception. I have searched this forum and found some threads that will fix my problem but don’t have the knowledge implement them correctly.
Any help that the gurus of this forum can provide would be greatly appreciated. I am sure the script as a whole is pretty poorly written and I would be most grateful for all your inputs and knowledge sharing. If anyone has any books or references that are extremely newbie friendly please pass them on. I have some and they have been helpful but I fear they are not what a newbie like me should start with.
Thanks in advance for any and all help!!!
v/r
Eric
Below is my script:
global gUnixPath, gDeviceName, gClientType, gIPAddress, gAnswer, gbooleanConnect, gCancelByUser
set gbooleanConnect to false --Determines if User confirmed selection of device
set gCancelByUser to false --Determines if User cancelled the Select Device List Box
–BEGIN: FOR DEVELOPMENT ONLY------------------------------------------------------
set gUnixPath to “/Users/emidd/Documents/AppleScriptTextFile.csv”
–END: FOR DEVELOPMENT ONLY------------------------------------------------------
–CHOOSE ONE OR THE OTHER:
–set gUnixPath to (choose file with prompt “Select a Device file to read:” of type {“TEXT”}) - browse for device file
–set gUnixPath to (choose file with prompt “Select a Device file to read:”) - browse for device file
set AppleScript’s text item delimiters to {“”} – set AppleScript’s text delimiter to the DEFAULT
–BEGIN: Select/Confirmation of Selected Device–
repeat while not gbooleanConnect
selectdevice(gUnixPath) -- Select the device you want to connect too
if gCancelByUser then
exit repeat
end if
confirmSelection(gDeviceName, gClientType, gIPAddress) -- Confirm the device to connect too
end repeat
–END: Select/Confirmation of Selected Device–
tell application “System Events” to set terminalOn to (exists process “Terminal”)
if gAnswer = “Connect” then
if (terminalOn) then -- If terminal process exists, open a new window
tell application "Terminal"
set number of rows of window 1 to 50
set number of columns of window 1 to 100
set background color of window 1 to "black"
set normal text color of window 1 to "green"
set custom title of window 1 to gDeviceName
if gClientType = "telnet" then
do script gClientType & " " & gIPAddress
else if gClientType = "ssh" then
do script gClientType & " -l eric " & gIPAddress
else
display dialog "Not able to determine client type (ssh/telnet)" with title "!!!ERROR!!!" with icon 2
end if
end tell
else
tell application "Terminal" -- If terminal process DOES NOT exist, open in FRONT window
set number of rows of window 1 to 50
set number of columns of window 1 to 100
set background color of window 1 to "black"
set normal text color of window 1 to "green"
set custom title of window 1 to gDeviceName
if gClientType = "telnet" then
do script gClientType & " " & gIPAddress in front window
else if gClientType = "ssh" then
do script gClientType & " -l eric " & gIPAddress in front window
else
display dialog "Not able to determine client type (ssh/telnet)" with title "!!!ERROR!!!" with icon 2
end if
activate
end tell
end if
else if gAnswer = “Reselect” then
selectdevice(gUnixPath)
end if
–#################
–Functions below this point–
–#################
on confirmSelection(device, client, ipAddr)
set question to display dialog "Do you want to use " & gClientType & " to connect to device " & gDeviceName & " at IP Address " & gIPAddress & "." with icon 2 buttons {"Reselect", "Connect"} default button 2
set gAnswer to button returned of question
if gAnswer = "Connect" then
set gbooleanConnect to true
end if
end confirmSelection
on selectdevice(filepath)
set devices to (do shell script "/bin/cat " & POSIX path of filepath) -- get list of devices
set theDeviceList to (paragraphs of devices) as list -- create a list of devices by line (paragraph)
set theDevice to (choose from list theDeviceList with title "Device Selection" OK button name "OK" cancel button name "Cancel" with prompt "Choose a device:")
if the result is not false then
set theText to theDevice as text -- create a text variable of "," delimited values
set AppleScript's text item delimiters to {","} -- set AppleScript's text delimiter to the ","
set {gDeviceName, gClientType, gIPAddress} to text items of theText -- set the attributes of the selected device
--display dialog gDeviceName with title "Device Name is:" with icon 2
--display dialog gIPAddress with title "IP Address is:" with icon 2
--display dialog gClientType with title "Client Type is:" with icon 2
return gDeviceName
return gClientType
return gIPAddress
return
else
set gCancelByUser to true
end if
end selectdevice