How to detect text change?

This is my first script.

I’m getting a bit of text from an application A, and putting it into application B.

What I’d like to do is have my script lie in wait until the text in A changes, then pop it into B until A is closed.

A “repeat” loop seems sorta intense. A is not my program, so it can’t be changed.

Welcome to MacScripter.

Before anyone can help, however, we’ll have to know several things – What are Applications A & B and are they scriptable for starters. You can tell if they’re scriptable by opening your Script Editor and trying to look at the Dictionaries of A & B. If they have one, they’re scriptable.

I also assume that A is saving a text document and you want all of that text transferred to B, but how? To a B document, B Window, what exactly.

Thanks. Sorry for not being clearer.

A is scriptible, B with UIScripting. A is a ham radio program, and the text in question is a Great Circle heading from my house to a foreign station. B gets that heading and puts it into an edit box, and tells an antenna rotator to point that way. Here’s a working script, but it has to be manually run each time.


on enabledGUIScripting(true)
	tell application "System Events"
		activate -- brings System Events authentication dialog to front
		set UI elements enabled to true
		return UI elements enabled
	end tell
end enabledGUIScripting

tell application "RUMlog"
	set hdg to TxRprt
	set beam to hdg as string
        repeat while length of beam as string < 3
	    set beam to "0" & beam
	end repeat
	activate application "RotorDCU"
	tell application "System Events"
		tell process "RotorDCU"
			--get value of text field 1 of window "RotorDCU"
			--display dialog hdg buttons "OK" default button "OK"
			set value of text field 1 of window "RotorDCU" to beam as string
		end tell
	end tell	
end tell



Hi.

I don’t have either application, so I can’t test this; but it should get you started. It it has be saved and run as a stay-open application.

It does nothing if RUMlog isn’t open or if ‘beam’ hasn’t changed since last checked. It quits if RUMlog was open at the last check but not now.

property checkInterval : 5 -- Number of seconds between checks. Adjust to requirement.

global RUMlogWasOpen, GUIScriptingWasEnabled, previousText

on run
	-- When the script starts up, note if RUMlog is running and GUI Scripting is enabled.
	tell application "System Events"
		set RUMlogWasOpen to (application process "RUMlog" exists)
		set GUIScriptingWasEnabled to (UI elements enabled)
	end tell
	
	-- Enable GUI Scripting if necessary and give the 'previousText' variable an initial value.
	switchGUIScripting(true)
	set previousText to ""
end run

on idle
	-- Each time the script's polled by the system, check that RUMlog's open.
	tell application "System Events" to set RUMlogIsOpen to (application process "RUMlog" exists)
	
	if (RUMlogIsOpen) then
		-- If RUMlog's open, update the "was open" flag to indicate that it's been open while the script's been running.
		set RUMlogWasOpen to true
		
		-- Get the latest value for 'beam'.
		tell application "RUMlog"
			set hdg to TxRprt
			set beam to hdg as string
		end tell
		if ((count beam) < 3) then set beam to text -3 thru -1 of ("000" & beam)
		
		-- If it's changed since the last check, enter it into RotorDCU and keep it for later checks.
		if (beam is not previousText) then
			tell application "RotorDCU" to activate
			
			tell application "System Events"
				tell application process "RotorDCU"
					set value of text field 1 of window "RotorDCU" to beam
				end tell
			end tell
			
			set previousText to beam
		end if
		
		-- Request the next 'idle' event in 'checkInterval' seconds' time.
		return checkInterval
	else if (RUMlogWasOpen) then
		-- RUMlog was open at the last check, but isn't now. Tell this script applet to quit.
		quit
		-- It'll actually quit on the next idle event, so request a short interval.
		return 1
	end if
end idle

-- Unless GUI Scripting was already on when the script started to run, turn it on or off as per the parameter.
on switchGUIScripting(onOff) -- 'onOff' is 'true' for on, 'false' for off.
	if (not GUIScriptingWasEnabled) then
		tell application "System Events"
			activate
			set UI elements enabled to (onOff)
		end tell
	end if
end switchGUIScripting

-- When this applet's told to quit, restore the previous GUI Scripting state before it does.
on quit
	switchGUIScripting(false)
	
	continue quit
end quit

YES! It works, and a BIG thanks. :smiley:

The script does not open RotorDCU if it’s not open, and gives the message “An error of type -10660 has occurred”. I can’t find a reference for that number. No error message if both programs are open before starting the script.

The ‘tell application “RotorDCU” to activate’ line should do that (and does with another app on my machine). :confused:

Dunno. It’s probably System Events complaining about some aspect of not being able to set the value of the nonexistent text field of the nonexistent window of the nonexistent “RotorDCU” process.

Presumably you’re talking about the script applet’s icon. Again, all I can say is that on my machine, the script applet quits when another app I set it to watch quits, even when it has to turn off GUI Scripting in the process. I’m running Snow Leopard. Maybe something’s different on your system. :confused:

Yes. Just after I wrote that, I ran the whole thing, and it quit very nicely. :rolleyes:I’m on Mountain Lion, Snow Leopard was the prettiest of the cats.

Again, a huge thanks!