Crossposting via CLI

Post to Mastodon, Bluesky, and LinkedIn from your terminal.

Crossposting via CLI
Published on in Blogvent & Zsh.

Day 4 of Blogvent, where I publish one post per day until Dec. 25th.

By now, you've read about my infatuation with automating posts to my socials and my command-line setup.

Well, what about combining them into one CLI command that posts to all of them? Here's a Zsh function I made to do just that:

Requirements

  • macOS or Linux running Zsh
  • Node.js and npm
  • A ~/.zshrc file

Install the @humanwhocodes/crosspost package globally.[^1]

npm install -g @humanwhocodes/crosspost

Copy and paste this command into your terminal. It adds a post function to the end of your ~/.zshrc, so only run it once.

cat >> ~/.zshrc <<'ZSHRC'
post() {
  # ── Credentials: DO NOT STORE LIKE THIS IN PRACTICE ──────────────
  # Create a Mastodon app @ https:///settings/applications
  local MASTODON_HOST="mastodon.social"
  local MASTODON_ACCESS_TOKEN="your-mastodon-token"

  # Create an App Password @ https://bsky.app/settings/app-passwords
  local BLUESKY_HOST="bsky.social"
  local BLUESKY_IDENTIFIER="you.bsky.social"
  local BLUESKY_PASSWORD="your-bluesky-app-password"

  # Create a LinkedIn app @ https://www.linkedin.com/developers/apps/
  local LINKEDIN_ACCESS_TOKEN="your-linkedin-token"
  # ────────────────────────────────────────────────────────────

  typeset -A names=(m Mastodon b Bluesky l LinkedIn)
  local -aU nets
  local a c msg file image image_url alt
  local -i i=1

  while (( i <= $# )); do
    a=${argv[i]}
    case "$a" in
      --mastodon)  nets+=(Mastodon) ;;
      --bluesky)   nets+=(Bluesky) ;;
      --linkedin)  nets+=(LinkedIn) ;;
      --file)      file=${argv[i+1]};      (( i++ )) ;;
      --image)     image=${argv[i+1]};     (( i++ )) ;;
      --image-url) image_url=${argv[i+1]}; (( i++ )) ;;
      --image-alt) alt=${argv[i+1]};       (( i++ )) ;;
      -*)
        if [[ "$a" =~ '^-[mbl]+$' ]]; then
          for c in ${(s::)a#-}; do nets+=($names[$c]); done
        fi ;;
      *) msg=$a ;;
    esac
    (( i++ ))
  done

  if (( ! ${#nets} )); then
    cat >&2 <<'EOF'
usage: post  [--image file | --image-url url] [--image-alt "alt"] ("message" | --file file.txt)

networks (at least one):
  -m, --mastodon
  -b, --bluesky
  -l, --linkedin
  combine short flags: -mb, -mbl
EOF
    return 1
  fi

  if [[ -n "$file" ]]; then
    [[ -f "$file" ]] || { echo "post: file not found: $file" >&2; return 1; }
    msg=$(<"$file")
  fi
  [[ -n "$msg" ]] || { echo "post: no message" >&2; return 1; }
  [[ -z "$image" || -f "$image" ]] || { echo "post: image not found: $image" >&2; return 1; }

  print -r -- "To:     ${(j:, :)nets}"
  [[ -n "$image" ]]     && print -r -- "Image:  $image${alt:+  (alt: $alt)}"
  [[ -n "$image_url" ]] && print -r -- "Image:  $image_url${alt:+  (alt: $alt)}"
  print -r -- "Length: ${#msg} chars"
  print -r -- "────────────────────────────────────────"
  print -r -- "$msg"
  print -r -- "────────────────────────────────────────"

  if ! read -q "?Send this post? [y/N] "; then
    print; print "Cancelled."
    return 1
  fi
  print

  MASTODON_HOST=$MASTODON_HOST \
  MASTODON_ACCESS_TOKEN=$MASTODON_ACCESS_TOKEN \
  BLUESKY_HOST=$BLUESKY_HOST \
  BLUESKY_IDENTIFIER=$BLUESKY_IDENTIFIER \
  BLUESKY_PASSWORD=$BLUESKY_PASSWORD \
  LINKEDIN_ACCESS_TOKEN=$LINKEDIN_ACCESS_TOKEN \
  crosspost "$@"
}
ZSHRC

Open ~/.zshrc and replace the placeholder values with your own credentials. You only need them for the networks you post to.

Reload your Zsh config.

source ~/.zshrc

Test it.

post -h

Try one network first.

post -m "Hello from my terminal!"

Once that works, post everywhere.

post -mlb "Conor is a genius!"

Profit.