Copy an array to clipboard

Hello, new challenge:

I have a list / NSArray containing either

missing value /
or
real / __NSCFNumber

I’d like to copy this array to the clipboard / pasteboard, so I can paste it to an other application (like Microsoft® Excelâ„¢): real values (or blank lines) separated by a character.

In ASOC I did so:

set myCopy to {}
repeat with i from 1 to count of myArray
	if item i of myArray is not missing value then
		set myCopy to (myCopy & (item i of myArray) as string) & return
	else
		set myCopy to myCopy & return
	end if
end repeat
set the clipboard to myCopy

and it worked perfectly. Now for Objective-C.

The problem is, if I use this:

I get, if the value is a number :

and of course if it’s null :

I read the docs and tried something else:
BOOL OK = [pasteboard setData:[myArray valueForKey:] forType: @“RTF”];
Bang.

Ok, and now? Could I create a NSValueTransformer, pseudocode:

if (item is a number) then
return a number +
else
return
endif

. or copy this array to a copy array, like in ASOC? Or use an other method?

Thanks for any answer!

Hi,

first of all: If you want to paste the result into Excel without user interaction,
you could do this directly with AppleScript. That’s the power of using ASOC even in a Objective-C app.

There are several ways to convert the array to paragraphs, replace the null items with empty strings and copy it to the pasteboard. Here is one:


NSMutableString *arrayAsParagraphs = [[myArray  componentsJoinedByString:@"\r"] mutableCopy];
    [arrayAsParagraphs replaceOccurrencesOfString:@"<null>" withString:@"" options:0 range:NSMakeRange(0, [arrayAsParagraphs length])];
[[NSPasteboard generalPasteboard]declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[[NSPasteboard generalPasteboard] setString:arrayAsParagraphs forType:NSStringPboardType];
[arrayAsParagraphs release];

Works perfectly. I could have tried hours before finding this. NSArray is like NSString: full of resources.

Thank you Stefan, you made my day!