TCP/UDP communication with AppleScript

First, a big thank you to everyone who makes this site such a great resource! I’ve been using AppleScript since 2008 and haven’t had to post a question yet because I always find the answer here after a quick search, but I haven’t found any advice on my question today…

I’ve developed quite a few scripts that allow me to communicate with video projectors over a closed LAN by targeting a faceless background app called XNet, helpfully provided by Jean-Baptiste Le Stang here: http://lestang.org/spip.php?article20

However XNet requires Rosetta to run, so it won’t run on OS X past 10.6, and it hasn’t been updated since 2005, so an update looks unlikely. Also, I’ve just come across a possible bug where XNet won’t pass a null character (ASCII character 0) back to my script.

So it looks like the time has come to move on to another method of opening TCP connections. I need to open a connection, conduct two way communication between my script and the device, then close the connection, so I don’t think targeting netcat in a do shell script statement will work for me.

AppleScript is the only language I currently know. Learning a new language isn’t out of the question, although similarity to AS would be a big plus. What direction should I go - Python, Ruby, start over from scratch in Xcode (a huge undertaking), other?

Hi,

native AppleScript cannot communicate with sockets.
AppleScript needs help with low level socket programming, python, ruby or even Objective-C.
The main drawback is that network communication is always asynchronous (multi threaded) and AppleScript is always synchronous (single threaded).

I’m just working on an application which is able to control a programmable power strip over LAN with AppleScript.
The whole TCP communication is realized with C/Objective-C. I couldn’t find a way closer to AppleScript

Since when? AFAIK it’s always the developer’s choice if he wants to wait for incoming data or not. Just like you would cat an pipe, you’ll wait until there is something send to the pipe or let another process or thread handle it for you.

I think the first step you’ll need to do is to open a connection with telnet to the video projector on the right port. When you have established a connection you need to find out how to communicate with the machine. If you’re able to communicate with the video projector you can write a telnet script which is pretty straightforward.

When you need interaction with the telnet script you can use an expect script. Most people would use the terminal application because it’s easier but here is an example of how I interact on the command line and send the right proper data when needed. Here an example with ssh, to execute a command remotely.

set theHost to "<ssh hostname or ip>"
set theUser to "<ssh user name>"
set thePwd to "<ssh password>"
set cmd to "<bash command to execute on remote machine>"

set expectScript to "set timeout 20
spawn ssh " & theUser & "@" & theHost & space & cmd & "
expect {
	\"Password:\" {
		send \"" & thePwd & "\\r\"
		exp_continue
	}
	\"yes/no?\" {
		send \"yes\\r\"
		exp_continue
	}
	\"password:\" { #to support older Mac OS X systems
		send \"" & thePwd & "\\r\"
		exp_continue
	}
}"

do shell script "expect <<<" & quoted form of expectScript

Is there any chance that your TCP solution using C/Obj-C could be released as a helper app for AppleScript? I’d guess that the communication with your power strip is very similar to my communication with projectors.

I’ve started working through Shane Stanley’s AppleScriptObjC Explored e-book, and I’ve gotten further with this book than I did the last time I tried to teach myself ObjC, but it still looks like it will take me quite some time to learn enough to roll my own solution.

Thank you very much for this info. I have been able to open a telnet connection to various equipment before, but as you said, I did it using Terminal.app, so I wasn’t able to send multiple commands over the same connection. I’ll do some reading and experimentation with telnet/expect, and let you know how it goes.