clipboard snippets

How to implement copy-to-clipboard functionality that works with Flash 10

Tagged copy, clipboard, copy-to-clipboard, clippy, flash 10, flash  Languages 

Use mojombo's Clippy which can be downloaded from GitHub.

How to create a custom button

  • First create your button and save it as a PNG file.
  • Change library.xml to use your button.
  • Customize the code behind the button by modifying clippy.hx. You can, for example, remove the "Copy to clipboard" label.
  • Set the width and height of the button. You have to set it in almost all files (compile.hxml, library.xml, etc).
  • Compile the SWF file:
swfmill simple library.xml library.swf && haxe compile.hxml

Alternatives

There are no good alternatives that I know of. I tried ZeroClipboard and other solutions, but they were full of bugs and not customizable.

How to make Vim yank to the OSX clipboard

Tagged clipboard, osx, vim, yank  Languages bash, vim

Add this to .vimrc:

set clipboard=unnamed

If you use Tmux you probably need this: https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard

$ brew install reattach-to-user-namespace

Note, do NOT use something like this because it doesn’t work perfectly:

function! ClipboardYank()
  call system('pbcopy', @@)
endfunction
function! ClipboardPaste()
  let @@ = system('pbpaste')
endfunction

vnoremap <silent> y y:call ClipboardYank()<cr>
vnoremap <silent> d d:call ClipboardYank()<cr>
nnoremap <silent> p :call ClipboardPaste()<cr>p

Copy-to-clipboard with plain Javascript

Tagged clipboard, copy, javascript  Languages javascript
var Copy2Clipboard = {
  init: function(selector) {
    var btns = document.querySelectorAll(selector);
    for (var i = 0, len = btns.length; i < len; i++) {
      var btn = btns[i]
      btn.addEventListener('click', function(event) {
        var btn = event.target
        try {
          console.debug("click")
          var textarea = document.getElementById(btn.getAttribute('data-target'))
          if (textarea == null) {
            alert("copy-to-clipboard target is undefined")
            return
          }
          textarea.select();
          var successful = document.execCommand && document.execCommand('copy')
          if (successful) {
            btn.innerHTML = 'Copied...'
          } else {
            alert("Press Ctrl+C or Cmd+C to copy")
          }
        } catch (err) {
          console.log('Oops, unable to copy')
        }
      })
    }
  }
}

Copy2Clipboard.init('.copy-to-clipboard')
textarea id="embed-code">
  This will be copied to the clipboard.
/textarea>