deAd
Scripting Contest Winner
Posts: 1060 Reputation: 28
– / /
Joined: Jan 2006
|
O.P. [UPDATED] Clipboard Functions (that WORK)
UPDATED: If you have added the readClipboard() function to your script, update it now. Fixed major bug that caused the clipboard to stay locked on certain occasions.
Here's some great functions I made to get the clipboard text and change the clipboard text! They are based off the non-functional code in these requests: Get Text and Set Text. They are fully functional, but BE CAREFUL when you modify them. I haven't had any problems yet.
Be careful when you modify these.
@Segosa - your problem was (1) no unicode and (2) you did it a bit wrong
Clear Text:
No return value.
code: function clearClipboard(){
if (Interop.Call("User32", "OpenClipboard", 0)){
Interop.Call("User32", "EmptyClipboard");
Interop.Call("User32", "CloseClipboard");
}
}
Get Text:
Do not remove the try/catch statement. Your clipboard may lock up.
This function will return false if there are errors (including the text being too long. I don't know how long it can go.) or if the clipboard is currently in use by another program. Otherwise it will return the text
code: function readClipboard() {
var CF_TEXT = 1;
var CF_OEMTEXT = 7;
var CF_UNICODETEXT = 13;
var toBeReturned = false;
try {
if (Interop.Call("User32", "OpenClipboard", 0)) {
if (Interop.Call("User32", "IsClipboardFormatAvailable", CF_TEXT | CF_OEMTEXT)) {
var hClipboardData = Interop.Call("User32", "GetClipboardData", CF_UNICODETEXT);
var pchData = Interop.Call("Kernel32", "GlobalLock", hClipboardData);
var size = Interop.Call("Kernel32", "GlobalSize", hClipboardData);
var str = Interop.Allocate(size+2);
Interop.Call("Kernel32", "RtlMoveMemory", str, pchData, size);
toBeReturned = str.ReadString(0);
var unlocked = Interop.Call("Kernel32", "GlobalUnlock", hClipboardData);
} else {
Interop.Call("User32", "CloseClipboard");
return false;
}
Interop.Call("User32", "CloseClipboard");
}
} catch(ex) {
Interop.Call("User32", "CloseClipboard");
return false;
}
return toBeReturned;
}
Set Text:
This function has no return value. To use it, type writeClipboard(String);.
code: function writeClipboard(str){
var size = str.length*2+2;
var GMEM_MOVEABLE = 0x2;
var GMEM_SHARE = 0x2000;
var GMEM_ZEROINIT = 0x40;
var memtype = GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT;
if (Interop.Call("User32", "OpenClipboard", 0)){
var heap = Interop.Call("Kernel32", "GlobalAlloc", 0, size);
var p = Interop.Call("Kernel32", "GlobalLock", heap);
Interop.Call('Kernel32','RtlMoveMemory',p,str,size);
Interop.Call("Kernel32", "GlobalUnlock", heap);
if (Interop.Call("User32", "OpenClipboard", 0)){
Interop.Call("User32", "EmptyClipboard");
Interop.Call("User32", "SetClipboardData", 13, heap);
Interop.Call("User32", "CloseClipboard");
}
}
}
This post was edited on 07-12-2006 at 01:16 PM by deAd.
|
|