Tag Archive for clipboard

Using the Clipboard

if(!window.config) var config = {};
config.Clipboard = {
pathToFlash: "/testsite/generic scripts/clipboard.swf",
//see <a href="http://javascript.internet.com/forms/clipboard-copy.html" >http://javascript.internet.com/forms/clipboard-copy.html</a>
//SWF file can be downloaded at <a href="http://javascriptsource.com/forms/clipboardFlash.zip" >http://javascriptsource.com/forms/clipboardFlash.zip</a>
flashCopierID: "flashCopier"
};

//static Clipboard object
var Clipboard = function()
{
//copies a string to the clipboard
function copy(strToCopy)
{
if(!strToCopy) return false;

if(window.clipboardData && window.clipboardData.setData("text", strToCopy)) return true;	//IE

var holder = document.createElement("textarea");
holder.innerText = strToCopy;
if(holder.createTextRange && holder.createTextRange().execCommand("copy")) return true;	//IE

return flashCopy(strToCopy);	//use the Flash copier
}

//copies the currently selected text to the clipboard
function copySelection()
{
var selectionText = "";
//order matters here: Opera sparcely supports the TextRange object (IE) as well
if(window.getSelection) selectionText = window.getSelection();	//(Selection object).toString()
if(document.selection) selectionText = document.selection.createRange().text;	//IE; (TextRange object).text

return copy(selectionText);
}

//use a Flash file to copy to clipboard (assuming Flash is enabled in the browser, of course)
//see <a href="http://javascript.internet.com/forms/clipboard-copy.html" >http://javascript.internet.com/forms/clipboard-copy.html</a>
//SWF file can be downloaded at <a href="http://javascriptsource.com/forms/clipboardFlash.zip" >http://javascriptsource.com/forms/clipboardFlash.zip</a>
function flashCopy(strToCopy)
{
if(!strToCopy || !window.config.Clipboard.pathToFlash) return false;

var holder = document.getElementById(window.config.Clipboard.flashCopierID);
if(!holder)
{
holder = document.createElement("div");
holder.id = window.config.Clipboard.flashCopierID;

//holder.style.display = "none";	//the copy fails with this
holder.style.position = "absolute";
holder.style.zIndex = "-1";

document.body.appendChild(holder);
}
holder.innerHTML = '<embed src="'+window.config.Clipboard.pathToFlash+'" FlashVars="clipboard='+
escape(strToCopy)+'" type="application/x-shockwave-flash"></embed>';

return null;	//no way to know whether it was successful or not
}

//returns the contents of the clipboard
function paste()
{
if(window.clipboardData)	//IE
{
return window.clipboardData.getData("text") || "";
}
else
{
var textarea = document.createElement("textarea");
if(textarea.createTextRange)	//IE
{
textarea.createTextRange().execCommand("paste");
return textarea.innerText;
}
}
return "";
}

//clears the clipboard or sets it to a period if it can't be cleared
function clear()
{
if(window.clipboardData)	//IE
{
window.clipboardData.clearData();
return true;
}
else return copy(".");
}

return {
copy: copy,
copySelection: copySelection,
paste: paste,
clear: clear
};
}();	//initialize Clipboard

source

javascript copy text to clipborad

