Working on a cut and paste integration with MacOS

Archives Forums/MacOS X Discussion/Working on a cut and paste integration with MacOS

Arowx(Posted 2009) [#1]
Well I'm trying to get a simple app to integrate with Mac OS to allow text based cut and paste but the examples I've found so far are either Objective C which I must admit I don't quite get but then I've never used it so I'm going to try a Carbon.h approach based on the PasteboardPeeker example...

Wish me luck this could take a while C++ the reason I went over to BlitzMAX !!!!

If you happen to have a simple MacOS example of Copy and Paste functions for text don't hesitate to shout out!


Arowx(Posted 2009) [#2]
First working attempt at accessing the MacOS pasteboard/clipboard for cut and paste operations...

// pasteboard the mac version of clipboard - aim same interface different OS!
#include <AppKit/AppKit.h>
#include <AppKit/NSPasteboard.h>
#include <brl.mod/blitz.mod/blitz.h>

BBString* bbStringFromNSString(NSString *s)
{
	BBString	*bbstring;

	int		n;
	n=[s length];
		
	unsigned short	*buff;
	buff=malloc(n*2);
	[s getCharacters:buff];
	
	bbstring=bbStringFromShorts(buff,n);
	free(buff);
	return bbstring;	
}

BBString* readText(void)
{
	NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
	NSString *text = [pasteBoard stringForType:NSStringPboardType];
		
	BBString *bbText = bbStringFromNSString(text);
		
	return bbText;
}

BOOL writeText(BBString* newString)
{
	NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
    [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];

    return [pasteBoard setString:[NSString stringWithCString:bbStringToCString(newString)] forType:NSStringPboardType];
}
pasteboard.m

Rem
	Simple test app to get the copy and paste function to work on the mac
End Rem

SuperStrict

?MacOS
Import "-framework WebKit"
Import "../Include/pasteboard.m"
?

Extern
	Function readText:String()
	Function writeText(text:String)
End Extern

Graphics 600, 400

Global clipboardString:String = "Hello World"
Global getCounter:Int = 0
Global setCounter:Int = 0
Global value:Int = 0
Global cliptext:String = "Testing..."

Global text:String[] = ["Alpha","Bravo","Charlie","Delta"]

While Not AppTerminate()

	Cls

	If MouseHit(2) Then 
		clipboardString = getClipboardText()
		DebugLog "ClipboardString:"+ clipboardString
	EndIf
		
	DrawText "Clipbaord Test App: "+ clipboardString, 10, 10
	DrawText "Left Click to copy text to clipboard", 10, 100
	DrawText "Right Click to paste text from clipboard", 10, 120
	
	If MouseHit(1) Then 
		clipboardString = cliptext+text[value]
		setClipboardText(clipboardString)
		DebugLog "ClipboardString:"+ clipboardString
		value:+1
	EndIf
	
	If value > 3 Then
		value = 0
		
	EndIf
	
	Flip

Wend


Function getClipboardText:String()
	Local text:String = readText()
	Print "getClipboardText:"+text
	Return text
End Function 
	
Function SetClipboardText:Int(txt:String)
	Return writeText(txt)
End Function
pasteboardText.bmx

EDIT 2 - It work's well kind of the paste gets the last clipping from the clipboard not the current!


Arowx(Posted 2009) [#3]
Excellent it now pastes text to the clipboard and NSLogs the text back from the clipboard!

But fails to return the NSString via bbStringFromNSString() as anything but a 0 or Null, from the source in cocoagui.bmx and cocoa.macos.m this should work what am I missing?


Brucey(Posted 2009) [#4]
I don't think you get bonus points for posting in three different places :-p


Arowx(Posted 2009) [#5]
Well it's two issues one is the IDE throwing a compile error from the NSLog entry, and the Mac OS thread was great to write about a 'successful' cut and paste option on the Mac although when I hit the problem I've got I posted in the appropriate place the Beginners thread!

It's very quite here in the Mac OS thread isn't it!


Brucey(Posted 2009) [#6]
It's very quite here in the Mac OS thread isn't it!

Quite :-)

btw, you really probably want not to use "C" string conversions, but rather UTF8, otherwise you limit yourself to ASCII characters.

You can also get away with not needing to do this memory stuff:
unsigned short	*buff;
	int		n;
	n=[s length];
	buff=malloc(n*2);


by using something like :
unsigned short buff[n*2];
[s getCharacters:&buff];

(untested, of course ;-)


Arowx(Posted 2009) [#7]
Hi Brucey that generates another issue an error regarding unaligned memory!

But got it SuperStrict is good! cilp... and clip... are different apparently!

Must put a request in for variable name highlighting or default SuperStrict! ;o)

Don't ask how long I've been banging my head against this one!

PS:> Updating code for my typo!


TikiDays(Posted 2016) [#8]
I was wondering do you know of a way to copy, paste images in OSX?