6

First of all, I know there is a question with an identical name, however it deals with c++ and not c.

Is there any way to set a string to the clipboard in c?

This is the mentioned question if anyone is curious, even though it is for windows.

I need it to be in c because I am writing a program in c, and I would like to copy a string to the clipboard.

printf("Welcome! Please enter a sentence to begin.\n> ");
fgets(sentence, ARR_MAX, stdin);   
//scan in sentence
int i;
char command[ARR_MAX + 25] = {0};
strncat(command, "echo '",6);
strncat(command, sentence, strlen(sentence));
strncat(command, "' | pbcopy",11);
command[ARR_MAX + 24] = '\0';
i = system(command); // Executes echo 'string' | pbcopy

The above code is saving 2 new lines in addition to the string. ARR_MAX is 300.

Community
  • 1
  • 1
user1753491
  • 311
  • 1
  • 3
  • 11
  • The question you linked to is for Windows. You have tagged your question for OS X. Those are, of course, completely different. Please clarify your question. Also, can you explain **why** it's important to use the C language? – Ken Thomases Nov 20 '14 at 23:34
  • 1
    I have added a short function which do exactly what you want. without using strncat which seems retarded to me. – rowan.G Nov 22 '14 at 05:19

1 Answers1

2

you tagged your question for osx. so this should be sufficient: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PasteboardGuide106/Articles/pbCopying.html#//apple_ref/doc/uid/TP40008102-SW1

however there is the problem of having to call non-native c. Whether this is directly possible I don`t know.

if you can accept some hacky behavior you could invoke the pbcopy command.

http://osxdaily.com/2007/03/05/manipulating-the-clipboard-from-the-command-line/

this would be very easy to implement. here is a short function which should copy to clipboard. But I dont have osx handy so cannot test myself

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int copytoclipboard(const char *str) {

    const char proto_cmd[] = "echo '%s' | pbcopy";

    char cmd[strlen(str) + strlen(proto_cmd) - 1]; // -2 to remove the length of %s in proto cmd and + 1 for null terminator = -1
    sprintf(cmd ,proto_cmd, str);

    return system(cmd);
}

int main()
{
    copytoclipboard("copy this to clipboard");

    exit(0);
}
rowan.G
  • 717
  • 7
  • 13
  • 1
    Thanks, isn't that objective c? – user1753491 Nov 20 '14 at 23:24
  • yes it is. I have no experience with objective c so can't really give any more help on how you would go about using it with c. But I think it is a superset of c so it if you're lucky it could be quite simple. – rowan.G Nov 20 '14 at 23:25
  • 2
    So your answer boils down to: Use objective-c instead, here is a link to official docs... – Deduplicator Nov 20 '14 at 23:27
  • not really. Most languages support interfacing with c in one way or another. – rowan.G Nov 20 '14 at 23:31
  • There used to be a Carbon Pasteboard Manager, which was C, but this disappeared in the 10.5 days. You can get an `NSPasteboard` pointer with the objective-C runtime API, which is also just C, but you do at least need the ObjC runtime in order for the functions to work. The version of `pbcopy` that ships with OS X now links against Cocoa so Apple's using ObjC too. Use Swift! – iluvcapra Nov 22 '14 at 06:03
  • I have no experience with the entire apple development environment. maybe you can answer his question better than me? – rowan.G Nov 22 '14 at 14:33
  • This `copytoclipboard` code is awesome. I love it. quick and neat – Rick Nov 02 '21 at 08:19