How I figured out how to publish an Apple Note online

While I wait for the day Apple adds a publish note link to Apple Notes, I have been trying to figure out how to do it with a workaround. This is the best I have come up with without having to involve another blogging service.

It’s not perfect, but here’s how I did it:

I found a script by Bear (another notes app) that will export your Apple Notes as HTML. I used that (and some ChatGPT) to get it to only export notes in a Posts folder in my Notes app:

Yesterday

Here’s the script:

set exportFolder to (choose folder) as string
-- Function to delete all .html files in the chosen folder
on deleteHTMLFilesInFolder(folderPath)
    tell application "Finder"
        set htmlFiles to every file of folder folderPath whose name ends with ".html"
        repeat with htmlFile in htmlFiles
            delete htmlFile
        end repeat
    end tell
end deleteHTMLFilesInFolder

-- Simple text replacing
on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs

    return subject
end replaceText

-- Get an HTML file to save the note in.  We have to escape
-- the colons, or AppleScript gets upset.
on noteNameToFilePath(noteName)
    global exportFolder
    set strLength to the length of noteName

    if strLength > 250 then
        set noteName to text 1 thru 250 of noteName
    end if

    set fileName to (exportFolder & replaceText(":", "_", noteName) & ".html")
    return fileName
end noteNameToFilePath

-- Delete all existing HTML files in the chosen folder
deleteHTMLFilesInFolder(exportFolder)

tell application "Notes"
    -- Limit the export to the "Posts" folder
    repeat with theNote in notes in folder "Posts" of default account
        set noteLocked to password protected of theNote as boolean
        set modDate to modification date of theNote as date
        set creDate to creation date of theNote as date

        if not noteLocked then
            -- File name composed only by note title
            set fileName to (name of theNote as string)
            set filepath to noteNameToFilePath(fileName) of me
            set noteFile to open for access filepath with write permission
            set theText to body of theNote as string
            set theContainer to container of theNote

            -- Export the folder containing the notes as tag in bear
            -- The try-catch overcomes a 10.15.7 bug with some folders
            try
                if theContainer is not missing value then
                    set tag to name of theContainer
                    set theText to ("" & theText & "#" & tag & "#") as string
                end if
            end try

            write theText to noteFile as Β«class utf8Β»
            close access noteFile

            tell application "Finder"
                set modification date of file (filepath) to modDate
            end tell
        end if

    end repeat
end tell

I saved this as an automator application that I can run anytime I want. Here’s a ZIP of that application. When you run it, it will export any notes in the Posts folder in Notes.app into a folder you choose.

CleanShot 2024-06-01 at 21.25.43@2x

This script will:

  • Export notes from a folder called Posts
  • Delete all *.html files in the folder you choose (in case you deleted anything)
  • Export all note into a .html file named with the title of the note

I then used shfs/MacFuse to mount an STFP folder on my mac:

How I figured out how to publish an Apple Note online.html

CleanShot 2024-06-01 at 21.20.52@2x

So, when I use the exporting app to select this folder, the new files will be uploaded via SFTP to a server automatically.

I then wrote a simple PHP script to list out the HTML files (stored in ./html/) and output the HTML when you select one, here’s that script (index.php):

<?php

$post = $_GET['post'] ?? '';

if ( ! empty( $post ) ) {
    show_post( $post );
} else {
    show_posts();
}

function get_base_post( $file ) {
    return basename( str_replace( '.html', '', $file ) );
}

function get_post_files() {

    // Notice if your site is notes.aubreypwd.com, mount the ./html folder on your Mac to export to.
    $files = glob( __DIR__ . '/html/*.html' );

    $sorted_files = array();

    foreach ( $files as $file ) {
        $sorted_files[ filemtime( $file ) ] = $file;
    }

    ksort( $sorted_files );

    return $sorted_files;
}

function show_posts() {

    ?>

    <?php the_header( 'Posts' ); ?>

    <div class="posts">
        <ul class="post-list">

                <?php foreach ( get_post_files() as $date => $file ) : ?>

                    <li>
                        <a href="?post=<?php echo get_base_post( $file ); ?>"><?php echo get_base_post( $file ); ?></a><br>
                        <small><span class="date"><date><?php echo date( 'm/d/Y', $date ); ?></date></span></small>
                    </li>

                <?php endforeach; ?>

            </ul>
    </div>

    <?php footer(); ?>

    <?php
}

function the_header( $title ) {
    ?>

    <!DOCTYPE html>
        <html lang="en"  data-theme="light">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">

            <title><?php echo $title; ?></title>

            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">

            <meta name="color-scheme" content="light dark" />

            <link
                rel="stylesheet"
                href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.blue.min.css"
            />

            <style>

                body {
                    /* zoom: 90%; */
                }

                .site-title a {
                    text-decoration: none;
                    color: black;;
                }

                .site-title {
                    font-size: 80%;
                    border-bottom: 1px solid #dadada;
                    padding-bottom: 30px;
                    padding-top: 30px;
                }

                .post-date {
                    margin-top: 30px;
                    padding-top: 30px;
                    border-top: 1px solid #dadada;
                }

                code {
                    display:block;
                    padding-left: 20px;
                    background: none;
                }

                .post h1.post-title {
                    margin-top: 20px;
                }

                .post > div {
                    margin-bottom: var(--pico-typography-spacing-vertical);
                }

                img {
                    border-radius: 3px;
                }

                .post-list {
                    padding-left: 0;
                }

                .post-list li {
                    list-style: none;
                    padding-bottom: 5px;
                }

            </style>

        </head>
        <body>

        <main  class="container">

            <header><h1 class="site-title"><a href="../">πŸ‘¨πŸ»β€πŸ’» Aubrey's Notes</a></h1></header>

    <?php
}

