Lion changes

There’s a page here that covers AppleScript and AppleScriptObjC in Lion: http://macosxautomation.com/lion/applescript.html

Obviously the ability to call C functions is a big change. And the ability to write AppleScriptObjC outside Xcode is good news. There are a couple of other changes, too:

  • A crashing bug when returning structs like NSSize has been fixed (this appeared particularly in one of the window delegate resize methods).

  • The behavior of class has changed. In 10.6, you could only ask for “class of…” with AS classes; with Cocoa classes you got an error, and you had to use |class|() instead. In Lion you can use “class of…” with Cocoa classes, too.

And there’s lots of new Cocoa goodness to play with. NSRegularExpressions, hmmm. On the downside, Lion uses Xcode 4(.1), although there are claims that you can keep using Xcode 3 as long as it was installed before upgrading.

Of course, apps that use the new stuff will need to be marked as 10.7-only.

I’ve been testing some of this out… The form of

tell current application to NSRectFill(aRect)

is working fine, even with my own custom function placed in it’s own .h/.m set… But, I’m unable to find a solution to calling a function that should return data back to me… The following have all failed:

set tString to current application's testFunction()
tell current application to set tString to testFunction()
set tString to current application's testFunction("abc") -- modified function to take a param

All bring error: unrecognized function testFunction. (error -10000)

Any ideas how we can get data back from a C type function?

Dave.

Dave,

Care to send me the sample?

This works for me

	on tester_(sender)

	beep  -- this does not work see below
		set tRect to myTestFunction_()
		log tRect
	end tester_
	
	on myTestFunction_()
set tRect to current application's NSMakeRect(10,10,100,100)
return tRect
	end myTestFunction_

tRect is returned as :

the beep above does not work it returns the following error:

doesn’t understand the «event sysobeep» message. (error -1708)
current application’s NSBeep() does work

In addition how can you declare NSMakeRect so you do not have to keep typing:

current application’s

All the best

Terry

I don’t see any way.

Interesting about beep, too, because it works in a Cocoa applet saved from AppleScript Editor or AppleScriptObjC Explorer.

You can also use:

tell current application to beep

It’s a bit much when you have to work hard just to beep, though…

Hi,

beep used to work.

Do you know when we will get some documentation that informs us properly about all the changes?

I know, it’s really to much to ask :wink:

All the best

Terry

I imagine official release notes will be out soon. But I don’t expect to see much more detail than above.

What I’m talking about, Terry, is custom C-Style functions:

NSString* myFunction (NSString* aString)
{
    return [aString uppercaseString];
}

and calling it with:


on testButton1Click_(sender)
        set aString to "Test String 1"
        set bString to current application's myFunction(aString)
        -- RETURNS: *** -[MyAppAppDelegate testButton1Click:]: unrecognized function myFunction. (error -10000)
        log bstring -- not reached
end testButton1Click_


Now, I’m having trouble even reproducing the one that worked which returned void. I’ll see if I can re-create the working version, then send you one, Shane.

I wonder if it’s limited to functions defined in Cocoa. The problem I see is that without an equivalent of an #include statement, a script isn’t going to know where to find a particular function. Presumably “current application’s” has some search scope, and I wonder if it just looks up some table of known functions.

But I’m also a bit puzzled about why you would want to do this, rather than use a method. The only reason it’s normally done in Objective-C is for speed, which is irrelevant here, and generally also when using C types rather than classes, also irrelevant to ASObjC.

Oh. It’s not that I’m so dead set on needing custom functions. When working with pure Obj-C, I tend to write some wrappers as functions if I’m going to be calling on them a lot.

I do tend to mix the two (Obj-C and ASOC) now and then if I figure I’m going to be doing any AppleScript calls outside of the app, and sometimes I jump over to AS for certain text manipulations. I’m just looking forward to see what can be done from within ASOC with functions. That particular example I gave was just a simple “whip something up” for a test, and not a need for returning uppercaseString from a function.

AppleScriptObjC knows about these functions because they are defined in the BridgeSupport xml data file that is included in the Cocoa framework bundle. Class information can generally be obtained dynamically but info regarding C structures, functions, enums, constants etc cannot. The BridgeSupport file defines the names and calling conventions for these C language elements.

You can produce a BridgeSupport file for any third party framework using gen_bridge_metadata

Ah, thank you very much, Jonathan. That explains a few things!

