Re: NSAppleScript - what is going on here?

2009-03-23 Thread Michael Tsai

On Mar 23, 2009, at 12:19 AM, Adam R. Maxwell wrote:

Using the MDItem C API had none of the AppleScript/Apple event  
problems, and was a small fraction of the code.


On the other hand, MDItem will probably fail if the file is on a  
volume for which Spotlight is disabled or its index isn't up-to-date.


--Michael


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSAppleScript - what is going on here?

2009-03-23 Thread Adam R. Maxwell


On Mar 23, 2009, at 6:28 AM, Michael Tsai wrote:


On Mar 23, 2009, at 12:19 AM, Adam R. Maxwell wrote:

Using the MDItem C API had none of the AppleScript/Apple event  
problems, and was a small fraction of the code.


On the other hand, MDItem will probably fail if the file is on a  
volume for which Spotlight is disabled or its index isn't up-to-date.


Thanks for pointing that out; I just wrote a quick test and it does  
fail on my iDisk.  Good thing I don't need it now that NSFileManager  
doesn't strip Finder comments...


If anyone wants the Apple event version, let me know and I'll dig it  
up from svn history; it's an order of magnitude faster than using  
AppleScript.





smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: NSAppleScript - what is going on here?

2009-03-22 Thread Matt Neuburg
On Sat, 21 Mar 2009 12:38:11 -0500, Steve Cronin steve_cro...@mac.com
said:
Folks;

I'm trying to get a string value back from a simple AppleScript in
Cocoa:

NSDictionary *errorDict= nil;
NSAppleScript *appleScriptObject = [[NSAppleScript alloc]
initWithSource:theScript]; 
NSAppleEventDescriptor *eventDescriptor = [appleScriptObject
executeAndReturnError: errorDict];
[appleScriptObject release]; 
if (([eventDescriptor descriptorType])  (errorDict==nil)) {
 return [self stringFromAppleEventDescriptor:eventDescriptor];  //my
own method that checks descriptorType and returns stringValue
} else {
 NSLog(@%@,[errorDict objectForKey:@NSAppleScriptErrorMessage]);
 return nil;
}

The 'theScript' is a valid script that executes flawlessly in 'Script
Editor':
tell application Finder
 try
  comment of file (/Users/steve/ as POSIX file)
 on error
  return Error
 end try
end tell

The problem is that the above errors out @ [appleScriptObject
executeAndReturnError: errorDict];
The stack is shown below.

That's hard to answer because you're so obviously lying. You are not
reporting what you're really doing. What is theScript really? A string? Show
us the string. And clearly you are not really saying /Users/steve/, so
show us what you *are* saying.

Let me show you how to answer. Here is a *complete* valid chunk of *real*
Objective-C code:

NSString* theScript = @tell application \Finder\\n
@try\n
@set pp to \/Users/mattneub/Desktop/BrahmsHandel2.mus\\n
@comment of file (pp as POSIX file)\n
@on error\n
@return \Error\\n
@end try\n
@end tell\n;
NSDictionary *errorDict= nil;
NSAppleScript *appleScriptObject = [[NSAppleScript alloc]
initWithSource:theScript];
NSAppleEventDescriptor *eventDescriptor = [appleScriptObject
executeAndReturnError: errorDict];
[appleScriptObject release];
if (([eventDescriptor descriptorType])  (errorDict==nil)) {
NSLog(@%@, [eventDescriptor stringValue]);
} else {
NSLog(@%@,[errorDict objectForKey:@NSAppleScriptErrorMessage]);
}

I actually ran that code and it actually works (the finder comment from the
specified file appears in the log). Okay, now it's your turn. :) m.

-- 
matt neuburg, phd = m...@tidbits.com, http://www.tidbits.com/matt/
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.tidbits.com/matt/default.html#applescriptthings



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSAppleScript - what is going on here?

2009-03-22 Thread Steve Cronin

Matt;

I really take offense at your tone.  I was not lying.
I merely clipped out my personal details in order not to provide them  
to the world.
The 'theScript' IS a string - constructed in ObjC differently than you  
show but a string nonetheless.


Turns out the error was that an SDEF file had been renamed.

As Samuel Jackson said in Black Snake Moan --  Collar that dog!,
Steve

