Posts Tagged ‘copy’

Patch for AntTweakBar to support pasting from clipboard on Mac OS X

Tuesday, January 3rd, 2012

I recently noticed I could use pbpaste and a system call to integrate the clipboard’s contents into my C++ programs. I use AntTweakBar all the time for quick prototyping UI and though it has a default internal clipboard, I found it frustrating that it doesn’t hook into my mac’s global clipboard. To make this happen I just modified TwBar.cpp in two places EditInPlaceSetClipboard and EditInPlaceGetClipboard, surrounding each edit with #elif defined ANT_OSX. Now I can copy from my AntTweakBar and paste into some other app or copy from some other app and paste into an AntTweakBar string field.

Here’s what to place in EditInPlaceSetClipboard

// PATCH BEGIN Alec Jacobson, 2012
// PATCH BEGIN Alec Jacobson, 2012
#elif defined ANT_OSX
    stringstream cmd;
    cmd << "echo \"" << _String << "\" | pbcopy";
    FILE* pipe = popen(cmd.str().c_str(), "r");
    if (!pipe)
    {
        return false;
    }
// PATCH END

And here’s what to place in EditInPlaceGetClipboard

// PATCH BEGIN Alec Jacobson, 2012
// PATCH BEGIN Alec Jacobson, 2012                                                                        
#elif defined ANT_OSX
    FILE* pipe = popen("pbpaste", "r");                                                                   
    if (!pipe)
    {   
        return false;
    }                                                                                                     
    char buffer[128];
    string result = "";                                                                                   
    while(!feof(pipe))
    {   
        if(fgets(buffer, 128, pipe) != NULL)                                                              
        {
            result += buffer;                                                                             
        }                                                                                                 
    }   
    pclose(pipe);
    *_OutString = result.c_str();                                                                         
// PATCH END

To ensure your binaries run with this patched version you might consider compiling AntTweakBar as a static library.

Accessing clipboard (copy paste) from C++ program on Mac OS X

Monday, January 2nd, 2012

I’ve been using pbcopy and pbpaste to control the clipboard via the command on mac for a while now. It’s not to hard to utilized these in a c++ program, integrating my little apps with the clipboard without having to go through Cocoa or objective c or any libraries. Here’s a little demo:


#include <string>
#include <iostream>
#include <sstream>
#include <stdio.h>

// http://stackoverflow.com/questions/478898
std::string exec(const char* cmd)
{
  FILE* pipe = popen(cmd, "r");
  if (!pipe) return "ERROR";
  char buffer[128];
  std::string result = "";
  while(!feof(pipe))
  {
    if(fgets(buffer, 128, pipe) != NULL)
    {
      result += buffer;
    }
  }
  pclose(pipe);
  return result;
}

std::string paste()
{
  return exec("pbpaste");
}

std::string copy(const char * new_clipboard)
{
  std::stringstream cmd;
  cmd << "echo \"" << new_clipboard << "\" | pbcopy";
  return exec(cmd.str().c_str());
}

int main(int argc, char * argv[])
{
  using namespace std;
  cout<<"old clipboard: "<<paste()<<endl;
  copy("Bomb!");
  cout<<"new clipboard: "<<paste()<<endl;
  return 0;
}

Vi(m) tip #9: Copy, Cut and Paste into Mac OS X clipboard

Wednesday, June 30th, 2010

I can get a lot done in vim without ever having to use the mouse or really exit “vim world”. But the one thing that I keep falling back on is copy and pasting from and to other applications. Here’s a way to do these
operations without resorting to the mouse or foreign keystrokes. Issue these in command mode:

Cut line under cursor


:.!pbcopy

Copy line under cursor


:.!pbcopy|pbpaste

Paste as line beneath cursor


:r !pbpaste

You can also use this if you’ve made a selection in Visual Mode:

Cut current selection

(warning: this grabs the whole lines of any lines within selection)


:'<,'>!pbcopy

Copy current selection

(warning: this grabs the whole lines of any lines within selection)


:'<,'>!pbcopy|pbpaste

Note: It seems for that one you should just select the first letter of each line. Not the entire block, then it just cuts…

Update: It’s much simpler:


"*yy

Yanks the current line to the clipboard. Similarly,


"*dd

cuts the current line and


"*p

pastes the clipboard below the current line.

Synergy server and client settings and commands

Saturday, November 21st, 2009

After the recommendation of a linux friend, I have begun using synergy to share my mouse and keyboard between the new macbook pro on my desk and the old powerbook two shelves up (I do have to physically switch the DVI cable to see the right computer on my monitor though … DVI switches still cost too much).

On the server (the computer which actually owns the mouse and keyboard), named
Enfermera:

Here is my synergy.conf file saved in ~/.synergy.conf


section: screens
  Enfermera.local:
  AJX.local:
end
section: links
  Enfermera.local:
    right = AJX.local
  AJX.local:
    left = Enfermera.local
end

Then on this computer (server) I run the command


synergys -f --config ~/.synergy.conf 

On the client (the computer not physically or emotionally connected to the mouse and keyboard), named AJX:
I run the command to start listening for mouse and keyboard coming from Enfermera:


synergyc -f Enfermera.local

Note: Another awesome feature of synergy is that your linked computers will share clipboards allowing you to copy from one computer and paste into the other.

Note: I was able to install this on Mac OS X 10.4 and Mac OS X 10.5 (both using sudo port install synergy), the two operating systems had no problems talking to each other through synergy.

Escape code for html <pre> or <code> tag into clipboard

Thursday, August 13th, 2009

Here’s a speed up for posting code in a <pre> or <code> tag in an html page or blog. The script escapes all less than and greater than symbols (< with &lt; and > with &gt;) in a given file then puts the results in the clipboard to facilitate pasting into a text area or text editor. Save this in a file called escape-copy.sh:


#!/bin/bash
cat "$1" | sed s/\</\\\&lt\;/g | sed s/\>/\\\&gt\;/g | pbcopy

Posting the above code was super easy now, I just ran ./escape-copy.sh escape-copy.sh and pasted it in a <pre> tag.