Possible AppleScript bug

I think I can across an AppleScript bug where I can’t create an NSMutableArray with over 3 items. I’ve created many with 3 or less but this is first time I tried over 3. Can some people try this out in case the problem is just on Mac?

In both cases the 1 line script below shown below crashed both “Script Debugger” and “Script Editor.” They both got the same error in console. They are listed at the end of this post.

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

current application's NSMutableArray's alloc()'s initWithObjects_("First", "Second", "Third", "Fourth")

Console report for ScriptEditor
9/19/16 7:59:26.346 AM com.apple.xpc.launchd[1]: (com.apple.ScriptEditor2.16672[10488]) Service exited due to signal: Segmentation fault: 11

9/19/16 7:59:26.390 AM com.apple.xpc.launchd[1]: (com.apple.ReportCrash[10489]) Endpoint has been activated through legacy launch(3) APIs. Please switch to XPC or bootstrap_check_in(): com.apple.ReportCrash

Console report for Script Debugger
9/19/16 8:01:35.462 AM com.apple.xpc.launchd[1]: (com.latenightsw.ScriptDebugger6.1189152[10492]) Service exited due to signal: Segmentation fault: 11

9/19/16 8:01:35.509 AM com.apple.xpc.launchd[1]: (com.apple.ReportCrash[10499]) Endpoint has been activated through legacy launch(3) APIs. Please switch to XPC or bootstrap_check_in(): com.apple.ReportCrash

Bill

The -[NSMutableArray initWIthObjects:] call expects a nil-terminated list of objects. This variation on your code seems to work:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

current application's NSMutableArray's alloc()'s initWithObjects_("First", "Second", "Third", "Fourth", missing value)

In Everyday AppleScriptObjC, Shane Stanley use

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
set theArray to current application's NSMutableArray's arrayWithArray:{5, 2, 9, 4, 2, 6, 3}

arrayWithArray is a different call from alloc()'s initWithObjects_ and accepts its parameters differently. However, yes, this is probably the better means of creating a pre-initialized NSMutableArray object.

Mark,

Thanks for reminding me about the nil at the end of an array. Originally I just created created an Applescript list and used the arrayWithArray method then I started thinking I would learn more if I just stuck with ASObj-C stuff thinking using AS lists was somehow cheating. But this experience taught me that’s a pretty dumb idea. Now I create arrays with AS lists. The nil problem did mislead me a bit when it worked for 3 and smaller arrays. I guess I was lucky with the stack for smaller arrays. Again thanks. As soon as I read your reply I decided no more of my new enlightened way, and I went back to using AS lists with arrayWithArray.

Bill

I’m not sure even any Objective-C coders use initWithObjects: any more – using @[obj1, obj2...] means the trailing nil is a bit of a fading memory.

I didn’t use an @ sign to create the array. I used the line:
current application’s NSArray’s alloc()'s initWithObjects_(1, 2, missing value)
to create (NSArray) {1, 2}. That was why I used the “alloc” koenigyvan asked about. initWithObjects requires alloc. I don’t know much about Objective-C so I don’t know what the @ you revered to does.

Bill

Whereas once you might write:

NSArray *array = [[NSArray alloc] initWithObjects:obj1, obj2, nil];

The equivalent “modern” Objective-C syntax is:

NSArray *array = @[obj1, obj2];

Apropos of nothing, I’d be surprised if that were true. I use the language the way it was when I learned it (which includes that “old” syntax), save for changes / deprecations that have been forced upon me.

Old habtis die had an’ all that, and I’d imagine most programmers haven’t got time to learn new habits if they don’t need to; there’s always so much stuff you have to learn just to stand still in today’s computing environment, why burden oneself with the unnecessary?

I’ve no time for niceties if my current habits continue to serve (one of the reasons I haven’t made the switch to Swift - seems like an endless succession of new learning curves (1.0, 2.0, 3.0…) just to do what I can already do in “old” old ObjC. I’m sure that won’t always remain true, but while it does, Swift can twiddle…).

That’s true, but programmers are essentially lazy. Look at the typing involved :slight_smile:

It’s even more efficient with dictionaries. And with numbers:

[NSNumber numberWithInteger:4];

vs:

@4;

Exactly what I think about extra typing :sunglasses:

Shane, shame on you!

Have you never heard of code completion… :yum:

I’m finding that adapting to F5 is taking quite some effort…

I never knew about F5 before, but as a touch typist I’m finding it far easier than the esc key. What was a bit of a stretch with the little finger is a much more comfortable (and faster) stroke with the index finger. :+1:t3:

It’s more the muscle memory that’s getting to me :confused: