Search code examples
applescriptclipboard

Paste SVG and other formats from clipboard on Mac


I know I can get PNG data from the clipboard on Mac like this:

osascript -e "get the clipboard as «class PNGf»"

Is it possible to get other types of data like for example SVG?


Solution

  • It turns out that - after copying from inkscape -

    osascript -e 'clipboard info' 
    

    returns

    «class PDF », 980, «class svg », 4256, JPEG picture, 807, TIFF picture, 180206, «class EPSF», 2457, «class HTML», 318, «class PNGf», 633, «class 8BPS», 6964, GIF picture, 322, «class jp2 », 3445, «class BMP », 135054, «class TPIC», 1818
    

    so that

    osascript -e 'get the clipboard as «class svg »'
    

    will return some sort of packed data, which appropriately massaged and decode will turn magically into SVG

    With perl:

    use Mojo::Util qw/decode encode/;
    use IPC::Run qw/run/;
    use strict;
    
    $\ = "\n"; $, = "\t";
    
    my ($stdin, $stdout, $stderr);
    
    run ["osascript", "-e", "get the clipboard as «class svg »"], \$stdin, \$stdout, \$stderr;
    $stdout = decode "UTF-8", $stdout;
    for ($stdout) {
        s/.+data ....//;
        s/.$//;
        $_ = pack('H*', $_);
    }
    
    print encode "UTF-8", $stdout;