Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Hotel application

Good Day Everyone,


this place seems to be the best online for asking help related to applescript that I am trying to create. I am sure that this will be easy task for you guys.

I need search option to compare user input and add it into variable. It does not require database, it has to search from simple text file of any format.

it will be used to reboot devices in hotel operations. I have part of code that works, unlucky i dont have enough time to search try and complete it therefore I need your help.


Here is part of working code I have, all what it does reboots unit 172.25.82.48. It sends 4 commands. Dont be confused "yes" is just some input that I need to send after ssh into machine so i can proceed.

Now prior this i need small window that asks user "enter room number:" when user types that example 1234B program should go to txt file and search for that numbers ip address. txt file will be like this:

1234B - 172.25.80.52

1245A - 172.25.17.54

etc....

after it founds room number that user input it has to extract ip address from written next to it and add it in below part of code admin@172.25.82.48

purpose is to reboot devices and make easy interface for user since all ip addresses of devices are static, and if we encounter some changes we can just update that txt file with new IP's


Thank you all !


property commandString : "ssh admin@172.25.82.48"

property commandString1 : "yes"

property commandString2 : "password"

property commandString3 : "sudo reboot now"

property commandString4 : "password"


tell application "Terminal"

launch

set visible of window 1 to false

if (count of windows) is 1 then

do script commandString in window 1

delay 10

do script commandString1 in window 1

delay 5

do script commandString2 in window 1

delay 5

do script commandString3 in window 1

delay 5

do script commandString4 in window 1

else

set frontmost of window 1 to true

do script commandString in window 1

delay 10

do script commandString1 in window 1

delay 5

do script commandString2 in window 1

delay 5

do script commandString3 in window 1

delay 5

do script commandString4 in window 1

end if

end tell

delay 1

tell application "Terminal"

if (count of windows) is 1 then

quit

else

set frontmost of window 1 to true

end if

end tell

tell application "Finder"

display dialog "Device Rebooted"

end tell

Mac mini, OS X Mountain Lion (10.8.1)

Posted on Mar 23, 2015 7:37 AM

Reply
1 reply

Mar 23, 2015 11:12 AM in response to opusteno

I'll leave aside for now the discussion of scripting Terminal.app in this way... there may or may not be better ways of handing this.


As for your specific request - Prompt the user for a room number, lookup that number in a static text file, extract the corresponding IP address and incorporate that in the Terminal.app script - that's pretty easy. Something like this should work:


-- ask the user for the room number;

set room_number to text returned of (display dialog "Enter room number:" default answer "")


-- lookup the room number in the file:

-- (change the path as needed - the default looks for a file called 'rooms.txt' on the desktop):

-- note this is a shell script, which is hacky, but actually easier than AppleScript for this kind of thing

set room_IP to do shell script "/usr/bin/awk '/^" & room_number & "/ { print $2} ' /Users/<you>/Desktop/rooms.txt"


if room_IP is "" then


-- we didn't find an entry, so cancel out

display dialog "Room number '" & room_number & "' not found" buttons "Cancel" default button 1

endif


--Now we have a room number, and an IP address,

--so incorporate that into the rest of your script:


set commandString to "ssh admin@" & room_IP as text


-- now do your thing:

tell application "Terminal"


-- the rest of your script goes here

end tell


Build a text file that contains the room number and the IP address - it can be tab or space-delimited, it doesn't matter - and make sure the path matches in the 'set room_IP...' line.


There is very little error checking here, apart from making sure that something comes back from the text file - in other words, if the script may fail if the IP address is wrong (or is not even an IP address).

Hotel application

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.