function footer() {
    ?>


        </main>
        </body>
    </html>

    <?php
}

function get_post_filename( $post ) {
    return __DIR__ . '/html/' . "{$post}.html";
}

function show_post( $post ) {

    if ( ! file_exists( get_post_filename( $post ) ) ) {

        show_posts();
        return;
    }

    ob_start();

    ?>

            <?php the_header( $post ); ?>

            <div class="post">

                <?php

                echo str_replace(
                    array(
                        '#Posts#',
                        '<div><tt',
                        '</tt></div',
                        "\t",
                        '<div><br></div>',
                    ),
                    array(
                        '',
                        '<code',
                        '</code',
                        '&nbsp;&nbsp;',
                        '',
                    ),
                    file_get_contents( get_post_filename( $post ) )
                );

                ?>

                <p class="post-date">
                    <strong>Posted on: </strong>
                    <date><?php echo date( 'm/d/Y', filemtime( get_post_filename( $post ) ) ); ?></date>
                </p>

            </div>

            <?php footer(); ?>

    <?php

    echo ob_get_clean();
}

It was dead-simple, but it worked! But now I can basically update any post in my Posts folder, run that export script, and just wait for a simple site to update!

CleanShot 2024-06-01 at 21.44.44@2x

As you can see, the images come out in base64, so there’s no files to manage (I’m fine with that for now).

But…

I wasn’t super happy with the solution, but wanted to share just in case it might be enough for someone else.

I left it up on notes.aubreypwd.com β†’

πŸ‘‹πŸ» Valet

After about three months of using LocalWP I switched back to Laravel Valet yesterday. LocalWP just made my workflow a bit tedious:

The Site Shell works, but I just wish wp worked in the site folder.

Turns out creating a site for each context (at work) resulted in about 20 sites being created this quarter. This turned out to feel really inefficient.

Blueprints turned out to be hard to maintain. Not that it’s super hard, but it became a nuncance to update them and prune old ones.

Don’t get me wrong, LocalWP is great. Totally. But here’s what I’m doing now:

I have one site in Valet for my work (affiliatewp-dev) and it’s setup just the way I like it. One WordPress install. One setup. No blueprints.

The repo I work on, mostly, is symlinked in my ~/Repos folder (I use ghq to manage repos). Now, I just have one repo… all my work… one place.

To “switch” between cases I just use a command that uses wp to export the DB and re-setup WP at a blank slate. I can switch back and forth through databases using a command I setup called wpdbs. It just exports the DB and imports the other by name. I can switch contexts this way much easier with one WP install and one repo cloned… and of course switch branches at will.

This was my old setup, and it turned out to feel more intuitive and easier to work with. I thought LocalWP would make development feel easier, but it didn’t.

If you’re curious about wpdbs checkout my dotfiles.

CMD+P & VSCode

What am I missing about VSCode? I don’t get why it’s so popular… I can’t even get it to go to a file in the directory tree using CMD+P?

VSCode even has its own tag on DEV!

πŸ’» 🚫

So I took down my external monitor.

About three months ago I hooked up my 27″ monitor, a bluetooth keyboard, some book-shelf speakers and was pretty happy… until I wasn’t… today.

The monitor is showing some signs of ghosting, and everyone once in a while it flickers. I really didn’t want the Apple Magic Keyboard (external) because of the janky arrow keys, so I bought what I thought was the best BT keyboard I could find. It takes 2.5 seconds to re-connect, it often “fights” with whatever signal my Magic Mouse was using, jets out some random characters, and finally connects. It doesn’t have touch-id so I have been manually typing my super-hard password everyday! I mis-type it about half the time. I had to use the bookshelf speakers remote control to switch it to Bluetooth before my computer would connect to it, so I also bought a dock and wired it, but I still had to turn them on….

I think you get the point. It just never works out for me when I try to do this… An external monitor is nice, but the setup always comes with some price I have to pay.

My MacBook Air has a screen and a keyboard ATTACHED to it, and it works…all the time!

So, I took all that shit down and am just back on my πŸ’»

…again.

It always feels weird going from this big-ass screen to this tiny 13″ one. My desk always feels a bit empty and barren too…it’s always nice to have your desk full of big tech πŸ’ͺ🏻! But, it just isn’t meant to be.

I missed βŒ¨οΈπŸ‘ˆπŸ» Touch ID so much.

How to disable the Lock key on a non-Mac Bluetooth Keyboard

I have been searching the Internet for the answer to this, and it turned out to be quite simple.

You don’t need Karabiner-Elements, just run:

defaults write com.apple.loginwindow DisableScreenLockImmediate -bool yes

That disabled the stupid Lock key above the Delete key! I hit this thing all the time, and I have a really complicated password!