REBOL [
    Title: "Clipboard (for Mac)"
    Author: "Christopher Ross-Gill"
    Version: 1.0.0
    Date: 19-Jan-2013
    File: %clipboard.r
    Purpose: "Clipboard Scheme for Mac"
    Needs: [2.7.8.2.5 shell]
    Usage: [
        read clipboard://
        write clipboard:// ["Clipboard " now]
    ]
]

unless in system/schemes 'clipboard [
    system/schemes: make system/schemes [clipboard: none]
]

system/schemes/clipboard: make system/standard/port compose [
    scheme: 'clipboard
    port-id: 0
    passive: none
    cache-size: 5
    proxy: make object! [host: port-id: user: pass: type: bypass: #[none]]
    handler: context [
        port-flags: system/standard/port-flags/pass-thru

        boards: ["general" "ruler" "font" "find"]

        init: func [port url][
            port/locals: any [
                all [
                    block? url
                    url: find/tail url [locals:]
                    find boards url: form pick url 1
                    url
                ]
                all [
                    url? url
                    url: find/match form url "clipboard::"
                    find boards url
                    url
                ]
                "general"
            ]
        ]

        open: func [port][port/state/flags: port/state/flags or port-flags]

        close: func [port][]

        copy: func [port][
            call/wait/output join "pbpaste -prefer txt -pboard " port/locals port/state/inbuffer: make string! 1024
            port/state/inbuffer
        ]

        insert: func [port values][
            call/wait/input join "pbcopy -pboard " port/locals port/state/outbuffer: repend make string! 1024 values
            port/state/outbuffer
        ]
    ]
]