Copy as displayed

@jessegrosjean , could you tell me if there any difference between Edit > Copy and Edit > Copy as displayed when there is no selection ? I would expect the latter to copy everything currently shown in the editor.

In the meanwhile, a default Copy All as displayed might, I think, look something like the script below, if run from an Execute JXA action in Keyboard Maestro, with output sent to the clipboard.

(Custom variants, for example with 4-space indentation, or outline syntaxes other than TaskPaper’s, could also be sketched as scripts)

(For other options, see Using Scripts · GitBook )

(() => {
    'use strict';

    // main :: IO ()
    const main = () => {
        const ds = Application('TaskPaper').documents;
        return either(alert('Problem'))(x => x)(
            bindLR(
                ds.length > 0 ? (
                    Right(ds.at(0))
                ) : Left('No TaskPaper documents open')
            )(
                d => d.evaluate({
                    script: tp3Context.toString()
                })
            )
        );
    };

    // ---------------- TASKPAPER CONTEXT ----------------

    const tp3Context = (editor, options) => {
        const main = () =>
            Right(
                ItemSerializer.serializeItems(
                    editor.displayedItems,
                    editor
                )
            );

        // ------------------- GENERIC -------------------
        // Right :: b -> Either a b
        const Right = x => ({
            type: 'Either',
            Right: x
        });

        return main();
    }

    // ----------------------- JXA -----------------------

    // alert :: String => String -> IO String
    const alert = title =>
        s => {
            const sa = Object.assign(
                Application('System Events'), {
                    includeStandardAdditions: true
                });
            return (
                sa.activate(),
                sa.displayDialog(s, {
                    withTitle: title,
                    buttons: ['OK'],
                    defaultButton: 'OK',
                    withIcon: sa.pathToResource('TaskPaper.icns', {
                        inBundle: 'Applications/TaskPaper.app'
                    })
                }),
                s
            );
        };

    // --------------------- GENERIC ---------------------

    // Left :: a -> Either a b
    const Left = x => ({
        type: 'Either',
        Left: x
    });

    // Right :: b -> Either a b
    const Right = x => ({
        type: 'Either',
        Right: x
    });

    // bindLR (>>=) :: Either a -> 
    // (a -> Either b) -> Either b
    const bindLR = m =>
        mf => undefined !== m.Left ? (
            m
        ) : mf(m.Right);

    // either :: (a -> c) -> (b -> c) -> Either a b -> c
    const either = fl =>
        // Application of the function fl to the
        // contents of any Left value in e, or
        // the application of fr to its Right value.
        fr => e => 'Either' === e.type ? (
            undefined !== e.Left ? (
                fl(e.Left)
            ) : fr(e.Right)
        ) : undefined;

    return main();
})();
1 Like

It should be that both Copy and Copy Display do nothing when the selection is empty. I see that right now “Copy Display” isn’t disabled when there is no selection… it should be and I will fix that.

The difference between Copy and Copy Displayed is how they handled folded/filtered regions. For example if you fold items in your editor… and then “Select All”:

  1. Copy will copy the folded content that you’ve selected
  2. Copy Displayed will only copy the visible content that you’ve selected, folded/filtered content will not be copied.

I hope that helps!

2 Likes

Thank you, @jessegrosjean. That is helpful !

@complexpoint, thank you so much for the script and the outstanding things you share here ! Really appreciate it :slight_smile:

1 Like

“Copy Displayed” should now be disabled in latest release when selection is empty:

2 Likes