Clipboard operations in MACOs with Objective C
Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie
Discuss newsgroups and documents
Daily nonsense
The access volume in the article "Mercurial GUI tool murky trial in MACOs" shows that there are still a few programmers who use MACOs in China after all ............ Just as I did not know much about Linux .....
Haha, but the number of readers is small, the work has to go on, and the learning has to go on. If I write to me, I will talk to myself when no one looks at it. (In fact, some people often talk to themselves -_-!)
Preface
I used to give up Windows Live writer to write blogs, but switched to Google document. Therefore, even on the MACOs platform, it has no significant impact on blog writing, there is only one problem. For technical blogs, some code is inevitable. wlw has many code highlighting plug-ins available, but Google Docs does not. Therefore, after turning to Google docs to write a blog, relying heavily on self-written code highlighting tools (see the one-click syntax highlighting tool release in the clipboard), this tool is actually a bug where someone raises win7, I can't try it because my win7 is deleted. At least it means someone except me has used ^. Actually, to tell the truth, it's so easy to use, the latest source code even solved the problem in the pop-up window. At that time, for convenience, some platform-related features were used and completed using pyqt, so now it's useless in MACOs ..... Depressed. solve this problem first.
I can still use QT, which saves a lot of trouble and does not need to learn some things. I did this on Windows and Linux platforms, but I just got started with MACOs, for the purpose of learning, I still use Objective C with cocoa to complete the process. Given that there are not many MACOs players in China, there will be fewer Objective C players, don't look too much forward to this article ............
Because of the above reasons, this article will naturally not be able to achieve syntax highlighting. Sorry, I am glad to find that the code in xcode is copied to use the RTF format, although Google Docs does not know how to enable highlight (read in RTF format), the indent format is still in progress. Unfortunately, we are lucky.
Really start
As mentioned in reference 2, the clipboard service in MACOs is an independent process named/usr/bin/pboard. Verify that:
$ PS-A | grep pboard
81 ?? 0: 00. 00/usr/sbin/pboard
408 ttys000. 00 grep pboard
It seems that there is no error.
Let's take a look at the basic usage:
I didn't find a way to program using the MACOs clipboard in the command line, so I used the window program for a simple test. I first used xcode to create a cocoa program. Here I named it macpasteboardtest,
Then, use interface builder to drag a multi-line text filed to display the clipboard content. (Clipboard in windows and pasteboard in MACOs .) Too troublesome. Buttons are saved. Bind the action directly through the menu. You can also use the shortcut key :)
In addition, to simplify the program, I directly use the delegate class of the automatically generated program and do not create my own class.
Macpasteboardtestappdelegate. h:
# Import
@ Interface macpasteboardtestappdelegate: nsobject {
Nswindow * window;
Nstextfield * textfield;
}
@ Property (assign) iboutlet nswindow * window;
@ Property (nonatomic, retain) iboutlet nstextfield * textfield;
-(Ibaction) Cut :( ID) sender;
-(Ibaction) Copy :( ID) sender;
-(Ibaction) paste :( ID) sender;
@ End
Macpasteboardtestappdelegate. m
# Import "macpasteboardtestappdelegate. H"
@ Implementation macpasteboardtestappdelegate
@ Synthesize window;
@ Synthesize textfield;
-(Void) applicationdidfinishlaunching :( nsnotification *) anotification {
// Insert code here to initialize your application
}
-(Void) writetopasteboard :( nspasteboard *) Pb withstring: String {
[Pb declaretypes: [nsarray arraywithobject: nsstringpboardtype]
Owner: Self];
[Pb setstring: String fortype: nsstringpboardtype];
}
-(Bool) readfrompasteboard :( nspasteboard *) Pb {
Nsarray * types = [Pb types];
If ([types containsobject: nsstringpboardtype]) {
Nsstring * value = [Pb stringfortype: nsstringpboardtype];
[[Self textfield] setstringvalue: value];
Return yes;
}
Return no;
}
-(Ibaction) Cut :( ID) sender {
[Self copy: sender];
[[Self textfield] setstringvalue: @ ""];
}
-(Ibaction) Copy :( ID) sender {
Nspasteboard * pb = [nspasteboard generalpasteboard];
[Self writetopasteboard: Pb withstring: [textfield stringvalue];
}
-(Ibaction) paste :( ID) sender {
Nspasteboard * pb = [nspasteboard generalpasteboard];
If (! [Self readfrompasteboard: Pb]) {
Nsbeep ();
}
}
@ End
Then, bind the cut, copy, and paste actions corresponding to the menu to the corresponding actions of the delegate class. (In this case, there are too many people to know. I don't know anyone who doesn't know this. If you don't know this, you can go to a hello World tutorial. that's all done .)
Here, the most important thing is to use the nspasteboard system class. All clipboard operations depend on it. After this implementation, it is almost the same as the procedure described in Chapter 21 in Section 2. In this example, considering the selected string and other conditions is troublesome, all operations are directly targeted at the entire string of the control, so it is a bit out of line with the Operation habits. Pay attention to this because the current topic is the clipboard.
Type of clipboard content
It is a pity that the original book has just ended. I need more, so I need to further explore the clipboard function. My goal is to directly copy HTML content to Google Docs and be able to recognize it normally, because of my previous learning experience, we know that this involves the content type of the clipboard, for example, nsstringpboardtype in the above example. Reference 2 lists 17 types of types, but unfortunately there are no HTML types. I was surprised to check the document. Refer to 2, which is too old. MACOs 10.5 has the HTML type, however, the type string of the clipboard has greatly changed in MACOs 10.6. It should only be changed from a constant to a constant, and the usage should be similar. For compatibility, I am still using MACOs 10.5 that is very large.
Documentation:
Types for standard data (Mac operating OS X 10.5 and earlier)
The nspasteboard class uses the following common pasteboard data types.
Nsstring * nsstringpboardtype;
Nsstring * nsfilenamespboardtype;
Nsstring * nspostscriptpboardtype;
Nsstring * nstiffpboardtype;
Nsstring * nsrtfpboardtype;
Nsstring * nstabulartextpboardtype;
Nsstring * nsfontpboardtype;
Nsstring * nsulerpboardtype;
Nsstring * nsfilecontentspboardtype;
Nsstring * nscolorpboardtype;
Nsstring * nsrtfdpboardtype;
Nsstring * nshtmlpboardtype;
Nsstring * nspictpboardtype;
Nsstring * nsurlpboardtype;
Nsstring * nspdfpboardtype;
Nsstring * nsvcardpboardtype;
Nsstring * nsfilespromisepboardtype;
Nsstring * nsmultipletextselectionpboardtype;
Types for standard data (Mac OS X 10.6 and later)
The nspasteboard class uses the following constants to define UTIs for common pasteboard data types.
Nsstring * const nspasteboardtypestring;
Nsstring * const nspasteboardtypepdf;
Nsstring * const nspasteboardtypetiff;
Nsstring * const nspasteboardtypepng;
Nsstring * const nspasteboardtypertf;
Nsstring * const nspasteboardtypertfd;
Nsstring * const nspasteboardtypehtml;
Nsstring * const nspasteboardtypetabulartext;
Nsstring * const nspasteboardtypefont;
Nsstring * const nspasteboardtyperuler;
Nsstring * const nspasteboardtypecolor;
Nsstring * const nspasteboardtypesound;
Nsstring * const nspasteboardtypemultipletextselection;
Nsstring * const nspasteboardtypefindpanelsearchoptions;
It should be noted that nssring itself is a constant. Here the so-called constant from the extraordinary amount is actually for this pointer value. The difference from nsstring * To nsstring * const is that, the pointer to the nsstring * value can be changed to the point (although the content cannot be changed, it is still very dangerous for system constants. This should be a design defect, so MACOs 10.6 has been modified ), nsstring * const cannot be changed regardless of content or point. Similar to the const * and const * const in C ++. Here we will show you this danger (do not learn it) and try using the HTML type to see if it can achieve what I want to be compatible with Google Docs.
-(Void) applicationdidfinishlaunching :( nsnotification *) anotification {
// Insert code here to initialize your application
// Notice! It's uugly but legality.
Nsstringpboardtype = nshtmlpboardtype;
}
This code is actually legal. Do you say apple can't change it?
As shown in, the value of nsstringpboardtype has changed ...... Of course, I need this type, so I use it like this. (Do not follow suit) We can see that even Apple's framework designers will make some very low-level mistakes.
View the HTML clipboard Effect
Enter the above content. Note that the above content is HTML: copy and paste it in Google Docs, as shown below:
I copied it from macpasteboardtest.
Have you seen it? Google Docs is recognized, just as I previously implemented using QT in windows. Copying the clipboard of HTML content to Google Docs is directly recognized, rather than being output as a text constant, in fact, I can show you the effects of the syntax highlighting software of my MACOs in the future :)
//
// Macpasteboardtestappdelegate. m
// Macpasteboardtest
//
// Created by jtianling on 3/3/10.
// Copyright 2010 jtianling. All rights reserved.
//
# Import "macpasteboardtestappdelegate. H"
@ ImplementationMacpasteboardtestappdelegate
@ Synthesize window;
@ Synthesize textfield;
-(Void) Applicationdidfinishlaunching :( nsnotification *) anotification {
// Insert code here to initialize your application
// Notice! It's uugly but legality.
Nsstringpboardtype = nshtmlpboardtype;
}
-(Void) Writetopasteboard :( nspasteboard *) Pb withstring: String {
[Pb declaretypes: [nsarray arraywithobject: nsstringpboardtype]
Owner:Self];
[Pb setstring: String fortype: nsstringpboardtype];
}
-(Bool) Readfrompasteboard :( nspasteboard *) Pb {
Nsarray * types = [Pb types];
If([Types containsobject: nsstringpboardtype]) {
Nsstring * value = [Pb stringfortype: nsstringpboardtype];
[[SelfTextfield] setstringvalue: value];
ReturnYes;
}
ReturnNo;
}
-(Ibaction) Cut :(ID) Sender {
[SelfCopy: sender];
[[SelfTextfield] setstringvalue: @ ""];
}
-(Ibaction) Copy :(ID) Sender {
Nspasteboard * pb = [nspasteboard generalpasteboard];
[SelfWritetopasteboard: Pb withstring: [textfield stringvalue];
}
-(Ibaction) paste :(ID) Sender {
Nspasteboard * pb = [nspasteboard generalpasteboard];
If(! [SelfReadfrompasteboard: Pb]) {
Nsbeep ();
}
}
@ End
The results are good. :) the code highlighter of the MACOs version is not far away ....... Don't drool.
Weaknesses
I have made three versions of the syntax highlighting software and tried a lot of other code highlighter, and even the online version. In fact, the one I did before best meets my own needs, after all, I am targeting Google Docs, and I only need the HTML clipboard. However, there has always been a very imperfect place. My syntax highlighting relies entirely on VIM (of course, there is nothing bad about dependency). This is one of them. In addition, vim's syntax highlighting is indeed beautiful, but it is no longer able to restore VS/xcode/eclipse, but in fact no matter which IDE the above copies the code, in fact, the original highlighted information is actually there, but Google Docs is stupid (Google Docs is silliness). It can only identify HTML clipboard content and cannot recognize other content (such as RTF ).
For example, check whether the highlighted content of xcode syntax is displayed on the clipboard: (the show clipboard function of finder in MACOs 10.6 is used. Why? Why is it clipboard? Class names are all called pasteboard .... Alas, it is estimated that MACOs and xcode developers are not in the same batch ....)
In fact, syntax recognition requires more content than Vim ..... Pay Attention to the text in the cilpboard window. clipboard contents: Rich Text (RTF). How stupid Google Docs can't even recognize RTF .......... In fact, the best solution should be to develop plug-ins for Google docs to directly identify the RTF format, it is to create a RTF-> HTML software and then go to Google Docs ..... Please try it later.
Last
I wrote a technology blog on my Mac Book for the first time. It took me one night to get things done ........ Sorrow ...... After all, I am still not familiar with xcode and Objective C. Of course, the input in MACOs is not very good. Apple's laptop keyboard .... In fact, I feel a little hard and cannot reach the realm of flying .... Oh, it's far away.
Reference
1. By Stephen G. kochan, objective-C 2.0 Programming
2. Aaron hillegass, third edition of "cocoa programming for Apple Development"
3. Apple online documentation
The author of the original article retains the copyright reprinted. Please indicate the original author and give a link
Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie