Frog Blast the JavaScriptCore
Two of my favorite features in Opacity-Calculation Variables and Math Anywhere-are powered by the same technology (that you can probably guess from the title). What’s interesting is that they were originally powered by a different one.
First, a little background. Calculation Variables are a type of variable in Opacity that take a general expression and evaluate that expression to get its value. So you could create one with the expression “resolution < 2″ and bind a layer’s visibility to it, and the layer would only be visible at resolutions less than two. Expressions can also evaluate to numbers, text, or even colors.
Math Anywhere lets you type a math equation into any numeric field to have it evaluated for you. You can even type things like cos(45) to be evaluated. What I love about this feature is how powerful it is, but with no extra UI to get in the way.
I didn’t want to write my own math parsing system for these features, so I tried to find an existing technology to use. I originally tried used AppleScript (more specifically, NSAppleScript). Unfortunately, this had a number of problems. It was slow (for fast AppleScript evaluation, ScriptingBridge is great, but it’s for compiled code, which wouldn’t work here). It was a little too powerful (users could enter ANY AppleScript they wanted, which could allow for some weird and potentially vulnerable stuff). But the biggest problem was that NSAppleScript only works on the main thread. This is a problem in Opacity because things like factories use NSOperations, so can run in background threads. To get calculation variables to work, I had to make blocking calls on the main thread, which was messy and made the threading a lot less effective.
Then, I found out about JavaScriptCore, a new framework in Leopard (it existed before in WebKit, but is now available as a nice standalone framework). JavaScriptCore solves all of the problems I had with NSAppleScript. It can be run from other threads (with, of course, the usual multi-threading precautions). It doesn’t allow access to any objects or data other than what you expressly give it. And it’s very fast (as anyone who’s used Safari should know).
So take a look at JavaScriptCore-it’s fast and useful for more than just scripting.