When you have too many buttons and on clicked takes too long...?

I have an AS project that has lots of buttons, maybe around 80-100. The ones at the bottom of my “if then” on clicked list take several seconds to respond. Is there a better way to deal with this? I need to keep my variables common, so I need all the code to be in one script. Is there another handler I could use to break some of these up, or is there another trick? I am using keyboard commands (a key plus option or command and/or shift) to trigger the buttons in most cases. It’s too bad that buttons can’t be triggered directly with a unique handler. Thanks again for all the help. :slight_smile:

-Evan

I can’t say I’ve ever encountered this problem before, but it may be a difference in coding practice and the types of projects I’ve worked on. None of my projects have had quite that many blocks of code evaluating that many objects. I assume that you’ve got a ton of if/else statements determining what code to execute.

Without seeing your code, I can only offer some basic suggestions (not that I’d want to read through the code for 100 buttons :o). How flexible is your code? Do any of your buttons do things similar enough to reuse one code block for many buttons? It may be easier and cleaner to try to write your code so that it dynamically reuses code that handles similar types of events. This leads me to my main suggestion. Is all of your code for every button contained in the on clicked handler? If so, you could potentially have thousands of lines of code in that one handler. There’s no reason for this type of coding, and could be avoided by using subroutines. If you take all of your distinctly different blocks of code, and create subroutines for each of them, then the code for each action lives in that subroutines rather than in individual action handlers. If the code isn’t in the clicked handler when it’s called, then you don’t have to read through it when evaluating buttons that don’t use it. In your clicked handler, evaluate your buttons as usual, then hand off the action to whatever subroutine executes the code for that action. You still may end up waiting to read through a couple hundred lines of code to get to the execution of the appropriate code, but reading through 200 lines has got to be far faster than reading through 2000 lines.

They can, but that’s something you’ll need to get into obj-c to do.

j

Offhand, if you have to use one file for your variable’s sake, then maybe you could make separate scripts (each containing the script inside a subroutine named, say, “onClicked()”) and load them into the main one.

on clicked theObject
	load script alias ((path to me as Unicode text) & "Contents:Resources:" & name of theObject & ".scpt")
	tell result to onClicked()
end clicked

I actually used to have most of my code up top in the onclicked handler, but I moved it all in to subroutines a while back when I saw how slow it would get. The onclicked handler now only has if/then statements which lead to other subroutines, but it’s still several hundred lines of code due to the number of buttons, and a few extra lines of code per button to prevent one routine from steppind on another. I guess it’s just something I’ll have to live with. Unless there was a different handler I could use, I don’t see any way to speed it up. I don’t see how using an external script would help, since 99% of my code is in subroutines already. Did I misunderstand? Could I use a keyboard down handler for a key with a modifier (option, cntl, shift)?

Either using separate scripts for each on clicked handler, or using something like what I posted, would remove the hundreds of if statements that are slowing you down.

Ah, I see. How quickly do these scripts load? Will I take a performance hit in loading them, or will it be similar to having a subroutine in the main script? I’m trying to weigh my options before I create 100+ scripts. Also, can I call subroutines in the main script from the other scripts, or do I need to put the entire called subroutine in the external script. …and I gather the variables will remain? :slight_smile: Thanks.

-Evan