Xcode Pasteboard Accumulator

February 1st, 2007

Way back in the MPW days, I used to rely heavily on an extension for the IDE that put powerful pasteboard manipulation tools in the window header. Essentially these tools let you treat your pasteboard like a stack, so you could easily accumulate multiple copies and then paste them all out at once.

For some dumb reason, I’ve been living without such conveniences for several years now. I know there are many 3rd party products that add an epic amount of functionality to the standard copy and paste of the system, but I’m not really interested in running a separate application for this purpose.

It occurred to me that this would be a perfect use of Xcode’s Menu Scripts. Xcode not only supports the addition of custom scripts to the menu bar, but supports a unique format by which the scripts can advertise keyboard shortcuts, and integrate seamlessly with Xcode’s text editor. For instance, you can use a script to process the selected text in Xcode and spit back out something else in its place.

Append To Pasteboard is a simple python script that takes advantage of the “pbcopy” and “pbpaste” commands to tack whatever you’ve selected in Xcode on to the end of the existing pasteboard. You stick this within your (horribly located) user scripts folder, where you must name the folders and files with numeric prefixes to inform Xcode where they belong. For example, the script lives in my home directory:

/Users/daniel/Library/Application Support/
	Apple/Developer Tools/Scripts/
	10-User Scripts/1-DCJ/

Unfortunately Xcode doesn’t handle the situation where some scripts come from your home directory, and some come from the /Library installation directory. So I put everything in my home directory. The “10-User Scripts” is how I tell it to name the menu bar item “User Scripts.” The “1-DCJ” folder is the folder with all my custom scripts in it, and since it is “1,” it comes before all of Apple’s standard scripts that I copied in (10-Open, 20-Search, etc). The process of providing scripts to Xcode is ugly but when you finally get it all put together, this is what you end up with:

Now it’s a snap to cruise through a .m and accumulate method declarations, for pasting into a corresponding .h. Note that the script always appends a newline to the existing pasteboard, which could be annoying depending on what you want to use it for. Since I envision myself almost always double-clicking a function or method declaration and appending it to a list, I find this behavior to be most convenient.

Thanks to Jon Wight for helping with Python.

Update: Any productivity gained by using this macro was certainly lost as I tried desperately to debug Xcode’s latest propensity to “beachball” on launch. Finally, I attached with gdb and found a telling backtrace. Hmm? Yep, when I “zipped” the script for upload to the blog, I left a zipped copy in the scripts folder. Next time I launched Xcode, it choked big time on trying to treat that zip file as a script.

9 Responses to “Xcode Pasteboard Accumulator”

  1. Doug Dickinson Says:

    Here’s another of a similar nature which I find useful: Swop selection with pb (script at bottom). I’ll often want to swop two things, such as the parameters in a method definition. Imagine I’ve got:

    – (void)defrabutateMozzle:(NSString *)aMozzle pilker:(NSNumber *)aPilker quipe:(NSString *)aQuipe { … }

    and I want to swop the last two. I select pilker:(NSNumber *)aPilker and copy it (I find if I cut it, I sometimes forget where it was). Then I select quipe:(NSString *)aQuipe and do a Swop selection with pb – the selected text is copied to the pb and what was on the pb is put into its place. So now, there are two pilkers and no quipe (except on the pb). Finally I select the first pilker:(NSNumber *)aPilker and do a paste.

    I normally assign Swop selection with pb to control-command-v. And here’s the shell script to do it; perform similar steps to the above to get it into the Script’s menu:

    —–8

  2. Doug Dickinson Says:

    Here’s the script for the Swop selection with pb; not quite sure why it got swopped out of the previous posting:

    #! /bin/sh
    #
    # — PB User Script Info —
    # %%%{PBXName=Swop selection with pb}%%%
    # %%%{PBXInput=Selection}%%%
    # %%%{PBXOutput=ReplaceSelection}%%%
    # %%%{PBXArgument=terminal=”\n”}%%%

    pbpaste
    pbcopy

  3. Michael McCracken Says:

    I know you said you’re not interested in running another application, but I still feel like I have to mention PTHPasteboard – it’s at least as important to my mac usage as Quicksilver.

    It solves your problem nicely, and gives you a way to visually inspect your pasteboard, swap things around on it, and *search* your pasteboard stack. This is one of those things where you never knew how much you needed it. At least it was for me…

    http://pth.com/products/pthpasteboard/

  4. Daniel Jalkut Says:

    Doug: cool idea – I think I’ll implement something similar!

    Michael: Thanks for the link – you certainly can’t beat the price. I’ll check that out.

  5. Corey Says:

    Quicksilver has a ‘clipboard history’ plugin that could do this as well. Use cmd-l (lowercase L) in QS once it’s installed.

  6. Nehemiah Says:

    :-) hehe, copy, THEN zip. things will go much better then.

  7. Jim DeVona Says:

    Neat. You can also drop this script on ThisService to make it available as a service to almost every application instead of just Xcode. The script works as a service without modification (except I’ve found that text is “cut” in TextWrangler instead of copied – but TextWrangler has built-in “Copy & Append” functionality anyway). Then the only challenge that remains is selecting a reasonable keyboard shortcut that won’t conflict with any of your other apps!

  8. Jim DeVona Says:

    Correction to my previous comment: the script does work fine packaged as a service with TextWrangler, too, as long as you indicate that it acts only on input, instead of both input and output as I had first tried. Logically, TextWrangler was just replacing the selected text with the nothing returned by the script, whereas other Cocoa text fields seemed to know not to bother if nothing was indeed returned.

  9. Faried Nawaz Says:

    I really really really want a selection buffer stack for X. I wonder if it’s possible to do it without hacking the X server.

Comments are Closed.

Follow the Conversation

Stay up-to-date by subscribing to the Comments RSS Feed for this entry.