On Mar 22, 2009, at 12:05 PM, Matt Neuburg wrote:

On Sat, 21 Mar 2009 12:38:11 -0500, Steve Cronin  
steve_cro...@mac.com

said:

Folks;

I'm trying to get a string value back from a simple AppleScript in
Cocoa:

NSDictionary *errorDict= nil;
NSAppleScript *appleScriptObject = [[NSAppleScript alloc]
initWithSource:theScript];
NSAppleEventDescriptor *eventDescriptor = [appleScriptObject
executeAndReturnError: errorDict];
[appleScriptObject release];
if (([eventDescriptor descriptorType])  (errorDict==nil)) {
return [self stringFromAppleEventDescriptor:eventDescriptor];  //my
own method that checks descriptorType and returns stringValue
} else {
NSLog(@%@,[errorDict objectForKey:@NSAppleScriptErrorMessage]);
return nil;
}

The 'theScript' is a valid script that executes flawlessly in 'Script
Editor':
tell application Finder
try
comment of file (/Users/steve/ as POSIX file)
on error
return Error
end try
end tell

The problem is that the above errors out @ [appleScriptObject
executeAndReturnError: errorDict];
The stack is shown below.


That's hard to answer because you're so obviously lying. You are not
reporting what you're really doing. What is theScript really? A  
string? Show
us the string. And clearly you are not really saying /Users/ 
steve/, so

show us what you *are* saying.

Let me show you how to answer. Here is a *complete* valid chunk of  
*real*

Objective-C code:

   NSString* theScript = @tell application \Finder\\n
   @try\n
   @set pp to \/Users/mattneub/Desktop/BrahmsHandel2.mus\\n
   @comment of file (pp as POSIX file)\n
   @on error\n
   @return \Error\\n
   @end try\n
   @end tell\n;
   NSDictionary *errorDict= nil;
   NSAppleScript *appleScriptObject = [[NSAppleScript alloc]
initWithSource:theScript];
   NSAppleEventDescriptor *eventDescriptor = [appleScriptObject
executeAndReturnError: errorDict];
   [appleScriptObject release];
   if (([eventDescriptor descriptorType])  (errorDict==nil)) {
   NSLog(@%@, [eventDescriptor stringValue]);
   } else {
   NSLog(@%@,[errorDict  
objectForKey:@NSAppleScriptErrorMessage]);

   }

I actually ran that code and it actually works (the finder comment  
from the

specified file appears in the log). Okay, now it's your turn. :) m.

--
matt neuburg, phd = m...@tidbits.com, http://www.tidbits.com/matt/
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.tidbits.com/matt/default.html#applescriptthings





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSAppleScript - what is going on here?

2009-03-22 Thread Adam R. Maxwell

On Mar 21, 2009, at 10:38 AM, Steve Cronin wrote:

The 'theScript' is a valid script that executes flawlessly in  
'Script Editor':

tell application Finder
try
comment of file (/Users/steve/ as POSIX file)
on error
return Error
end try
end tell


It sounds like you've solved your problem, but I'd just note that if  
you're only getting Finder comments, MDItem is faster and simpler:


- (NSString *)commentForURL:(NSURL *)fileURL;
{
NSParameterAssert([fileURL isFileURL]);

MDItemRef mdItem = NULL;
CFStringRef path = (CFStringRef)[fileURL path];
NSString *theComment = nil;

if (path  (mdItem = MDItemCreate(CFGetAllocator(path), path))) {
theComment = (NSString *)MDItemCopyAttribute(mdItem,  
kMDItemFinderComment);

CFRelease(mdItem);
[theComment autorelease];
}
return theComment;
}





smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: NSAppleScript - what is going on here?

2009-03-22 Thread Steve Cronin

Adam;

I'm an ObjC guy -- I generally avoid the C stuff if possible - 'cause  
I have to support my own code.


But your method seems like it will work not only as a replacement but  
actually a doorway to the full gamut of common metadata attributes -  
very nice!!


I know I should lose the resistance but there are only so many  
mountains one guy can climb...


Thanks for the very helpful note!
Steve

On Mar 22, 2009, at 12:43 PM, Adam R. Maxwell wrote:


On Mar 21, 2009, at 10:38 AM, Steve Cronin wrote:

The 'theScript' is a valid script that executes flawlessly in  
'Script Editor':

tell application Finder
try
comment of file (/Users/steve/ as POSIX file)
on error
return Error
end try
end tell


It sounds like you've solved your problem, but I'd just note that if  
you're only getting Finder comments, MDItem is faster and simpler:


- (NSString *)commentForURL:(NSURL *)fileURL;
{
   NSParameterAssert([fileURL isFileURL]);

   MDItemRef mdItem = NULL;
   CFStringRef path = (CFStringRef)[fileURL path];
   NSString *theComment = nil;

   if (path  (mdItem = MDItemCreate(CFGetAllocator(path), path))) {
   theComment = (NSString *)MDItemCopyAttribute(mdItem,  
kMDItemFinderComment);

   CFRelease(mdItem);
   [theComment autorelease];
   }
   return theComment;
}





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSAppleScript - what is going on here?

2009-03-22 Thread Kyle Sluder
On Sun, Mar 22, 2009 at 1:53 PM, Steve Cronin steve_cro...@mac.com wrote:
 I'm an ObjC guy -- I generally avoid the C stuff if possible - 'cause I have
 to support my own code.

You cannot reasonably expect to avoid C... there's a lot of
functionality Cocoa doesn't provide.  Launch Services for example.
You're only doing yourself a disservice by avoiding C APIs, which are
just as much part of Objective-C as the OO stuff -- ObjC is a strict
superset after all.

I bet that by avoiding C you actually create more work for yourself
than if you just went with C when appropriate.

--Kyle Sluder
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSAppleScript - what is going on here?

2009-03-22 Thread Jim Correia

On Mar 22, 2009, at 1:53 PM, Steve Cronin wrote:

I'm an ObjC guy -- I generally avoid the C stuff if possible -  
'cause I have to support my own code.


Objective-C is a superset of C. Avoiding the C stuff isn't really  
possible - it is a core part of the language.


As far as choice of API, I tend to be a pragmatist, choosing an  
appropriate tool for the task at hand, without artificial restrictions.


JIm
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSAppleScript - what is going on here?

2009-03-22 Thread Adam R. Maxwell


On Mar 22, 2009, at 8:54 PM, Michael Ash wrote:

On Sun, Mar 22, 2009 at 1:53 PM, Steve Cronin steve_cro...@mac.com  
wrote:

Adam;

I'm an ObjC guy -- I generally avoid the C stuff if possible -  
'cause I have

to support my own code.


In addition to what the others have said, avoiding the C stuff if
possible but being perfectly happy to use *AppleScript*, of all
languages, is completely bizarre. C may be somewhat tedious and
unforgiving, but it doesn't come anywhere remotely close to the utter
horribleness that is AppleScript.


Indeed.  Let me add that the MDItem code I posted was the 3rd  
iteration of that method.  The first used NSAppleScript, but it took  
0.5s to execute and caused a noticeable hiccup in the app.  My second  
attempt used raw Apple events and executed in 0.01s, but would cause  
Finder to stall when it was called during a drag-and-drop targeting  
Finder (ISTR Finder's event loop was trying to process the Apple event  
and the drop event at the same time).


Using the MDItem C API had none of the AppleScript/Apple event  
problems, and was a small fraction of the code.  Once you get used to  
working with CF, it's really no harder than Obj-C, and gives you more  
tools to choose from.


--
Adam



smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: NSAppleScript - what is going on here?

2009-03-21 Thread Jerry Krinock


On 2009 Mar 21, at 10:38, Steve Cronin wrote:


What am I overlooking here?



#0  0x9543c1ab in CFDataGetBytePtr
#1  0x90462218 in XMLDataResolvingXIncludes
#2  0x90464fa8 in OSACopyScriptingDefinition
#3  0x90861531 in -[NSScriptSuiteRegistry loadSuitesFromBundle:]
#4	0x90860ede in -[NSScriptSuiteRegistry  
_loadSuitesForAlreadyLoadedBundles]

#5  0x90860bd4 in -[NSScriptSuiteRegistry init]


Looks like maybe it's having trouble parsing your sdef file.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com