Re: Sending a paste event to another app?
Re: Sending a paste event to another app?
- Subject: Re: Sending a paste event to another app?
- From: Steve Moon <email@hidden>
- Date: Mon, 17 Dec 2001 22:28:04 -0800
Thank a lot, this is exactly what I wanted !! And yes my application as
a particular behavior but my users are aware of this automatic paste
(since they are the ones that asked for it) ! In fact the paste is
happening automatically when the user is asking for it (using some
distributed object from a main application) : when the user request the
paste, my application will automatically paste the pasteboard's content
in the application the user had choosen previously (which hopefully
accept the CMD-V), this content can potentially take a long time to
compute and the users wanted the final result automatically pasted (they
have a warning sound before it happen so they know this 'auto paste' is
coming)
Thank you
Steve
On Monday, December 17, 2001, at 03:01 PM, Mason Mark wrote:
Hello,
I just yesterday was proofing the manual for one of our products in
development which does this very thing. (Our app does allow the user to
perform the text insertion via drag and drop, or Services, as Erik
suggests, but a floating palette with some "paste" buttons is also part
of the UI spec.)
While I do not entirely agree with Erik's comments below, I think you'd
be wise to heed his cautions. Mucking with the user's clipboard and
simulating a paste can be intrusive and dangerous (e.g., in some cases
paste can obliterate selected data, and you can't know for sure that
the app being pasted into even supports undo!). So, it is good to make
sure that the user is fully aware that whatever action she is taking in
your application will not only copy something to the clipboard, but
also attempt to simulate pasting it. It is unusual functionality that
your app should be extremely explicit about.
However, I wouldn't suggest that you not do it, just that you be very
sure that you need to.
As far as the implementation, there is no API to make this clean.
AppleEvents would be a fine one, but compatibility across applications
is relatively poor. There is no reasonable and consistent API for this
kind of interapplication communication where the apps in question are
arbitrary and not under your control.
So, kludgey though it seems to me, I think the best way to go about
this is to actually synthesize a command-V keystroke combination. This
works for pasting into Carbon, Classic, and Cocoa apps, and is much
closer to universal than any other method. Plus, it has the advantage
of being easy. ;-)
You can do it something like this:
+ (void)fakeCommandV
/*" +fakeCommandV synthesizes keyboard events for Cmd-v Paste
shortcut. "*/
{
CGPostKeyboardEvent( (CGCharCode)0, (CGKeyCode)55, true ); //
shift down
CGPostKeyboardEvent( (CGCharCode)'v', (CGKeyCode)9, true ); // 'v'
down
CGPostKeyboardEvent( (CGCharCode)'v', (CGKeyCode)9, false ); // 'v'
up
CGPostKeyboardEvent( (CGCharCode)0, (CGKeyCode)55, false ); //
'shift up
}
Just be sure to bear in mind the pitfalls:
- you cannot tell if the paste operation succeeded
- does not work with strange applications like VueScan which have
astonishing keyboard shortcuts (Cmd-V is something like "View Memory"
in that application.
- kludgey - what else might go wrong?
This is why our user manual explains the behavior as "simulates
pressing Command-V on the keyboard" rather than as "Paste".
Hope that helps,
--
Mason
On Monday, December 17, 2001, at 09:42 AM, Erik M. Buck wrote:
Your request seems like very bad behavior. Why not use drag and
drop ? Why
not use services ? Why not use anything rather than taking control of
another application and pasting without the user's control or
permission
possibly replacing any current selection. In fact, what you describe
sounds
like a virus to me.
All of that said, I don't know how to do it and I hope you won't do it.
----- Original Message -----
From: "Steve Moon" <email@hidden>
To: <email@hidden>
Sent: Monday, December 17, 2001 10:39 AM
Subject: Sending a paste event to another app?
Hi
my AppKit application need to automatically paste some data from its
pasteboard to another application. In fact it is required that my app
paste the data contents automatically : basically my app need to
execute
the equivalent of the keyboard shortcut CMD-V to any app.
Is there an easy way for doing that ?? I was thinking that by using
some
AppleScript I can probably send the event ID kAEPaste to the final
application but I can't find any example that explain how to proceed.
Right now my code looks like :
[... code to activat the final application + some wait time...]
// frontProcess below is initialize using : GetFrontProcess() from
Carbon
myErr = AECreateDesc(typeProcessSerialNumber, &frontProcess,
sizeof(ProcessSerialNumber), &addr);
myErr = AECreateAppleEvent(kAEMiscStandards, eventID, &addr,
kAutoGenerateReturnID, kAnyTransactionID, &actionEvent);
myErr = AESendMessage(&actionEvent, NULL, kAENoReply, kNoTimeOut);
But this code does nothing... it does not crash but nothing happen.
And
yes my pasteboard is correctly set since by doing a CMD-V in the final
app I got the right data pasted in the app.
Steve
Note: my app do not know in advance what is the final application
where
the Paste event should occur; this is something that the user can
define
at will : it could be a Cocoa app (like TextEdit), a Carbon app (like
Internet Explorer), ... and so I can not add my custom code in this
final app for handling this case : my app need so to activate first
the
final app and send the Paste event.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.