Fastest way to continuously set variable to UI button value

I have an app that continuously sets a variable to the value of a UI button in a different application. I’m using System Events with assistive devices enabled to get the value. The button value in the other app is a position counter, so the value changes very quickly and I need to constantly refresh my variable. Right now I’m using performSelector_withObject_afterDelay_ to create an “on idle” situation in my ASOC app, but I’m wondering if there’s a better way. Could I set up an ObjC class with an NSapplescript loop to set the variable and then just access it somehow from my Applescript class? My app works ok the way I’m doing it now, but it’d be great if the refresh could be even faster, so I just thought I’d see if there is a better way. Thanks so much.

You can do this with the NSDistributedNotificationCenter. I created a small program that simulates a value changing over time (using an NSTimer), and then a setter method that sees that change and posts a notification. Another program listens for that notification, and logs it. The sender:

script NoteSenderAppDelegate
	property parent : class "NSObject"
	property myValue : 0
	property noter : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		set noter to current application's NSDistributedNotificationCenter's defaultCenter()
		current application's NSTimer's scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1, me, "noteSender:", missing value, 1)
	end applicationWillFinishLaunching_
	
	on noteSender_(theTimer)
		setMyValue_(myValue + 1)
	end noteSender_
	
	on setMyValue_(sender)
		set myValue to sender
		set dict to current application's NSDictionary's dictionaryWithObjectsAndKeys_(myValue, "theValue")
		noter's postNotificationName_object_userInfo_deliverImmediately_("valueDidChangeNotification", missing value, dict, 1)
	end setMyValue_
	
end script

The listener:

script RemoteNotificationsAppDelegate
	property parent : class "NSObject"
	
	on applicationWillFinishLaunching_(aNotification)
		set noter to current application's NSDistributedNotificationCenter's defaultCenter()
		noter's addObserver_selector_name_object_(me, "watch:", "valueDidChangeNotification", missing value)
	end applicationWillFinishLaunching_
	
	on watch_(aNotification)
		log aNotification's userInfo's valueForKey_("theValue")
	end watch_
	
end script

Ric

So, the method that I’m using to get the value from the other app is:


tell application "System Events"
tell process targetapp
set theCounter to value of button 2 of tool bar 3 of window 1
end tell
end tell

Do I need to run 2 different ASOC apps to make this work? How do I detect the UI change, keeping in mind the counter can change many times per second. I’m hanging on for dear life with the ObjC stuff, so please excuse my confusion. :slight_smile:

If I’m understanding what you’re asking here, the answer is no. My sender program is just a stand-in for your program with the counter. It looks like from your last post that the information you need to get to your receiving program is the value of button 2 – is that right? How is that button getting updated? It would be best to know that so that I could offer more relevant help.

Ric

After Edit: Actually, I’m a little confused as to which program your counter is in, so my sender program is a stand-in for whichever program has the rapidly changing value that you want to get to your other program.

The application with the rapidly changing button value is a 3rd-party commercially made application, so I don’t have any way to add code to it, I can only read the value via the UI. Does this make more sense, or maybe I’m still missing something? I’m just trying to figure out the best (and with the fastest refresh) way to read that value and be able to work with it in my ASOC app. I’m sorry if I’m being vague, but this is more of a conceptual issue I think. :slight_smile:

Ok, you didn’t make that clear in your original post – You can’t use the method I suggested then. I don’t know of any way that would be better than what you are currently doing.

Ric

AH, sorry I wasn’t clear in my original post. Thanks for the tips though, I’m sure they will come in handy down the road. :slight_smile: