-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdv
More file actions
executable file
·75 lines (68 loc) · 2.08 KB
/
Copy pathmdv
File metadata and controls
executable file
·75 lines (68 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
# mdv — open Markdown files in the mdv.app GUI.
#
# Usage:
# mdv Launch the app
# mdv FILE [FILE ...] Open file(s) in the app
# mdv - Read markdown from stdin and open it
# mdv -h | --help Show this help
# mdv --version Print bundle version
set -euo pipefail
# --- locate mdv.app -----------------------------------------------------------
# Search order:
# 1) $MDV_APP env var (if set and points to a bundle)
# 2) /Applications/mdv.app
# 3) ~/Applications/mdv.app
# 4) ../mdv.app relative to this script (dev layout: build/mdv.app)
# 5) `mdfind` query as last resort
find_app() {
if [ -n "${MDV_APP:-}" ] && [ -d "$MDV_APP" ]; then echo "$MDV_APP"; return; fi
for p in \
"/Applications/mdv.app" \
"$HOME/Applications/mdv.app" \
"$(cd "$(dirname "$0")/.." && pwd)/build/mdv.app" \
"$(cd "$(dirname "$0")/.." && pwd)/mdv.app"
do
[ -d "$p" ] && { echo "$p"; return; }
done
if command -v mdfind >/dev/null; then
local hit
hit="$(mdfind "kMDItemCFBundleIdentifier == 'com.mdv.app'" 2>/dev/null | head -1)"
[ -n "$hit" ] && { echo "$hit"; return; }
fi
return 1
}
APP="$(find_app)" || { echo "mdv: mdv.app not found (set MDV_APP or install to /Applications)" >&2; exit 1; }
usage() {
sed -n '2,9p' "$0" | sed 's/^# \{0,1\}//'
}
# --- arg handling -------------------------------------------------------------
case "${1:-}" in
-h|--help)
usage; exit 0 ;;
--version)
/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$APP/Contents/Info.plist"
exit 0 ;;
esac
if [ $# -eq 0 ]; then
open "$APP"
exit 0
fi
# Read stdin into a temp .md and open that
if [ "$1" = "-" ] && [ $# -eq 1 ]; then
tmp="$(mktemp -t mdv-stdin).md"
cat - > "$tmp"
open -a "$APP" "$tmp"
exit 0
fi
# Resolve relative paths so LaunchServices opens what the user meant.
files=()
for f in "$@"; do
if [ -e "$f" ]; then
files+=("$(cd "$(dirname "$f")" && pwd)/$(basename "$f")")
else
echo "mdv: no such file: $f" >&2
exit 1
fi
done
open -a "$APP" "${files[@]}"