#!/bin/sh # Markko setup for AI coding tools — https://markko.app/docs/ai-tools/ # # Adds one hook per tool: after the agent writes a Markdown file (.md, .markdown, .mdown), # the hook runs `open -g -a Markko `, so the file opens in Markko (in the background). # # curl -fsSL https://markko.app/setup | sh set up # curl -fsSL https://markko.app/setup | sh -s -- --dry-run show the changes, make none # curl -fsSL https://markko.app/setup | sh -s -- --uninstall remove the hooks again # # The only files it changes (each only when the tool is installed): # Claude Code ~/.claude/settings.json (skipped when the Markko plugin is installed) # Cursor ~/.cursor/hooks.json # Windsurf ~/.codeium/windsurf/hooks.json (Windsurf, now Devin Desktop) # Before a change it saves a copy next to the file: .markko-backup-. # It never uses sudo. Running it again changes nothing. The hooks carry the tag "markko-setup". set -eu MODE=install for arg in "$@"; do case "$arg" in --dry-run) MODE=dry-run ;; --uninstall) MODE=uninstall ;; -h|--help) sed -n '2,18p' "$0" 2>/dev/null || echo "See https://markko.app/docs/ai-tools/"; exit 0 ;; *) echo "Unknown option: $arg (use --dry-run or --uninstall)" >&2; exit 2 ;; esac done if [ "$(id -u)" = 0 ]; then echo "Do not run this script as root or with sudo. It changes files in your home folder only." >&2; exit 1; fi if [ "$(uname -s)" != Darwin ]; then echo "Markko is a Mac app. This script runs on macOS only." >&2; exit 1; fi command -v osascript >/dev/null 2>&1 || { echo "osascript is missing (it is part of macOS)." >&2; exit 1; } STAMP=$(date +%Y%m%d-%H%M%S) TAG=markko-setup CHANGED=0 # The hook command for one tool. $1: the key path of the file in the hook's JSON input. hook_cmd() { printf '%s' "f=\$(plutil -extract $1 raw -o - - 2>/dev/null) || exit 0; case \"\$f\" in *.md|*.markdown|*.mdown) open -g -a Markko \"\$f\" >/dev/null 2>&1 ;; esac; exit 0 # $TAG" } # Edit one JSON config file with JavaScript for Automation (part of macOS): add or remove the hook. # Args: file, tool (claude|cursor|windsurf), action (add|remove), hook command. # Prints "changed" or "unchanged"; with action "check" it only reports. json_edit() { osascript -l JavaScript - "$@" <<'JXA' ObjC.import('Foundation'); function run(argv) { const [file, tool, action, cmd] = argv; const fm = $.NSFileManager.defaultManager; let data = {}; if (fm.fileExistsAtPath(file)) { const text = ObjC.unwrap($.NSString.stringWithContentsOfFileEncodingError(file, $.NSUTF8StringEncoding, null)); if (text && text.trim()) data = JSON.parse(text); } const tagged = (h) => typeof h.command === 'string' && h.command.includes('markko-setup'); let before = JSON.stringify(data); if (tool === 'claude') { data.hooks = data.hooks || {}; let list = (data.hooks.PostToolUse || []).map((m) => ({ ...m, hooks: (m.hooks || []).filter((h) => !tagged(h)) })).filter((m) => m.hooks.length); if (action === 'add') list.push({ matcher: 'Write|Edit|MultiEdit', hooks: [{ type: 'command', command: cmd }] }); if (list.length) data.hooks.PostToolUse = list; else delete data.hooks.PostToolUse; if (!Object.keys(data.hooks).length) delete data.hooks; } else { const event = tool === 'cursor' ? 'afterFileEdit' : 'post_write_code'; if (tool === 'cursor' && action === 'add') data.version = data.version || 1; data.hooks = data.hooks || {}; let list = (data.hooks[event] || []).filter((h) => !tagged(h)); if (action === 'add') list.push({ command: cmd }); if (list.length) data.hooks[event] = list; else delete data.hooks[event]; if (!Object.keys(data.hooks).length) delete data.hooks; } // "add" on a file that already has exactly this hook: nothing to do. const had = before.includes('markko-setup'); if (action === 'add' && had && before.includes(JSON.stringify(cmd).slice(1, -1))) return 'unchanged'; if (action === 'remove' && !had) return 'unchanged'; if (action === 'check') return had ? 'present' : 'absent'; const out = $(JSON.stringify(data, null, 2) + '\n'); out.writeToFileAtomicallyEncodingError(file, true, $.NSUTF8StringEncoding, null); return 'changed'; } JXA } # One tool: $1 name, $2 config file, $3 tool id, $4 key path of the file in the hook input. setup_tool() { name=$1 file=$2 tool=$3 key=$4 dir=$(dirname "$file") if [ ! -d "$dir" ]; then echo " $name: not installed (no $dir), skipped"; return; fi cmd=$(hook_cmd "$key") if [ "$MODE" = uninstall ]; then if [ ! -f "$file" ] || [ "$(json_edit "$file" "$tool" check "")" = absent ]; then echo " $name: no Markko hook, nothing to remove"; return; fi cp "$file" "$file.markko-backup-$STAMP" json_edit "$file" "$tool" remove "" >/dev/null echo " $name: removed the Markko hook from $file (backup: $file.markko-backup-$STAMP)"; CHANGED=1; return fi if [ -f "$file" ] && [ "$(json_edit "$file" "$tool" check "")" = present ] && grep -F -q "$key raw" "$file"; then echo " $name: already set up in $file"; return fi if [ "$MODE" = dry-run ]; then echo " $name: would add a hook to $file:" echo " $cmd" return fi [ -f "$file" ] && cp "$file" "$file.markko-backup-$STAMP" json_edit "$file" "$tool" add "$cmd" >/dev/null if [ -f "$file.markko-backup-$STAMP" ]; then echo " $name: added the Markko hook to $file (backup: $file.markko-backup-$STAMP)" else echo " $name: created $file with the Markko hook"; fi CHANGED=1 } claude_plugin_installed() { f="$HOME/.claude/plugins/installed_plugins.json" [ -f "$f" ] && grep -q '"markko@markko"' "$f" } case $MODE in install) echo "Markko setup: adds a hook that opens Markdown files in Markko." ;; dry-run) echo "Markko setup (dry run): shows the changes and makes none." ;; uninstall) echo "Markko setup: removes the Markko hooks." ;; esac if claude_plugin_installed && [ "$MODE" != uninstall ]; then echo " Claude Code: the Markko plugin is installed, so settings.json stays as it is" else setup_tool "Claude Code" "$HOME/.claude/settings.json" claude tool_input.file_path fi setup_tool "Cursor" "$HOME/.cursor/hooks.json" cursor file_path setup_tool "Windsurf" "$HOME/.codeium/windsurf/hooks.json" windsurf tool_info.file_path echo " VS Code, JetBrains and other editors: no hook to add. Open a file with: open -a Markko " if [ "$MODE" = install ] && ! open -Ra Markko >/dev/null 2>&1; then echo echo "Markko is not installed yet. Get it from the Mac App Store: https://markko.app/" echo "The hooks do nothing until Markko is installed." fi if [ "$MODE" = install ] && [ $CHANGED = 1 ]; then echo echo "Done. Start a new session in each tool so that it loads the hook." fi exit 0