Call method with parameter (array)

Hey all,

I’m currently busy with ASStudio and want to use the call method with parameter (which is an array) in order to get some things done.

This is mainly about the C/ObjC part, handling the array which was send using the call method.

Here’s some code from my Applescript:


set myArray to {100, 200, 300}
set myReturnedString to call method "ReturnMyValue:" with parameter myArray

and the method in C/ObjC:


- (NSString*)ReturnMyValue:(NSArray *)myValues
{
        ---
        Missing code:
        I have to work with the incoming array 'myValues', like:
        
        int theCalculatedValue =  myValues[0] + myValues[1] / myValues[2];
        ---
       The result should be send back to my Applescript:
	return [NSString stringWithFormat:@"%hu", theCalculatedValue];
	
}

I’ve tried several things, but keep getting errors. The most common error is:
Incompatible types in assignment, whenever I add the following code to my method:


	NSArray *myVar[3];
	myVar[0] = myValues[1];

This is just an example of what I try to achieve.
I must say that I’m not a programmer at all, and that C/ObjC is very difficult for me.

A brief description of the things that I actually would like to achieve (in the end) can be found here:
http://lists.apple.com/archives/colorsync-dev/2003/May/msg00009.html
(for those who read the article above, I’ve managed to create a colorworld that can do the transformation, so the focus is now on parsing and working with the array)

Thnks in advance for any help on this,

Erik

The following worked for me. Note that you can enter just about any type of input in the array (strings, floats, integers) and any reasonable format should be made into an integer and able to be mathmatized. :wink:

Applescript Code…

set myArray to {100, "1000", 10.0}
set myValue1 to call method "ReturnMyValue:" of class "JBCalculate" with parameter myArray

Obj-c code in a cocoa implementation file “JBCalculate.m”…

#import <Cocoa/Cocoa.h>

@interface JBCalculate : NSObject
+(int)ReturnMyValue:(NSArray *)myValues;
@end

@implementation JBCalculate
+(int)ReturnMyValue:(NSArray *)myValues {
	int total, number0, number1, number2;
	number0 = [[myValues objectAtIndex: 0] intValue];
	number1 = [[myValues objectAtIndex: 1] intValue];
	number2 = [[myValues objectAtIndex: 2] intValue];
	
	total = (number0 + (number1 / number2));
	
	return total;
}
@end

Hope that does the trick…
j

Thanks jobu, it looks great!

Whit this method, everything is treated as an integer, which is OK for the first part of my method. Actually those numbers from myArray will be 16-bit RGB/CMYK/Lab color values, which I need to transform to another colorspace via the method.

But, the method needs to have two other variables passed via applescript, the path to the ICC profiles(source profile and destination profile) which are going to be used.

An example path might be:


srcProfile = "/Library/ColorSync/Profiles/sRGB Profile.icc" --with spaces in its name?
dstProfile = "/Libray/ColorSync/Profiles/ISOcoated.icc" 

Can you tell me if it’s possible to pass those variables as well via your method?
If that can be done, I think everything is solved here.

But again, thanks for your work already!

Erik

Things are only treated as an integer because that’s all you’re passing to it, and because it coerces everything it receives to an integer. The only thing you’re limited to in terms of things being treated as integers, is that the method is configured to RETURN an integer because of the “+(int)…” declaration in the beginning of the method. If you only want to PASS variables to the method, you only need to adjust the method to accept more parameters. Then the method needs to be adjusted depending on what you want to get back FROM the method.

Applescript… to return an array:

set srcProfile to "/Library/ColorSync/Profiles/sRGB Profile.icc"
set destProfile to "/Library/ColorSync/Profiles/ISOcoated.icc"
set colorValues to {100, "1000", 10.0}
set {theTotal, theSrcProfile, theDestProfile} to call method "stringFromColorValues:withSourceProfile:toDestinationProfile:" of class "JBCalculate" with parameters {colorValues, srcProfile, destProfile}

Obj-c… to return an array:

+(NSArray *)stringFromColorValues:(NSArray *)myValues withSourceProfile:(NSString *)srcProfile toDestinationProfile:(NSString *)destProfile {
	int number0, number1, number2;
	number0 = [[myValues objectAtIndex: 0] intValue];
	number1 = [[myValues objectAtIndex: 1] intValue];
	number2 = [[myValues objectAtIndex: 2] intValue];

	NSValue *total = [NSNumber numberWithInt:(number0 + (number1 / number2))];

	NSArray *resultArray;
	resultArray = [NSArray arrayWithObjects: total, srcProfile, destProfile, nil];

	return resultArray;
}

Applescript… to return only the integer:

set theTotal to call method "stringFromColorValues:withSourceProfile:toDestinationProfile:" of class "JBCalculate" with parameters {colorValues, srcProfile, destProfile}

Obj-c… to return only the integer:

+(int)stringFromColorValues:(NSArray *)myValues withSourceProfile:(NSString *)srcProfile toDestinationProfile:(NSString *)destProfile {
	int total, number0, number1, number2;
	number0 = [[myValues objectAtIndex: 0] intValue];
	number1 = [[myValues objectAtIndex: 1] intValue];
	number2 = [[myValues objectAtIndex: 2] intValue];
	
	total = (number0 + (number1 / number2));
	
	return total;
}

j

Well jobu, I must say that right now you’re helping me to solve a puzzle that started 18 months ago!

With your examples, I should be able to complete the first, but most important part of the project; getting variables from Applescript to the ObjC method.

I now can try to implement everything, and build some default color conversions.

So, thanks again, I’ll keep you posted on the progress!

Erik

OK, things are getting somewhere here!

But, there’s one issue I have to deal with:
The …withSourceProfile:(NSString *)srcProfile… defines the path to the ICC profile as NSString, but I now found that this needs to be char.

If I set the code manually, it works, and I can load the correct profile, eg:

char *srcProfPath;
srcProfPath="/Library/ColorSync/Profiles/sRGB Profile.icc";

Is there an easy way to convert the NSString (srcProfile) to char?

Erik

Finally, I got things working!
I learned that there’s no easy way to convert NSString to char.
But, the solution was closer then I thought…

Original code started with: …withSourceProfile:(NSString *)srcProfile…

I now use: …withSourceProfile:(char *)srcProfile…

The srcProfile path is now tread as char, which is the format that need to load the profile.

Thanks again for the support!

Erik

Hi again,

I have been playing with a record (which is equivalent to NSDictionary).

You can read and write a record to/from a file (pList i think) using call method with

dictionaryWithContentsOfFile:

and

writeToFile:atomically:

I am thinking it must therefore be possible via an objective C custom class to

transfer the ASS record into the class as a dictionary

and

read it from the dictionary contained in the class into ASS record

This class would be available to all scripts and would therefore allow transfer of a global record

Anyone with the objective C skills to implement this please.

TIA

–Terry