Hi Shane,

First, thank you for your four-part asobjc tutorials. I’m still having trouble getting bindings to work in xcode 4, but without your tutorials, I’d barely even have gotten started with it. I just upgraded to lion, and xcode 4.1 in hopes that the funny bindings behavior I see would be better. Sadly it’s worse. In fact, your part2 and part4 asobjc tutorial codes do not run at all under lion and xcode-4.1 (or perhaps I’ve made a setup error). These are your examples that read/write/modify a booklist. The messages I get are shown below. Any ideas on how to repair them would be greatly appreciated.

cheers,
Carl

------- snipped message output------

GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Fri Jul 1 10:50:06 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “x86_64-apple-darwin”.tty /dev/ttys000
[Switching to process 3644 thread 0x0]
2011-07-31 21:18:29.200 PartFour[3644:707] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found. Did find:
/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
PartFour: OpenScripting.framework - scripting addition “/Library/ScriptingAdditions/Adobe Unit Types.osax” declares no loadable handlers.
2011-07-31 21:18:29.317 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 1 into type boolean. (error -1700)
2011-07-31 21:18:29.319 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 0 into type boolean. (error -1700)
2011-07-31 21:18:29.320 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 0 into type boolean. (error -1700)
2011-07-31 21:18:29.322 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 1 into type boolean. (error -1700)
.
.
snip (many more lines like this)

2011-07-31 21:18:29.323 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 0 into type boolean. (error -1700)
2011-07-31 21:18:29.324 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 0 into type boolean. (error -1700)
2011-07-31 21:18:38.612 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 0 into type boolean. (error -1700)
2011-07-31 21:18:38.613 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 0 into type boolean. (error -1700)
2011-07-31 21:18:38.614 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 1 into type boolean. (error -1700)
2011-07-31 21:18:38.615 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 0 into type boolean. (error -1700)
2011-07-31 21:18:38.617 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 0 into type boolean. (error -1700)
2011-07-31 21:18:38.618 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 1 into type boolean. (error -1700)
2011-07-31 21:18:38.626 PartFour[3644:707] *** -[PartFourAppDelegate tableView:objectValueForTableColumn:row:]: Can’t make 0 into type boolean. (error -1700)
Program ended with exit code: 0

Thanks for the thanks, but you have the wrong person – I think you’re referring to Craig Williams’s tutorials.

Not that that stops us from trying to solve your problem, but I’m a bit confused when you refer to bindings, because Craig’s examples don’t use bindings.

We need some more information, like what code is running when you get those errors.

Hi,

Ahh, sorry, wrong person. Yes, you’re right, it’s Craig Williams. Well, the tutorial I’m referring to is here:
http://macscripter.net/viewtopic.php?id=30313 . It’s a simple booklist, and now that I’ve upgraded to lion and xcode 4.1, I can’t even get it to run straight out of the box.

I was hoping someone else on Lion/xcode4.1 could have a go at it and see if it runs for them. Perhaps there’s something wrong with my install.

I downloaded this tutorial because I was having a terrible time trying to do what I think ought to be the hello-world equivalent of bindings: make a one-entry window that simply keeps the text in the window in sync with the code (i.e. a user could enter some text, the code could then append, say, “hello” to it, and that change made by the code would appear back in the window. My (very limited) understanding of bindings makes me think this ought to be easy.

My plan was to take that part-2 tutorial and modify it so that did the above. But sadly, it no longer runs at all.

Thanks again for any thoughts on this. This forum is just plain fantastic, though it doesn’t reflect too well on apple that they aren’t even close to the leading source of documentation for their own code.

cheers,
carl

P.S. I’ve added what I think is relevant system info below

Model: macbook pro 15-inch, Mid 2010 Mac OS X Lion 10.7 (11A511)
AppleScript: xcode Version 4.1 (4B110)
Browser: Chrome 12.0.742.122
Operating System: Other

carlcasca -

Yes, I’m having the exact same problem.

ok, thanks for letting me know. I posted over on the original topic so that perhaps Craig will see it.

The problem is in the script – I’m not sure why it ever worked. This line:

	if ident's isEqualToString_("theStatus") then

should be like this:

	if (ident's isEqualToString_("theStatus")) as boolean then

Whenever you get a result from a Cocoa call, you need to coerce it to an AppleScript class before you can use it (with missing value arguably the exception).

I see. Thank you for your help!