-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnotes-app
More file actions
executable file
·3281 lines (2839 loc) · 74.7 KB
/
notes-app
File metadata and controls
executable file
·3281 lines (2839 loc) · 74.7 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
###############################################################################
# NOTE: Use /bin/bash to ensure script uses macOS Bash version.
#
# Notes.app CLI
#
# A command line interface for Notes.app.
#
# https://github.com/xwmx/notes-app-cli
#
# Based on Bash Boilerplate: https://github.com/xwmx/bash-boilerplate
#
# Copyright (c) 2020 William Melody • hi@williammelody.com
# GPLv2 • See LICENSE for details.
###############################################################################
###############################################################################
# Strict Mode
#
# More Information:
# https://github.com/xwmx/bash-boilerplate#bash-strict-mode
###############################################################################
set -o nounset
set -o errexit
set -o pipefail
set -o noglob
IFS=$'\n\t'
###############################################################################
# Platform Check
###############################################################################
if [[ ! "$OSTYPE" =~ ^darwin ]]
then
printf "notes-app-cli only works on macOS.\\nExiting...\\n"
exit 0
fi
###############################################################################
# Environment
###############################################################################
# $_VERSION
#
# The most recent program version.
_VERSION="0.0.7"
# $_ME
#
# Set to the program's basename.
_ME="$(basename "${0}")"
# $_MY_PATH
#
# Set to the program's full path.
# shellcheck disable=SC2034
_MY_PATH="$(cd "$(dirname "$0")"; pwd)/$(basename "${0}")"
# $_MY_PROJECT_NAME
#
# Set to the program's project name.
# shellcheck disable=SC2034
_MY_PROJECT_NAME="notes-app-cli"
# $_MY_GITHUB_REPO
#
# Set to the program's primary GitHub repository URL.
# shellcheck disable=SC2034
_MY_GITHUB_REPO="xwmx/notes-app-cli"
# extglob
#
# Enable extended pattern matching operators.
#
# https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html
shopt -s extglob
# $_SED_I_COMMAND
#
# `sed -i` takes an extension on macOS, but that extension can cause errors in
# GNU `sed`.
#
# NOTE: To use this command, call it with `"${_SED_I_COMMAND[@]}"`
#
# https://stackoverflow.com/q/43171648
# http://stackoverflow.com/a/16746032
if sed --help >/dev/null 2>&1
then # GNU
export _SED_I_COMMAND=(sed -i)
else # macOS
export _SED_I_COMMAND=(sed -i '')
fi
# $_NEWLINE
#
# Use ANSI-C quoted newline for string building.
_NEWLINE=$'\n'
###############################################################################
# .notes-apprc
###############################################################################
# .notes-apprc
#
# If a `.notes-apprc` file exists in `$HOME`, source it.
export _MY_RC_PATH="${_MY_RC_PATH:-${HOME}/.notes-apprc}"
if [[ -e "${_MY_RC_PATH}" ]]
then
_MY_RC_PATH="$(realpath "${_MY_RC_PATH}")"
source "${_MY_RC_PATH}"
fi
###############################################################################
# $EDITOR
###############################################################################
# Set default $EDITOR if one has not been set.
if [[ -z "${EDITOR:-}" ]]
then
if hash "code" 2>/dev/null
then
export EDITOR="code"
elif hash "subl" 2>/dev/null
then
export EDITOR="subl"
elif hash "mate" 2>/dev/null
then
export EDITOR="mate"
elif hash "nano" 2>/dev/null
then
export EDITOR="nano"
elif hash "vi" 2>/dev/null
then
export EDITOR="vi"
else
_die printf "\$EDITOR not found. Set the editor in ~/.notesrc\\n"
fi
fi
###############################################################################
# Debug
###############################################################################
# _debug()
#
# Usage:
# _debug printf "Debug info. Variable: %s\\n" "$0"
#
# A simple function for executing a specified command if the `$_USE_DEBUG`
# variable has been set. The command is expected to print a message and
# should typically be either `echo`, `printf`, or `cat`.
__DEBUG_COUNTER=0
_debug() {
if [[ "${_USE_DEBUG:-"0"}" -eq 1 ]]
then
__DEBUG_COUNTER=$((__DEBUG_COUNTER+1))
# Prefix debug message with "bug (U+1F41B)"
printf "🐛 %s " "${__DEBUG_COUNTER}"
"${@}"
printf "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\\n"
fi
}
# debug()
#
# Usage:
# debug "Debug info. Variable: $0"
#
# Print the specified message if the `$_USE_DEBUG` variable has been set.
#
# This is a shortcut for the _debug() function that simply echos the message.
debug() {
_debug echo "${@}"
}
###############################################################################
# Die
###############################################################################
# _die()
#
# Usage:
# _die printf "Error message. Variable: %s\\n" "$0"
#
# A simple function for exiting with an error after executing the specified
# command. The command is expected to print a message and should typically
# be either `echo`, `printf`, or `cat`.
_die() {
# Prefix die message with "cross mark (U+274C)", often displayed as a red x.
printf "❌ "
"${@}" 1>&2
exit 1
}
# die()
#
# Usage:
# die "Error message. Variable: $0"
#
# Exit with an error and print the specified message.
#
# This is a shortcut for the _die() function that simply echos the message.
die() {
_die echo "${@}"
}
###############################################################################
# Temp Directory
###############################################################################
export _MY_TEMP_DIRECTORY
_MY_TEMP_DIRECTORY="$(mktemp -d)"
# Usage: _tempfile_path <basename>
_tempfile_path() {
[[ -z "${1:-}" ]] && return 1
printf "%s/%s" "${_MY_TEMP_DIRECTORY}" "${1:-}"
}
# Usage: _my_cleanup_on_exit
_my_cleanup_on_exit() {
if [[ -n "${_MY_TEMP_DIRECTORY:-}" ]] &&
[[ -e "${_MY_TEMP_DIRECTORY}" ]]
then
rm -rf "${_MY_TEMP_DIRECTORY}"
fi
}
trap _my_cleanup_on_exit EXIT
###############################################################################
# Configuration
###############################################################################
# `$_MY_DEFAULT_ACCOUNT_NAME`
export _MY_DEFAULT_ACCOUNT_NAME="${_MY_DEFAULT_ACCOUNT_NAME:-iCloud}"
# `$_MY_DEFAULT_FOLDER_NAME`
export _MY_DEFAULT_FOLDER_NAME="${_MY_DEFAULT_FOLDER_NAME:-Notes}"
# `$_MY_HIGHLIGHT_COLOR`
#
# Default: 11 (yellow) for 256 color terminals, 3 (yellow) for 8 color.
#
# Set highlighting color. This should be set to an xterm color number, usually
# a value between 1 and 256. For a table colors and their numbers run:
#
# notes settings colors
#
# Supported Values: [0..255]
_set_my_highlight_color() {
local _colors=
_colors="$(tput colors)"
if [[ -n "${_colors}" ]] && [[ "${_colors}" -gt 8 ]]
then
export _MY_HIGHLIGHT_COLOR="${_MY_HIGHLIGHT_COLOR:-11}"
else
export _MY_HIGHLIGHT_COLOR="${_MY_HIGHLIGHT_COLOR:-3}"
fi
} && _set_my_highlight_color
###############################################################################
# Helpers
###############################################################################
# _alias_subcommand()
#
# Usage:
# _alias_subcommand <subcommand> <alias>
#
# Description:
# Create an <alias> of <subcommand>. NOTE: aliases also have to be added to
# the $_SUBCOMMANDS variable.
_alias_subcommand() {
local _subcommand="${1:-}"
local _alias="${2:-}"
if [[ -z "${_subcommand}" ]] || [[ -z "${_alias}" ]]
then
return 1
fi
local _subcommand_help
_subcommand_help="$(
printf "%s\\n" "${_subcommand}" | cut -d' ' -f1
)"
eval "desc \"${_alias}\" \"\$(_help '${_subcommand_help}')\""
eval "_${_alias}() { _${_subcommand} \"\${@}\"; }"
}
# _command_exists()
#
# Usage:
# _command_exists <name>
#
# Takes a potential command <name> as an argument and returns whether a command
# exists with that name.
#
# For information on why `hash` is used here, see:
# http://stackoverflow.com/a/677212
_command_exists() {
hash "${1}" 2>/dev/null
}
# _contains()
#
# Usage:
# _contains "$item" "${list[*]}"
#
# Returns:
# 0 If the item is included in the list.
# 1 If not.
_contains() {
local _test_list=(${*:2})
for __test_element in "${_test_list[@]:-}"
do
_debug printf "_contains() \${__test_element}: %s\\n" "${__test_element}"
if [[ "${__test_element}" == "${1}" ]]
then
_debug printf "_contains() match: %s\\n" "${1}"
return 0
fi
done
return 1
}
# _edit_file()
#
# Usage:
# _edit_file <path> [--no-wait]
#
# Description:
# Open the file in $EDITOR. Use the `-f` option in vim and `--wait` option in
# other GUI editors to wait until the file is closed in the editor before
# continuing, unless `--no-wait`.
_edit_file() {
local _file_path="${1:-}"
local _wait=1
for __arg in "${@:-}"
do
case "${__arg}" in
--no-wait)
_wait=0
;;
*)
if [[ -z "${_file_path}" ]]
then
_file_path="${__arg}"
fi
;;
esac
done
if [[ -z "${_file_path}" ]]
then
return 1
fi
if ((_wait)) &&
[[ "${EDITOR}" =~ mvim ]] ||
[[ "${EDITOR}" =~ gvim ]]
then
"${EDITOR}" -f "${_file_path}"
elif ((_wait)) &&
[[ "${EDITOR}" =~ code ]] ||
[[ "${EDITOR}" =~ mate ]] ||
[[ "${EDITOR}" =~ subl ]]
then
"${EDITOR}" --wait "${_file_path}"
else
"${EDITOR}" "${_file_path}"
fi
}
# _file_is_text()
#
# Usage:
# _file_is_text <path>
#
# Returns:
# 0 If file is text.
# 1 if not.
_file_is_text() {
local _file_path="${1:-}"
local _file_type="${_file_path##*.}"
# Avoid calling `file` for better performance.
[[ "${_file_type}" =~ ^html|latex|md|markdown|org|rst|textile|txt$ ]] ||
[[ "$(file -b --mime-type "${1:-}")" =~ ^text ]]
}
# _get_hash()
#
# Usage:
# _get_hash <path>
#
# Description:
# Generate a hash for the file at the given <path>.
_get_hash() {
local _hash
if _command_exists "md5sum"
then
_hash="$(md5sum "${1:-}" | awk '{ print $1 }')"
elif _command_exists "md5"
then
_hash="$(md5 -q "${1:-}")"
fi
printf "%s\\n" "${_hash}"
}
# _get_id_normalized()
#
# Usage:
# _get_id_normalized <id>
#
# Returns:
# Return a formatted id suitable for including in AppleScript.
_get_id_normalized() {
local _id="${1:-}"
if [[ -z "${_id:-}" ]]
then
return 1
elif [[ "$(_get_id_type "${_id}")" == "id" ]]
then
_id="\"${_id}\""
elif [[ "$(_get_id_type "${_id}")" == "number" ]]
then # enclose name in quotes
_id="\"${_id}\""
fi
printf "%s\\n" "${_id}"
}
# _get_id_selector()
#
# Usage:
# _get_id_selector <id>
#
# Returns:
# A formatted id suitable as a simple selector in AppleScript.
_get_id_selector() {
local _id="${1:-}"
if [[ -z "${_id:-}" ]]
then
return 1
elif [[ "$(_get_id_type "${_id}")" == "id" ]]
then
_id="id \"${_id}\""
elif [[ ! "$(_get_id_type "${_id}")" == "number" ]]
then # enclose name in quotes
_id="\"${_id}\""
fi
printf "%s\\n" "${_id}"
}
# _get_id_type()
#
# Usage:
# _get_id_type <id>
#
# Returns:
# id If id is a Notes.app id.
# name If the id is a name.
# number If the id is a number.
_get_id_type() {
local _id="${1:-}"
local _type=
if [[ -z "${_id:-}" ]]
then
return 1
elif [[ "${_id}" =~ x\-coredata\:\/\/ ]]
then
_type="id"
elif [[ "${_id}" =~ ^[[:digit:]]+$ ]]
then
_type="number"
else
_type="name"
fi
printf "%s\\n" "${_type}"
}
# _highlight()
#
# Usage:
# _highlight <string>
#
# Description:
# Use `tput` to highlight the given string.
export _TPUT_HIGHLIGHT_COLOR
_TPUT_HIGHLIGHT_COLOR="$(tput setaf "${_MY_HIGHLIGHT_COLOR}")"
export _TPUT_SGR0= && _TPUT_SGR0="$(tput sgr0)"
_highlight() {
local _input="${1:-}"
if [[ -z "${_input}" ]]
then
_die printf "Usage: _highlight <string>"
fi
printf "${_TPUT_HIGHLIGHT_COLOR:-}%s${_TPUT_SGR0:-}\\n" "${_input}"
}
# _interactive_input()
#
# Usage:
# _interactive_input
#
# Returns:
# 0 If the current input is interactive (eg, a shell).
# 1 If the current input is stdin / piped input.
_interactive_input() {
[[ -t 0 ]]
}
# _join()
#
# Usage:
# _join "," a b c
# _join "${an_array[@]}"
#
# Returns:
# The list or array of items joined into a string with elements divided by
# the optional separator if one is provided.
#
# More information:
# https://stackoverflow.com/a/17841619
_join() {
local _delimiter="${1}"
shift
printf "%s" "${1}"
shift
printf "%s" "${@/#/${_delimiter}}" | tr -d '[:space:]'
}
# _note_has_attachments()
#
# Usage:
# _validate_no_attachments <note>
#
# Returns:
# 0 If the <note> has attachments.
# 1 If not.
_note_has_attachments() {
local _note_name="${1:-}"
if [[ -z "${_note_name:-}" ]]
then
return 1
fi
local _note_attachments
_note_attachments="$(
_attachments "${_note_name}" --properties id
)"
[[ -n "${_note_attachments[*]:-}" ]]
}
# __option_value_is_present()
#
# Usage:
# __option_value_is_present "${variable}"
#
# Returns:
# 0 The argument is present and does not match an option flag.
# 1 The argument is blank or matches as an option flag.
__option_value_is_present() {
[[ -n "${1:-}" ]] && [[ ! "${1:-}" =~ ^- ]]
}
# _print_line()
#
# Usage:
# _print_line <text>
#
# Description:
# Print a line of dashes the length of <text>.
#
# More information:
# http://wiki.bash-hackers.org/commands/builtin/printf
_print_line() {
local _text="${1:-}"
local _text_length=0
local _line=
_text_length=${#_text}
printf -v _line '%*s' "${_text_length}"
printf "%s\\n" "${_line// /-}"
}
# _setup_folder()
#
# Usage:
# _setup_folder <folder>
#
# Description:
# Setup the folder to use for the subcommand.
_setup_folder() {
local _folder_name="${1:-}"
printf "%s\\n" "${_folder_name}"
}
# _spinner()
#
# Usage:
# _spinner <pid>
#
# Description:
# Display an ascii spinner while <pid> is running.
#
# Example Usage:
# ```
# _spinner_example() {
# printf "Working..."
# (sleep 1) &
# _spinner $!
# printf "Done!\\n"
# }
# (_spinner_example)
# ```
#
# More Information:
# http://fitnr.com/showing-a-bash-spinner.html
_spinner() {
local _pid="${1:-}"
local _delay=0.75
local _spin_string="|/-\\"
if [[ -z "${_pid}" ]]
then
_die printf "Usage: _spinner <pid>\\n"
fi
while ps a | awk '{print $1}' | grep -q "${_pid}"
do
local _temp="${_spin_string#?}"
printf " [%c] " "${_spin_string}"
_spin_string="${_temp}${_spin_string%${_temp}}"
sleep ${_delay}
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
# _validate_account_contains_folder()
#
# Usage:
# _validate_account_contains_folder <account> <folder>
#
# Returns:
# 0 If the <account> contains <folder>.
# 1 If not.
_validate_account_contains_folder() {
local _account_name="${1:-}"
local _folder_name="${2:-}"
if [[ -z "${_account_name:-}" ]] || [[ -z "${_folder_name:-}" ]]
then
return 1
fi
local _account_folders
_account_folders="$(
_folders \
--account "${_account_name}" \
--properties name
)"
_contains "${_folder_name}" "${_account_folders[@]}"
}
###############################################################################
# desc
###############################################################################
# desc()
#
# Usage:
# desc <name> <description>
# desc --get <name>
#
# Options:
# --get Print the description for <name> if one has been set.
#
# Examples:
# ```
# desc "list" <<HEREDOC
# Usage:
# ${_ME} list
#
# Description:
# List items.
# HEREDOC
#
# desc --get "list"
# ```
#
# Set or print a description for a specified command or function <name>. The
# <description> text can be passed as the second argument or as standard input.
#
# To make the <description> text available to other functions, `desc()` assigns
# the text to a variable with the format `$___desc_<name>`.
#
# When the `--get` option is used, the description for <name> is printed, if
# one has been set.
#
# NOTE:
#
# The `read` form of assignment is used for a balance of ease of
# implementation and simplicity. There is an alternative assignment form
# that could be used here:
#
# var="$(cat <<'HEREDOC'
# some message
# HEREDOC
# )
#
# However, this form appears to require trailing space after backslases to
# preserve newlines, which is unexpected. Using `read` simply requires
# escaping backslashes, which is more common.
desc() {
set +e
[[ -z "${1:-}" ]] && _die printf "desc(): No command name specified.\\n"
if [[ "${1}" == "--get" ]]
then # get ------------------------------------------------------------------
[[ -z "${2:-}" ]] && _die printf "desc(): No command name specified.\\n"
local _name="${2:-}"
local _desc_var="___desc_${_name}"
if [[ -n "${!_desc_var:-}" ]]
then
printf "%s\\n" "${!_desc_var}"
else
printf "No additional information for \`%s\`\\n" "${_name}"
fi
else # set ------------------------------------------------------------------
if [[ -n "${2:-}" ]]
then # argument is present
read -r -d '' "___desc_${1}" <<HEREDOC
${2}
HEREDOC
_debug printf "desc() set with argument: \${___desc_%s}\\n" "${1}"
else # no argument is present, so assume piped input
read -r -d '' "___desc_${1}"
_debug printf "desc() set with pipe: \${___desc_%s}\\n" "${1}"
fi
fi
set -e
}
###############################################################################
# help
###############################################################################
desc "help" <<HEREDOC
Usage:
${_ME} help [<subcommand> | -l | --long]
Options:
-l --long Include a list of subcommands and their descriptions.
Description:
Print the program help information. When a subcommand name is passed, print
the help information for the subcommand.
Shortcut Alias: \`h\`
HEREDOC
_help() {
if [[ -z "${1:-}" ]] || [[ "${1}" =~ ^-l$|^--long$ ]]
then
cat <<HEREDOC
Notes.app CLI
A command line interface for Notes.app on macOS.
Usage:
${_ME} accounts [show <account>] [--properties <prop1>,<prop2>]
${_ME} add [<name>] [--account <account>] [--body <body>] [--folder <folder>]
${_ME} attachments <note> [--account <account>] [--folder <folder>]
[--properties <prop1>,<prop2>]
${_ME} commands
${_ME} count [--account <account>] [--folder <folder>]
${_ME} delete <note> [--account <account>] [--folder <folder>]
${_ME} edit <note> [--account <account>] [--folder <folder>]
${_ME} export <path> [--account <account>] [--folder <folder>]
[--note <note>]
${_ME} folders [show <folder>] [--account <account>] [--folder <folder>]
[--properties <prop1>,<prop2>]
${_ME} list [--account <account>] [--folder <folder>]
[--properties <prop1>,<prop2>]
${_ME} show <note> [--account <account>] [--folder <folder>] [--open]
[--properties <prop1>,<prop2>]
${_ME} update <note> (--body <body> | --name <name>) [--account <account>]
[--folder <folder>]
${_ME} -h | --help | help [<subcommand>] [-l | --long]
${_ME} --version | version
Help:
${_ME} help [<subcommand>]
More Information:
https://github.com/${_MY_GITHUB_REPO}
HEREDOC
if [[ "${1:-}" =~ ^-l$|^--long$ ]]
then
cat <<HEREDOC
Subcommands:
(default)
help Display this help information.
version Display version information.
Program Options:
-h --help Display this help information.
--version Display version information.
HEREDOC
fi
else
desc --get "${@}"
fi
}
_alias_subcommand "help" "h"
###############################################################################
# Subcommands
###############################################################################
# accounts ####################################################################
desc "accounts" <<HEREDOC
Usage:
${_ME} accounts [show <account>] [--properties <prop1>,<prop2>]
Options:
--properties <prop1>,<prop2> A comma-separated list of property names to
include in the output. Set to 'all' to include
all properties.
Identifiers:
<account> can be identified with one of the following:
id The Notes.app core data id (starts with 'x-coredata://').
name The name property.
number The current sequence number.
Available Properties:
all
id
name
Subcommands:
(default) List all accounts.
show Show properties of <account>.
Description:
List accounts or show an account.
Examples:
# List accounts by name.
${_ME} accounts
# List accounts with ids and names.
${_ME} accounts --properties id,name
# Print the id and name of the iCloud account.
${_ME} accounts show "iCloud"
HEREDOC
_accounts() {
local _id=
local _properties=
local _subcommand="list"
while ((${#}))
do
local _arg="${1:-}"
local _val="${2:-}"
case "${_arg}" in
--properties)
if [[ -n "${_val:-}" ]] && [[ ! "${_val:-}" =~ ^- ]]
then
_properties="$(printf "%s\\n" "${_val}" | tr ',' ' ')"
else
_help "accounts"
return 1
fi
shift
;;
show)
if [[ "${_subcommand:-}" == "list" ]]
then
_subcommand="show"
fi
;;
*)
if [[ -z "${_id:-}" ]]
then
_id="${_arg:-}"
fi
;;
esac
shift
done
_debug printf "\${_properties}: %s\\n" "${_properties[*]:-}"
_debug printf "\${_subcommand}: %s\\n" "${_subcommand:-}"
local _script_footer=
local _script_header=
local -a _script_properties=()
if [[ "${_subcommand}" == "list" ]]
then
read -r -d '' _script_header <<HEREDOC || true
tell application "Notes"
set myAccounts to accounts
set myCounter to 0
repeat with theAccount in myAccounts
tell theAccount
set myCounter to myCounter + 1
-- log myCounter
HEREDOC
read -r -d '' _script_footer <<HEREDOC || true
end tell
end repeat
end tell
HEREDOC
if [[ "${_properties}" =~ id|^all$ ]]
then
_script_properties+=("set accountId to get id")
_script_properties+=("log Id")
fi
if [[ "${_properties}" =~ name|^all$ ]]
then
_script_properties+=("set accountName to get name")
_script_properties+=("log accountName")
fi
if [[ -z "${_script_properties[*]:-}" ]]
then # no properties were specified
_script_properties+=("set accountName to get name")
_script_properties+=("log accountName")
fi
local _script="\
${_script_header}
${_script_properties[*]:-}
${_script_footer}"
elif [[ "${_subcommand}" == "show" ]]
then
if [[ -z "${_id:-}" ]]
then
_help "show"
return 1
else
_id="$(_get_id_selector "${_id}")"
fi
if [[ -z "${_properties:-}" ]]
then # no properties were specified
_properties="all"
fi
read -r -d '' _script_header <<HEREDOC || true
tell application "Notes"
tell account ${_id}
HEREDOC
read -r -d '' _script_footer <<HEREDOC || true
end tell
end tell
HEREDOC
if [[ "${_properties}" =~ id|^all$ ]]
then
_script_properties+=("set folderId to get id")
_script_properties+=("log folderId")
fi
if [[ "${_properties}" =~ name|^all$ ]]
then
_script_properties+=("set folderName to get name")
_script_properties+=("log folderName")