function copyToClipboard(txt) {
if(window.clipboardData) {
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
} else if(navigator.userAgent.indexOf("Opera") != -1) {
window.location = txt;
} else if (window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("被瀏覽器拒絕!
請在瀏覽器地址欄輸入'about:config'並回車
然後將'signed.applets.codebase_principal_support'設置為'true'");
}
var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
if (!clip)
return;
var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
if (!trans)
return;
trans.addDataFlavor('text/unicode');
var str = new Object();
var len = new Object();
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
var copytext = txt;
str.data = copytext;
trans.setTransferData("text/unicode",str,copytext.length*2);
var clipid = Components.interfaces.nsIClipboard;
if (!clip)
return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
alert('已經複製');
}
</script>

source

Irb power tricks with Wirble & Utility-Belt

## Required

# install
sudo gem install utility_belt

# settings for ~/.irbrc
require 'rubygems'
require 'utility_belt'
UtilityBelt::Themes.background(:light) #optional

## Now you can play in irb

# Interactively edit IRB code in TextMate
mate

# read from clipboard
MacClipboard.read

# write to clipboard
MacClipboard.write 'some text to be pasted elsewhere'

# review history
h # 'history' also works, but who wants to type?

# re-play last command
h!

# re-play arbitrary command from history
h! 123

# save history to some file
history_write('/path/to/file')

# find class name
grep_classes("num")
#=> ["Bignum", "Fixnum", "Numeric", "REXML::SyncEnumerator"]

# find method name
"foo".grep_methods("pretty")
#=> ["pretty_inspect", "pretty_print_instance_variables", ... ]

# google for whatever's in clipboard
google

# arbitrary google search
google singleWord
google 'multiple words'

source

Putting data on the clipboard in OS X

IO.popen('pbcopy', 'w').print "Text to go on clipboard"

source

clipboard commands for OSX

pbcopy < filename
#copies contents of a text, rtf, or eps file to the clipboard

pbpaste
#pastes content of clipboard to stdout

source

Summarize Web page bookmarklet

//url-escaped, ready for use:
javascript:function%20userSel()%7Bif%20(window.getSelection)%7Btxt%20=%20window.getSelection();%7Delse%20if%20(document.getSelection)%20%20%20%20%7Btxt%20=%20document.getSelection();%7Delse%20if%20(document.selection)%7Btxt%20=%20document.selection.createRange().text;%7Delse%20return;%20%20return%20txt;%7Dfunction%20add(h)%7Bb.appendChild(h);%7Dfunction%20makeTag(t)%7Breturn%20document.createElement(t);%7Dfunction%20makeText(tag,text)%7Bt=makeTag(tag);%20t.appendChild(document.createTextNode(text));%20return%20t;%7Dp=document.location;%20q=document.title.replace(/%5C[[%5E%5C]]*%5C](.*)%20-%20JAG%20JIRA/,%20'$1');%20d=window.open().document;%20d.open();%20d.close();%20b=d.body;%20d.title%20=%20q%20+%20'%20:%20summary%20:%20JIRA';%20u=p+'%5Cn'+q+'%5Cn'+userSel();%20add(makeText('style',%20'textarea%7Bwidth:100%;height:100%;%7D'));%20add(makeText('textarea',%20u));%20void%200

//unescaped, with line breaks
javascript: function userSel() {
if (window.getSelection) {
txt = window.getSelection();
} else if (document.getSelection) {
txt = document.getSelection();
} else if (document.selection) {
txt = document.selection.createRange().text;
} else return;
return txt;
}
function add(h) {
b.appendChild(h);
}
function makeTag(t) {
return document.createElement(t);
}
function makeText(tag, text) {
t = makeTag(tag);
t.appendChild(document.createTextNode(text));
return t;
}
p = document.location;
q = document.title.replace(/[[^]]*](.*) - JAG JIRA/, '$1');
d = window.open().document;
d.open();
d.close();
b = d.body;
d.title = q + ' : summary : JIRA';
u = p + '
' + q + '
' + userSel();
add(makeText('style', 'textarea{width:100%;height:100%;}'));
add(makeText('textarea', u));
void 0;

source

J2ME – System Properties

