Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

pudge (1)

pudge
  (email not shown publicly)
http://pudge.net/
AOL IM: Crimethnk (Add Buddy, Send Message)

I run this joint, see?

Journal of pudge (1)

Wednesday July 12, 2006
12:35 AM

Stupid Mac::Glue Tricks: Clipboard Cleaning

[ #30258 ]

I hate that the Mac likes to put styled text on the clipboard and use it everywhere. Like when I am in Safari, I ctrl-click a link and select "Copy Link," and I get styled text of the link text that is hyperlinked. I do not want that. I want the link. So I have to paste it into Safari or BBEdit, then select it and copy it again, so I can paste it into iChat without being retarded.

And I don't care if you like it that way! I don't.

So I figured I'd use this simple AppleScript:

set the clipboard to (the clipboard as string) as string

It seemed to work, except that it still carried some text style information in it. I have no idea why. So I tried the equivalent Perl version:

#!/usr/bin/perl
use Mac::Glue ':all';
my $glue = new Mac::Glue 'Finder';
$glue->set_the_clipboard_to($glue->the_clipboard(as => 'string'));

That works. Why?

The returned Apple event does not return merely the text, but a record containing text and some default style information. The Mac::Glue version knows you want the text and sucks that part out, but the AppleScript version keeps the style information for you. Once I figured this part out, it was a matter of duplicating that behavior in AppleScript (I could leave it in perl, but the perl version can take a couple of seconds, and I'll be putting this in the Script Menu and calling it regularly to clean my clipboard).

So here's the final version, which first converts the returned clipboard value to a record, then extracts the key containing the text:

set the clipboard to «class ktxt» of ((the clipboard as string) as record)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • It's cool, but there's a much easier way:
    $ pbpaste | pbcopy
    • That's not easier for this purpose, which is to just run a script while I am in the GUI. That would execute more slowly than the AppleScript version, if I put it in Script Menu. It's less characters, though!
      • Fair enough. I figure most people with OS X don't know about pbcopy/pbpaste - two of the most useful tools on a mac IMHO.