Listing attached / pasted files

Let’s say I have a document. Some of the notes contain photos that I’ve pasted inside them.

I was browsing through my file system the other day and noted that (for a TBX document that’s mostly text) the file size is rather large. I think it’s probably worth pruning out some of those photos (or at the very least reducing their file size before repasting).

Is there a way to list all ‘attached’ files which have been pasted into documents?

You can query for notes with a non-zero ImageCount attribute value.

You could also do this from Script Editor, copying to the clipboard a TaskPaper-formatted list of all notes in (the front Tinderbox document) which contain images, by running the following JavaScript code.

(with the language tab at top left of Script Editor set to JavaScript)

(() => {
    'use strict';

    // A Taskpaper-formatted list of the titles of all notes
    // (in the active Tinderbox document)
    // which contain images.

    // (The list is placed in the clipboard)

    const main = () => {

        const
            a = Application.currentApplication(),
            sa = (a.includeStandardAdditions = true, a);

        const
            ds = Application('Tinderbox 8').documents,
            strList = bindLR(
                0 < ds.length ? (
                    Right(ds.at(0))
                ) : Left('No documents open in Tinderbox'),
                d => 'Titles of notes with images:\n' + unlines(
                    foldTree(
                        notesWithImages,
                        pureTreeTBX(d)
                    )
                )
            );
        return (
            sa.setTheClipboardTo(strList),
            console.log(strList),
            strList
        );
    };

    // TINDERBOX ------------------------------------------

    // notesWithImages :: Tree a -> [String] -> [String]
    const notesWithImages = (note, xs) =>
        0 < note.attributes.byName('imageCount').value() ? (
            concat(xs.concat('- ' + note.name()))
        ) : concat(xs);

    // pureTreeTBX :: Note  -> Tree Note
    const pureTreeTBX = note => {
        const go = x =>
            Node(x, x.notes().map(go));
        return go(note);
    };

    // GENERIC FUNCTIONS ----------------------------------
    // https://github.com/RobTrew/prelude-jxa

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

    // Node :: a -> [Tree a] -> Tree a
    const Node = (v, xs) => ({
        type: 'Node',
        root: v, // any type of value (consistent across tree)
        nest: xs || []
    });

    // 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);

    // concat :: [[a]] -> [a]
    // concat :: [String] -> String
    const concat = xs => [].concat(...xs);

    // foldTree :: (a -> [b] -> b) -> Tree a -> b
    const foldTree = (f, tree) => {
        const go = node => f(node.root, node.nest.map(go));
        return go(tree);
    };

    // unlines :: [String] -> String
    const unlines = xs => xs.join('\n');

    // MAIN ---
    return main();
})();

1 Like

Perfect. Exactly what I was looking for. Thank you.

1 Like

FWIW, $ImageCount was a new system attribute for v6.2.0 . The v6 aTbRef baseline was actually v6.2.0 but I somehow missed out the addition of $ImageCount. It doesn’t turn up until aTbRef v7.
I’ve now cross-linked the aTbRef8 $ImageCount page to Exporting Images. This will hopefully make the attribute easier to find.

Edit - URL now fixed (and bug in aTbRef template found!)

Just a heads up that following the link results here in a Not Found error.

Try this.

1 Like

Hmm. Now found and fixed 2 separate bugs in my export templates.