package System;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class SystemProperties extends MIDlet implements CommandListener
{
private Command esci;

private Display display;

private Form form;

protected void startApp() throws MIDletStateChangeException
{
display = Display.getDisplay(this);

form = new Form("System Propiertis");
form.setCommandListener(this);

esci = new Command("Esci", Command.EXIT, 0);
form.addCommand(esci);

Runtime rt = Runtime.getRuntime();
rt.gc(); // Garbage Collection

form.append("Free Memory: " + rt.freeMemory() + "
");
form.append("Total Memory: " + rt.totalMemory() + "
");
form.append(showProp("microedition.configuration"));
form.append(showProp("microedition.platform"));
form.append(showProp("microedition.locale"));
form.append(showProp("microedition.encoding"));
form.append(showProp("microedition.encodingClass"));
form.append(showProp("microedition.http_proxy"));

display.setCurrent(form);
}

protected void pauseApp()
{

}

protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
{
notifyDestroyed();
}

public String showProp(String str)
{
String value = System.getProperty(str);
StringBuffer stringbuffer = new StringBuffer(50);

stringbuffer.setLength(0);
stringbuffer.append(str);
stringbuffer.append(" = ");

if(value == null)
stringbuffer.append("<undefined>");
else
{
stringbuffer.append(""");
stringbuffer.append(value);
stringbuffer.append(""");
}

stringbuffer.append("
");

return stringbuffer.toString();
}

public void commandAction(Command c, Displayable d)
{
if(c == esci)
{
try
{
destroyApp(true);
}
catch(MIDletStateChangeException e)
{
showException(e);
}
}
}

public void showException(Exception e)
{
Alert alert = new Alert("Errore !!!");
alert.setString(e.getMessage());
alert.setType(AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);

display.setCurrent(alert);
}
}

source

Java – int2bin

int numero = 15;
System.out.println(Integer.toBinaryString(numero));

source

Java – CUT&PASTE

package system.clipboard;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class ClipBoard extends JFrame implements ClipboardOwner, ActionListener
{
private static final long serialVersionUID = 1L;

JTextArea srcText, dstText;
JButton copyButton, pasteButton;
Clipboard clipboard = getToolkit().getSystemClipboard();

public ClipBoard()
{
super("Clipboard Test");

GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

setLayout(gridbag);

srcText = new JTextArea(8, 32);
c.gridwidth = 2;
c.anchor = GridBagConstraints.CENTER;
gridbag.setConstraints(srcText, c);
add(srcText);

copyButton = new JButton("Copy Above");
copyButton.setActionCommand("copy");
copyButton.addActionListener(this);
c.gridy = 1;
c.gridwidth = 1;
gridbag.setConstraints(copyButton, c);
add(copyButton);

pasteButton = new JButton("Paste Below");
pasteButton.setActionCommand("paste");
pasteButton.addActionListener(this);
pasteButton.setEnabled(false);
c.gridx = 1;
gridbag.setConstraints(pasteButton, c);
add(pasteButton);

dstText = new JTextArea(8, 32);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
gridbag.setConstraints(dstText, c);
add(dstText);

pack();
}

public void actionPerformed(ActionEvent evt)
{
String cmd = evt.getActionCommand();

if(cmd.equals("copy"))
{
// Implement Copy operation
String srcData = srcText.getText();

if(srcData != null)
{
StringSelection contents = new StringSelection(srcData);
clipboard.setContents(contents, this);
pasteButton.setEnabled(true);
}
}
else if(cmd.equals("paste"))
{
// Implement Paste operation
Transferable content = clipboard.getContents(this);
if(content != null)
{
try
{
String dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
dstText.append(dstData);
}
catch(Exception e)
{
System.out.println("Couldn't get contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName());
}
}
}
}

public void lostOwnership(Clipboard clipboard, Transferable contents)
{
System.out.println("Clipboard contents replaced");
}

public static void main(String[] args)
{
ClipBoard test = new ClipBoard();
test.setVisible(true);
}
}

source

wsh_setClipboard

function setClip( txt )
{
var ie = new ActiveXObject( "InternetExplorer.Application" );
ie.Navigate( "about:blank" );

while( ie.Busy ) WScript.Sleep( 50 );

ie.Document.parentWindow.clipboardData.setData( "text", txt );
ie.Quit( );
}

source