CoolTerm Script for Periodic Polling

If you have specific questions or problems with any of my Freeware applications, post them here.
Post Reply
erikj
Posts: 2
Joined: Mon Jun 22, 2020 3:10 pm

CoolTerm Script for Periodic Polling

Post by erikj »

Howdy Roger,

I'm new to Mac and am still learning AppleScript. I have some CS background but I'm trying to get ahead of a router issue.

I have a text file for some command lines to the router. I can successfully send the te4xt file (CT_Debug_Script.txt) to the serial port successfully to pull data that I'm logging. What I'd like to do is have CoolTerm do this automatically, say every 5 to to minutes.

I was a bit puzzled on the SendTextFile syntax, so I only have this so far:

Code: Select all

tell apllication "CoolTerm"

	SendTextFile(1, /Users/zyxeltest/Desktop/EX5510 Console Logs/CT_Debug_Script.txt) 1

end tell
I know this isn't right, I probably have to assign local variables to the window like you mentioned here, but I'm also wondering if I can create an empty if loop and set a delay for 300 seconds, for example, and then tell the script to terminate when I close the window.

I've had some experience with Terra Term scripting, but AppleScript is new ti me. Thanks for any help you can provide.
User avatar
roger
Site Admin
Posts: 431
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: CoolTerm Script for Periodic Polling

Post by roger »

Hi,

here is an example that sends a text file:

Code: Select all

tell application "CoolTerm"
	
	set Filepath to "/Users/Shared/textfile.txt"
	
	# Get the ID of the first open window
	set WinID to WindowID (0)
	if WinID < 0 then
		display alert "No open windows"
		return
	end if
	
	# Open the serial port
	if not (Connect WinID) then
		display alert ("Not Connected")
		return
	end if
	
	# Send the file
	set success to SendTextFile {WinID, Filepath}
	if success is not true then
		display alert "Error opening text file."
		return
	end if
	
	# Wait until it's done sending
	repeat until (BytesLeftToSend (WinID)) <= 0
		Poll (WinID)
		delay 0.2
	end repeat
	
	display alert ("Done sending!")
	
end tell
To send the file repeatedly every 5 minutes, you change the script as follows:

Code: Select all

tell application "CoolTerm"
	
	set Filepath to "/Users/Shared/textfile.txt"
	
	# Get the ID of the first open window
	set WinID to WindowID (0)
	if WinID < 0 then
		display alert "No open windows"
		return
	end if
	
	# Open the serial port
	if not (Connect WinID) then
		display alert ("Not Connected")
		return
	end if
	
	repeat
		# Send the file
		set success to SendTextFile {WinID, Filepath}
		if success is not true then
			display alert "Error opening text file."
			return
		end if
		
		# Wait until it's done sending
		repeat until (BytesLeftToSend (WinID)) <= 0
			Poll (WinID)
			delay 0.2
		end repeat
		
		# wait for 5 minutes
		delay 300
	end repeat
	
end tell
erikj
Posts: 2
Joined: Mon Jun 22, 2020 3:10 pm

Re: CoolTerm Script for Periodic Polling

Post by erikj »

Thanks! One of my questions is what the ID was for; I figured it was the ID of the window for multiple instances, but I wasn't sure how to find said ID.

My thanks, this will go a long way towards helping me understand AppleScript and getting this periodic check up and running.
User avatar
roger
Site Admin
Posts: 431
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: CoolTerm Script for Periodic Polling

Post by roger »

The ID is a unique handle that CoolTerm assigns to a terminal window when it is first opened. This handle remains the same for as long as CoolTerm is running. Even if a window is closed, CoolTerm will not reuse the ID of that window as long as the CoolTerm app is running. The handle allows CoolTerm to access a specific terminal window

The ID is not the same as the index of the window. The index is merely the number of the window in the list of open windows. If there are N open windows, the indices of the open window range from 0..N-1, and the numbers are in the order in which the windows were opened. The window with index 0 is always the window that has been opened the longest. If that window closes, then the window that previously had index 1 now has index 0.

Thus, it's important that the first thing every script does is to get the ID for the window it needs to access. If there is only one window, then the index of that window is always 0, and when you know the index of the window you want to access, you can get a handle this way:

Code: Select all

----------------------------------------------------------------------------
WindowID(Index as integer) as integer
----------------------------------------------------------------------------
Returns the ID of the window with the specified index.
Returns -1 if the window doesn't exist.
If you don't know the index of the window, then you can look it up with its name if you know it, like this:

Code: Select all

----------------------------------------------------------------------------
WindowIDfromName(WindowName as String) as integer
----------------------------------------------------------------------------
Returns the ID of the window with the specified name.
Returns -1 if the window doesn't exist.

E.g.:	# Get the ID of the terminal Window named "Untitled_0".
	Set WinID to WindowIDfromName ("Untitled_0")
Here is a simple script you can use to create a list of all open terminal windows:

Code: Select all

	# Display the IDs of all terminal windows
	set winList to {}
	set n to WindowCount
	repeat with i from 0 to n - 1
		set WinID to WindowID (i)
		set WinName to WindowName (i)
		copy ("Index: " & i & ", ID: " & WinID & ", Name: " & WinName) to the end of winList
	end repeat
	winList
I hope this clarifies it.
Post Reply