Re: is Shane's advice being collated?
Re: is Shane's advice being collated?
- Subject: Re: is Shane's advice being collated?
- From: Shane Stanley <email@hidden>
- Date: Fri, 21 Nov 2014 16:01:54 +1100
I just want to set a few facts, as opposed to opinions, correct, in case someone's following along.
On 20 Nov 2014, at 4:22 pm, 2551 <email@hidden> wrote:
>
>> I'm going to assume from that you're talking theoretically, and you haven't done it much, if at all.
>
> Your assumptions are wrong on both counts.
>
> I've had my fair share of fun with NSAppleEventDescriptors, but people don't need to get involved with those or ASObjC just to mix a bit of AppleScript into an otherwise Cocoa-based app. Just running an NSTask and calling osascript would give more or less the same return as an AppleScript editor, hassle-free (for the interested, appended is a method I keep in Xcode's snippet editor for just this purpose).
>
> You lament the lack of interest in ASObjC among scripters, and I'm pointing out why someone like myself - someone who sometimes needs more power than vanilla AS can provide - doesn't have an interest in it.
>
>
>
> --------CODE SNIPPET---------
>
> -(id)callOSATask: (NSString *)OSACmd {
> NSString *reply;
> NSTask *osaTask = [[NSTask alloc]init];
> [osaTask setLaunchPath:@"/usr/bin/osascript"];
> NSArray *osaArgs = [NSArray arrayWithObjects:@"-e", OSACmd, nil];
> [osaTask setArguments:osaArgs];
> NSPipe *replyPipe = [NSPipe pipe];
> [osaTask setStandardOutput:replyPipe];
> [osaTask launch];
> NSData *replyData = [[replyPipe fileHandleForReading] readDataToEndOfFile];
> reply = [[NSString alloc] initWithData:replyData encoding:NSUTF8StringEncoding];
> return reply;
> }
>
> A trivial example of use:
>
> NSString *theClipBoard = [self callOSATask: @"(the clipboard)"];
Let me follow up on this, but first, Phil also wrote:
> try comparing two strings for equivalence in Objective C and in AppleScript, for example. Damned site easier in the latter.
And later:
> When doing comparisons of hard-coded strings against strings returned from methods, AppleScript's "=" handles any null-termination for you automatically, ignoring the terminating character, whereas isEqualToString: doesn't.
>
> That means if you're comparing two strings in Cocoa and you don't know whether one, both or neither might be null-terminated, you have to write extra code to account for that.
Now that last quote is wrong -- it's a complete misunderstanding of what happens with NSStrings. And the one before it is based on the same misunderstanding. That's no big deal -- we all come to wrong conclusions. But the reason for Phil's conclusions seem to have stemmed directly from using the result of the above code-snippet. It turns out that what Phil thought was a null-termination problem was simply a bug in the code: Unix tasks always add a linefeed to the end, and his code wasn't stripping it out (presumably because he was used to "do shell script" doing just that by default). So his comparisons were failing simply because of an extra linefeed, and nothing to do with NSString.
We've all been there; it can easily happen to anyone. He could change the last line to "return [reply substringToIndex:[reply length] - 1];", and the problem would be solved. As long as he is happy to deal only in scripts that return a single string, and he's happy to muck around building any values he wants to pass into the source of the script.
But if he had gone the ASObjC route, his code would have looked something like this:
NSString *theClipBoard = [ASObject nameOfHandler];
And:
on nameOfHandler()
the clipboard
end nameOfHandler
In other words, he would have been able to write his AppleScript as AppleScript in a .applescript file (with, admittedly, a couple of gotchas), and call it as a method from Objective-C in one line. ASObjC would have provided the bridging between AS's text and Cocoa's NSString, and that's all. Much less code and simpler code, fewer places for stuff to go wrong, and much simpler to debug. As a bonus, he could use the same thing and return not just strings but also lists, records and numbers. Time to learn, given he knows Objective-C and AppleScript: maybe 10 minutes.
And in a way, this makes my point. I'm not lamenting the lack of interest in ASObjC just because I think it's useful, I'm lamenting the fact that some people are doing themselves a great dis-service by ruling it out without even having a proper look. That's what I meant in my (admittedly clumsy) earlier reference to being content.
If you're running Yosemite, go back to my last message in the thread about Scripting Alerts, copy-and-paste the code into Script Editor, and run it (with the control key, remember). Then tell me you've never wanted more than one text input in a dialog anyway, that you're quite happy bombarding users with dialog after dialog, and that this sort of thing is clearly no use to you.
--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden