Tuesday, July 17, 2018

#1 2016-04-25 01:04:06 pm

Registered: 2008-04-23
Posts: 43

Error with methods providing services

Hi All!

I'm trying to implement a few services in my app, and I'd like to use the NSUserData string in the property list to have the services perform differently depending on how they are called. 

The documentation says this about the NSUserData property:
NSUserData is an optional string that contains a value of your choice. You can use this string to customize the behavior of your service. For example, if your application provides several similar services, you can have the same NSMessage value for all of them (each service invokes the same method) and use different NSUserData values to distinguish between them. This entry is also useful for applications that provide open-ended, or add-on, services. This property is ignored for Automator workflows being used as services.

The problem looks like I can't find the right syntax to use for the method.

The services work fine if I use the following method, i.e.- deleting the userData and error arguments:


on helloWorld__(pboard)
display dialog "Hello World"
end helloWorld__

… but of course I don't get any strings from the userData argument.

If I try to implement the userData:error: part of the method, I get an error:  "Thread 1: EXC_BAD_ACCESS (code=1, address=0x20)" and the app freezes in Xcode.

I've tried to change the syntax, but it still fails the same way every time:


on helloWorld_userData_error_(pboard, userData, anError)
display dialog "Hello World"
end helloWorld_userData_error_


on helloWorld:pbData userData:uData |error|:aErr
display dialog "Hello World"
end helloWorld:userData:|error|:

I've included the part of the documentation that deals with the method below.

Thanks for taking a look and please let me know if there's anything you think I should try.

Thanks so much!!!

The documentation states:

The method providing the service is of the form messageName:userData:error: and takes the values shown in Listing 2. The method itself takes data from the pasteboard as needed, operates on it, and writes any results back to the pasteboard. In case of an error, the method simply sets the pointer given by the error argument to a non-nil NSString and returns. The error message is logged to the console. The userData parameter is not used here.

Listing 2  Service method


- (void)simpleEncrypt:(NSPasteboard *)pboard
userData:(NSString *)userData error:(NSString **)error {

// Test for strings on the pasteboard.
NSArray *classes = [NSArray arrayWithObject:[NSString class]];
NSDictionary *options = [NSDictionary dictionary];

if (![pboard canReadObjectForClasses:classes options:options]) {
*error = NSLocalizedString(@"Error: couldn't encrypt text.",
@"pboard couldn't give string.");

// Get and encrypt the string.
NSString *pboardString = [pboard stringForType:NSPasteboardTypeString];
NSString *newString = [self rotateLettersInString:pboardString];
if (!newString) {
*error = NSLocalizedString(@"Error: couldn't encrypt text.",
@"self couldn't rotate letters.");

// Write the encrypted string onto the pasteboard.
[pboard clearContents];
[pboard writeObjects:[NSArray arrayWithObject:newString]];



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)