-
Notifications
You must be signed in to change notification settings - Fork 9
/
itunes
executable file
·1244 lines (1118 loc) · 38.3 KB
/
itunes
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
#
# A simple command-line wrapper for Apple's iTunes.
#
# Install or symlink into your path and "itunes -h" or "itunes help" for help.
#
# Copyright 2012-2014 Sam Graham. http://www.illusori.co.uk/
# This work is licensed under a
# Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License
# http://creativecommons.org/licenses/by-sa/2.0/uk/
# In human-readble terms: you're free to copy, distribute and modify
# providing you maintain attribution and licence.
#
# Use at your own risk, no fitness for purpose implied, etc, etc.
version='2.1.0'
progname=`basename $0`
config_dir="$HOME/.bash-itunes"
plugins_dir="$config_dir/plugins"
#
# You can override the config values below in ~/.bash-itunes/config
#
# Filename of a mid3v2-compatible tags editor.
# If you use MacPorts rather than Brew you may need to set this to mid3v2-2.4 or something.
id3v2_editor="mid3v2"
# id3v2 tags record ratings against an email address so files can have ratings by multiple people.
# If this is something you care about, set your email address in this variable.
# This *will* embed your email address for everyone to read within the file metadata though.
# Some programs just embed the name of the player rather than use the email for this reason.
id3v2_email="Bash iTunes"
# Add the commented-out line to your ~/.bash-itunes/config if you're still using iTunes.app.
#itunes_app="iTunes"
itunes_app="Music"
# Version and help.
function _cmd_version() {
echo "$progname version $version"
}
function _help_help() {
_cmd_help
}
function _cmd_help() {
if [ -z "$*" ]; then
_cmd_version
echo "Usage: $progname [-v] [-h] [-d] <command> [<args>]
Switches:
-v Show version information and exit.
-h Show this help and exit.
-d Enable debug messages, additional -d options increase verbosity.
Navigation commands:
play Resume playing or choose track.
pause Pause playing.
stop Stop playing.
next Skip to next track.
prev Skip back to previous track.
info View info about current track.
search Search for tracks.
shuffle View or set shuffle status.
repeat View or set repeat status.
playlist View or choose playlist.
playlists List all playlists.
Volume commands:
vol[ume] View or adjust iTunes volume 'up', 'down' or percentage.
mute Mutes iTunes.
unmute Unmutes iTunes, restoring previous volume level.
sysvol[ume] View or adjust system volume 'up', 'down' or percentage.
sysmute Mute system sound.
sysunmute Unmute system sound.
Application commands:
open Start iTunes running.
quit Quit iTunes.
show Hide the iTunes window.
hide Show the iTunes window.
Editing commands:
rate Set the iTunes star rating.
heart Add heart/loved status to a track.
unheart Remove heart/loved status from a track.
sync Sync ratings to or from audio files files and iTunes database.
Additional commands:
version Show version information and exit.
help Show this help and exit.
plugins Show a list of plugins installed."
else
# Dispatch to _help_<cmd> if they've supplied a command name.
orig_cmd="$1"
shift
_normalize_cmd_name "cmd" "$orig_cmd"
if [ -z "$cmd" ]; then
_cmd_unknown "$orig_cmd"
else
# TODO: check the functions exists. Say no help available if it doesn't.
local something_shown=0
declare -f "_usage_${cmd}" >/dev/null
if [ "$?" = 0 ]; then
_usage_${cmd} "$@"
something_shown=1
fi
declare -f "_help_${cmd}" >/dev/null
if [ "$?" = 0 ]; then
if [ "$something_shown" = 1 ]; then
echo
fi
_help_${cmd} "$@"
something_shown=1
fi
if [ "$something_shown" = 0 ]; then
echo "No help found for '$progname $cmd', try the general help at '$progname help'."
fi
fi
fi
}
function _cmd_unknown() {
echo "Unknown command '$*', try '$progname help' for a list of valid commands."
}
# Helper functions
function _err() {
echo "$*" 1>&2
}
function _err_func() {
${1} 1>&2
}
function _log() {
if [ "$debug" -ge "$1" ]; then
shift;
_err "$*"
fi
}
function _pluralize() {
if [ "$1" = 1 ]; then
echo "$1 $2"
else
if [ -n "$3" ]; then
echo "$1 $3"
else
echo "$1 ${2}s"
fi
fi
}
# mid3v2 communication hackery.
function _find_id3v2_editor() {
if [ -n "is_id3v2_editor_installed" ]; then
# -s option to which isn't supported in older OS X.
which "$id3v2_editor" >/dev/null
is_id3v2_editor_installed=$[!$?]
fi
}
# AppleScript communication hackery.
function _quote_for_applescript_string() {
local value="$*"
value="${value//\\/\\\\}"
value="${value//\"/\\\"}"
_log 4 "Escaping '$*' to '$value' for use in applescript string"
echo "$value"
}
function _osascript() {
_log 2 "Run: osascript -e \"$*\""
if [ "$debug" -gt 0 ]; then
osascript -e "$*"
else
osascript -e "$*" 2>&-
fi
}
function _tell_itunes() {
_osascript "tell application \"${itunes_app}\" to $*"
}
function _ask_itunes() {
_osascript "tell application \"${itunes_app}\"
$*
set AppleScript's text item delimiters to \"\\n\"
ret as text
end tell"
}
# Fetch from iTunes from the data-structures in $1 into the array
# variables constructed from prefix $2 and column names $3+ the
# given columns in the data-structure.
# If that's clear.
# No?
# Anyone?
function _fetch_from_itunes() {
local source="$1"
local use_loop=0
if [ "$source" = "LOOP" ]; then
use_loop=1
source="$2"
shift
fi
local prefix="$2"
shift 2
# Yet more hacks, this allows multi-word AppleScript properties
# such as "database id" to end up in "database_id".
# You can also do this by messing with $IFS and ${var//search/replace}
# but, although I got it working, I had a brief outbreak of sanity and
# chose this instead...
local columns=''
local script_columns=''
local column_defaults=''
for column in "$@"; do
underscored_column="${column// /_}"
columns="${columns} ${underscored_column}"
script_columns="${script_columns}, ${column}"
# "missing value" is what AppleScript uses to stringify missing values, may as well emulate
column_defaults="${column_defaults}, ${underscored_column}:\"missing value\""
done
columns="${columns:1}"
script_columns="${script_columns:2}"
if [ $# = 1 ]; then
column_defaults='"missing value"'
else
column_defaults="{${column_defaults:2}}"
fi
_log 4 "source '$structure', prefix '$prefix', columns '$columns', script_columns '$script_columns'"
# Best if we don't loop within AppleScript, it can be painfully slow
# for large arrays, but on the other hand, for columns that are
# actually expressions rather than properties, we appear to have to loop.
# Same deal appears to be the case if we need to safeguard against properties
# that may or may not exist on the track objects (file location for example)
if [ "$use_loop" = 0 ]; then
local result=$(_ask_itunes "set ret to ($script_columns) of ($source)")
_log 4 "result '$result'"
_read_rows_from_value_lines_grouped_by_column "$prefix" "$result" $columns
else
# Error -1728 is property doesn't exist. It'd be nice to catch it on a per
# property basis, but that's a task for another day. Probably should rethrow
# the error if it isn't a -1728 too.
# Using ((properties of v) & $column_defaults was a possibility, but unfortunately
# the properties property on iTunes tracks doesn't actually include all the
# properties of the object, including some we want. AppleScript, I really hate you.
local result=$(_ask_itunes "set ret to {}
repeat with v in $source as list
try
copy ($script_columns) of v to the end of ret
on error errorMessage number errorNumber
if errorNumber = -1728 then
copy ($column_defaults) to the end of ret
end if
end try
end repeat")
_log 4 "result '$result'"
_read_rows_from_value_lines "$prefix" "$result" $columns
fi
}
# Low-level row/rowset fetching/clearing. This stuff is nasty.
# This function is lifted from my bash-snippets repo on GitHub,
# gawp in horror at its unspeakable glory.
function _read_rows_from_value_lines_grouped_by_column() {
local prefix="$1"
local content="$2"
shift 2
local columns
read -r -a columns <<< "$*"
local row_count=$(($(wc -l <<< "$content") / ${#columns[*]}))
for column in "${columns[@]}"; do
unset "${prefix}_${column}"
done
local row_idx=0
local column_idx=0
local value
while read value; do
column=${columns[column_idx]}
# Ho hum, I gave up and used eval, couldn't get this working with $(())
printf -v value "%q" "$value"
eval "${prefix}_${column}[${row_idx}]=$value"
row_idx=$((row_idx + 1))
if [ "$row_idx" -ge "$row_count" ]; then
row_idx=0
column_idx=$((column_idx + 1))
fi
#_log 5 "Column is $column, column_idx now $column_idx, row_idx now $row_idx, value $value"
done <<< "$content"
}
function _read_rows_from_value_lines() {
local prefix="$1"
local content="$2"
shift 2
local columns
read -r -a columns <<< "$*"
for column in "${columns[@]}"; do
#echo "Scrubbing ${prefix}_${column}"
unset "${prefix}_${column}"
done
local row_idx=0
local column_idx=0
local value
while read value; do
column=${columns[column_idx]}
# Ho hum, I gave up and used eval, couldn't get this working with $(())
printf -v value "%q" "$value"
eval "${prefix}_${column}[${row_idx}]=$value"
column_idx=$((column_idx + 1))
if [ "$column_idx" -ge "${#columns[*]}" ]; then
column_idx=0
row_idx=$((row_idx + 1))
fi
#_log 5 "Column is $column, column_idx now $column_idx, row_idx now $row_idx"
done <<< "$content"
}
function _scrub_rows() {
local prefix="$1"
shift
local columns="$@"
for column in $columns; do
local full_column="${prefix}_${column}"
_log 6 "Scrubbing \$$full_column"
unset $full_column
done
}
# Schema stuff.
function _load_schema() {
if [ "$supports_hearting" = "true" ]; then
_track_columns="name artist album year rating loved time id"
else
_track_columns="name artist album year rating time id"
fi
_playlist_columns="name time id"
}
# Fetchers/scrubbers for the objects in the schema.
function _fetch_tracks() {
_fetch_from_itunes "$@" $_track_columns
if [ "$1" = "LOOP" ]; then
shift
fi
local source="$1"
local prefix="$2"
# Can't just add location to _track_columns because not all tracks define it.
# (Streams, Home Sharing, etc)
# Can't just get only file tracks because then indexes don't line up.
# Can't just use the LOOP fetch safeguard either because we need to transform
# to POSIX path, which you can't do in an array copy.
# As usual AppleScript randomly decides it needs a temporary variable too.
# AppleScript, you never make life easy do you?
#_fetch_from_itunes LOOP "$source" "$prefix" "location"
local result=$(_ask_itunes "set ret to {}
repeat with v in $source as list
try
set loc to location of v
if loc is missing value then
copy \"missing value\" to the end of ret
else
copy POSIX path of loc to the end of ret
end if
on error errorMessage number errorNumber
if errorNumber = -1728 then
copy \"missing value\" to the end of ret
else
error \"Unhandled error: \" & errorMessage & \", number: \" & errorNumber number 1001
end if
end try
end repeat")
_log 4 "result '$result'"
_read_rows_from_value_lines "$prefix" "$result" "location"
}
function _scrub_tracks() {
local prefix="$1"
_scrub_rows "$prefix" $_track_columns
_scrub_rows "$prefix" "location"
}
function _fetch_playlists() {
local source="$1"
local prefix="$2"
_fetch_from_itunes "$@" $_playlist_columns
_fetch_from_itunes LOOP "$source" "$prefix" "count tracks"
}
function _scrub_playlists() {
local prefix="$1"
_scrub_rows "$prefix" $_playlist_columns
_scrub_rows "$prefix" count_tracks
}
# Fetchers/scrubbers for specific global objects.
function _fetch_current_track() {
if [ -z "$current_track_name" ]; then
_fetch_tracks "current track" current_track
_fetch_track_id3v2 "current track" current_track
fi
if [ -z "$current_track_pos" ]; then
current_track_pos=$(_tell_itunes "player position as integer")
local min=$((current_track_pos / 60))
local sec=$(printf "%02d" $((current_track_pos % 60)))
current_track_pos="${min}:${sec}"
_log 3 "Fetched trackpos '$current_track_pos'"
fi
}
function _scrub_current_track() {
_scrub_tracks "current_track"
unset "current_track_pos"
}
function _fetch_current_playlist() {
if [ -z "$current_playlist_name" ]; then
_fetch_playlists "current playlist" current_playlist
fi
}
function _scrub_current_playlist() {
_scrub_playlists "current_playlist"
}
function _fetch_track_id3v2() {
_find_id3v2_editor
if [ "$is_id3v2_editor_installed" != 1 ]; then
_log 1 "id3v2 editor not found."
return
fi
local source="$1"
local prefix="$2"
local row_idx="${3:-0}"
eval "local location=\${${prefix}_location[row_idx]}"
local rating=0
if [ -z "$location" -o "$location" = "missing value" ]; then
rating=0
else
# TODO: test with multple ratings
# TODO: should match same rating email as configured? or prefer at least?
rating=$($id3v2_editor -l "$location" | grep POPM | cut -f 3 -d '=' | cut -f 2 -d ' ' | cut -f 1 -d '/')
fi
#echo "rating '$rating'"
eval "${prefix}_id3v2_rating[${row_idx}]=\"$rating\"";
}
# Views.
function _show_track() {
local row_prefix="$1"
local row_idx="${2:-0}"
local brief="${3:-0}"
local preamble="$4"
# Ho ho.
eval "local name=\${${row_prefix}_name[row_idx]}"
eval "local artist=\${${row_prefix}_artist[row_idx]}"
eval "local album=\${${row_prefix}_album[row_idx]}"
eval "local year=\${${row_prefix}_year[row_idx]}"
eval "local rating=\${${row_prefix}_rating[row_idx]}"
eval "local time=\${${row_prefix}_time[row_idx]}"
eval "local id=\${${row_prefix}_id[row_idx]}"
eval "local loved=\${${row_prefix}_loved[row_idx]}"
eval "local location=\${${row_prefix}_location[row_idx]}"
eval "local id3v2_rating=\${${row_prefix}_id3v2_rating[row_idx]}"
if [ "$loved" = "true" ]; then
heart="<3 "
else
heart=""
fi
if [ -n "$preamble" ]; then
preamble="$preamble "
if [ -z "$heart" ] && [ "$supports_hearting" = "true" ]; then
heart=" "
fi
fi
echo -n "$preamble$heart\"$name\""
if [ -n "$artist" ]; then
echo -n " by $artist"
fi
if [ -n "$album" ]; then
echo -n ", from \"$album\""
if [ -n "$year" -a "$year" != 0 ]; then
echo -n " ($year)"
fi
fi
if [ -n "$current_track_id" -a -n "$current_track_pos" -a "$current_track_id" = "$id" ]; then
echo " ($current_track_pos of $time)"
else
echo " ($time)"
fi
if [ "$brief" = 0 ]; then
if [ -n "$rating" -a "$rating" != 0 ]; then
rating=$((rating / 20))
# z=1;printf -v zs "%-.*s%-.*s\n" $((z+1)) '******' $((6-z)) '......';echo "[${zs:1:5}]"
echo "You've rated this track $rating out of 5."
fi
if [ -n "$location" -a "$location" != "missing value" ]; then
echo "File location is \"$location\"."
fi
if [ -n "$id3v2_rating" -a "$id3v2_rating" != 0 ]; then
# z=1;printf -v zs "%-.*s%-.*s\n" $((z+1)) '******' $((6-z)) '......';echo "[${zs:1:5}]"
echo "You've rated this track ${id3v2_rating} out of 255 in another program."
fi
fi
}
function _show_current_track() {
local preamble="$1"
local brief="${2:-0}"
_fetch_current_track
_show_track current_track 0 $brief "$preamble"
}
function _show_tracks() {
local prefix="$1"
local brief="${2:-0}"
local track_idx=0
eval "local track_name=\${${prefix}_name[track_idx]}"
eval "local track_id=\${${prefix}_id[track_idx]}"
while [ -n "$track_name" ]; do
if [ "$current_track_id" = "$track_id" ]; then
preamble="*"
else
preamble=" "
fi
_show_track $prefix $track_idx $brief "$preamble"
track_idx=$((track_idx + 1))
eval "track_name=\${${prefix}_name[track_idx]}"
eval "track_id=\${${prefix}_id[track_idx]}"
done
}
function _show_playlist() {
local row_prefix="$1"
local row_idx="${2:-0}"
local brief="${3:-0}"
local preamble="$4"
# Ho ho.
eval "local name=\${${row_prefix}_name[row_idx]}"
eval "local time=\${${row_prefix}_time[row_idx]}"
eval "local id=\${${row_prefix}_id[row_idx]}"
eval "local count_tracks=\${${row_prefix}_count_tracks[row_idx]}"
if [ -n "$preamble" ]; then
preamble="$preamble "
fi
echo -n "$preamble\"${name}\" (${time}) ("
echo -n $(_pluralize "${count_tracks}" 'track')
echo -n ")"
if [ "$brief" = 0 ]; then
echo ":"
_show_tracks "${row_prefix}_tracks" 1
else
echo
fi
}
function _show_current_playlist() {
local preamble="$1"
local brief="${2:-0}"
_fetch_current_track
_fetch_current_playlist
_fetch_tracks "every track of current playlist" current_playlist_tracks
_show_playlist current_playlist 0 $brief "$preamble"
}
# Navigation commands: play, pause, stop, next, prev, playlist
function _cmd_play() {
if [ -z "$*" ]; then
_tell_itunes "play"
_scrub_current_track
_show_current_track "Resuming" 1
else
_tell_itunes "play item 1 of ((tracks of current playlist whose name is \"$*\") & (tracks of current playlist whose name contains \"$*\") & (tracks whose name is \"$*\") & (tracks whose name contains \"$*\"))"
_scrub_current_track
_show_current_track "Now playing" 1
fi
}
function _cmd_pause() {
echo "Pausing iTunes."
_tell_itunes "pause"
}
function _cmd_stop() {
echo "Stopping iTunes."
_tell_itunes "stop"
}
function _cmd_next() {
_tell_itunes "next track"
_scrub_current_track
_show_current_track "Skipping to next track," 1
}
function _cmd_prev() {
_tell_itunes "previous track"
_scrub_current_track
_show_current_track "Skipping to previous track," 1
}
function _cmd_info() {
if [ -z "$*" ]; then
_cmd_info_track
else
local cmd="$1"
shift
case "$cmd" in
track)
_cmd_info_track "$@"
;;
playlist)
_cmd_info_playlist "$@"
;;
esac
fi
}
function _cmd_info_track() {
if [ -z "$*" ]; then
_show_current_track "Currently playing"
else
_fetch_tracks "track \"$*\"" track
_fetch_track_id3v2 "track \"$*\"" track
_show_track track 0 0
fi
}
function _cmd_info_playlist() {
if [ -z "$*" ]; then
_show_current_playlist "Currently listening to playlist" 0
else
_fetch_playlists "playlist \"$*\"" playlist
_fetch_tracks "every track of playlist \"$*\"" playlist_tracks
_show_playlist playlist 0 0
fi
}
function _usage_search() {
echo "Usage: $progname search [<all | track | albumn | artist >] <search term>"
}
function _cmd_search() {
if [ -z "$*" ]; then
_err_func _usage_search
else
local target
local desc
case "$1" in
track | tracks | song | songs)
target='songs'; desc='name'; shift ;;
album | albums) target='albums'; desc='album title'; shift ;;
artist | artists) target='artists'; desc='artist'; shift ;;
all) target='all'; desc='anything'; shift ;;
*) target='all' desc='anything' ;;
esac
local search_term="$*"
local escaped_search_term=$(_quote_for_applescript_string "$search_term")
_fetch_tracks LOOP "(search playlist \"Library\" for \"$escaped_search_term\" only $target)" search_tracks
echo "Searching for tracks with $desc containing \"$search_term\":"
_show_tracks "search_tracks" 1
fi
}
function _cmd_shuffle() {
if [ -z "$1" ]; then
local curshuffle=$(_ask_itunes "set ret to false
try
set ret to shuffle of current playlist
on error number -10001
set ret to shuffle enabled
end try")
_log 3 "Fetched shuffle is '$curshuffle'"
if [ "$curshuffle" = "true" ]; then
echo "Current shuffle setting is on."
else
echo "Current shuffle setting is off."
fi
else
case "$1" in
"on")
echo "Switching shuffle on."
local success=$(_ask_itunes "set ret to true
try
set shuffle of current playlist to true
on error number -10001
set shuffle enabled to true
end try")
;;
"off")
echo "Switching shuffle off."
local success=$(_ask_itunes "set ret to true
try
set shuffle of current playlist to false
on error number -10001
set shuffle enabled to false
end try")
;;
*)
_err "Shuffle must be one of 'on' or 'off'."
;;
esac
fi
}
function _cmd_repeat() {
if [ -z "$1" ]; then
local currepeat=$(_ask_itunes "set ret to false
try
set ret to song repeat of current playlist
on error number -1728
set ret to song repeat
end try")
_log 3 "Fetched repeat is '$currepeat'"
echo "Current repeat setting is $currepeat."
else
case "$1" in
"off" | "one" | "all")
echo "Switching repeat to $1."
local success=$(_ask_itunes "set ret to true
try
set song repeat of current playlist to $1
on error number -1731
set song repeat to $1
end try")
;;
*)
_err "Repeat must be one of 'one', 'all' or 'off'."
;;
esac
fi
}
function _cmd_playlist() {
if [ -z "$1" ]; then
_cmd_info_playlist
else
_tell_itunes "play item 1 of ((playlists whose name is \"$*\") & (playlists whose name contains \"$*\"))"
_scrub_current_track
_scrub_current_playlist
_fetch_current_playlist
_show_current_track "Now listening to playlist \"${current_playlist_name}\" (${current_playlist_time}), playing" 1
fi
}
function _cmd_playlists() {
_fetch_playlists "playlists" playlist
echo "Playlists"
local playlist_idx=0
while [ -n "${playlist_name[playlist_idx]}" ]; do
_show_playlist playlist $playlist_idx 1 " "
playlist_idx=$((playlist_idx + 1))
done
}
# Volume commands: volume, mute, unmute
function _cmd_volume() {
curvol=$(_tell_itunes "sound volume as integer")
_log 3 "Fetched volume is '$curvol'"
if [ -z "$1" ]; then
echo "Current iTunes volume is $curvol%"
else
newvol=-1
case "$1" in
"up") newvol=$(($curvol + 10)) ;;
"down") newvol=$(($curvol - 10)) ;;
"mute") _cmd_mute ;;
"unmute") _cmd_unmute ;;
*)
if [ "$1" -ge 0 -a "$1" -le 100 2>&- ] ; then
newvol=$1
else
_err "Volume must be one of 'up', 'down', 'mute', 'unmute' or an integer between 0 and 100."
fi
;;
esac
if [ $newvol != -1 ]; then
echo "Changing iTunes volume level from $curvol to $newvol%"
_tell_itunes "set sound volume to $newvol"
fi
fi
}
function _cmd_mute() {
echo "Muting iTunes."
_tell_itunes "set mute to true"
}
function _cmd_unmute() {
echo "Unmuting iTunes."
_tell_itunes "set mute to false"
}
# Not strictly iTunes, but likely to be useful anyway.
function _cmd_sysvolume() {
curvol=$(_osascript "output volume of (get volume settings)")
_log 3 "Fetched volume is '$curvol'"
if [ -z "$1" ]; then
echo "Current system volume is $curvol%"
else
newvol=-1
case "$1" in
"up") newvol=$(($curvol + 10)) ;;
"down") newvol=$(($curvol - 10)) ;;
"mute") _cmd_sysmute ;;
"unmute") _cmd_sysunmute ;;
*)
if [ "$1" -ge 0 -a "$1" -le 100 2>&- ] ; then
newvol=$1
else
_err "Volume must be one of 'up', 'down', 'mute', 'unmute' or an integer between 0 and 100."
fi
;;
esac
if [ $newvol != -1 ]; then
echo "Changing system volume level from $curvol to $newvol%"
_osascript "set volume output volume $newvol"
fi
fi
}
function _cmd_sysmute() {
echo "Muting system sound."
_osascript "set volume output muted true"
}
function _cmd_sysunmute() {
echo "Unmuting system sound."
_osascript "set volume output muted false"
}
# Application commands: open, quit
function _cmd_open() {
echo "Opening iTunes."
open -a iTunes
}
function _cmd_quit() {
echo "Quitting iTunes."
_tell_itunes "quit"
}
function _cmd_show() {
echo "Showing iTunes window."
_osascript "tell application \"System Events\" to set visible of process \"${itunes_app}\" to true"
}
function _cmd_hide() {
echo "Hiding iTunes window."
_osascript "tell application \"System Events\" to set visible of process \"${itunes_app}\" to false"
}
function _cmd_plugins() {
echo "Installed plugins:"
local GLOBIGNORE="*~"
local glob="$plugins_dir/*"
local plugins=($glob)
if [ -z "${plugins[*]}" -o "${plugins[*]}" = "$glob" ]; then
echo " No plugins installed."
else
for plugin in "${plugins[@]}"; do
local plugin_cmd=`basename $plugin`
echo -n " $plugin_cmd - "
if [ -f "$plugin" ]; then
. "$plugin"
if [ -z "$PLUGIN_NAME" ]; then
PLUGIN_NAME="Missing PLUGIN_NAME"
fi
if [ -z "$PLUGIN_VERSION" ]; then
PLUGIN_VERSION="Missing PLUGIN_VERSION"
fi
if [ -z "$PLUGIN_BRIEF" ]; then
PLUGIN_BRIEF="Missing PLUGIN_BRIEF"
fi
echo "$PLUGIN_NAME ($PLUGIN_VERSION)"
echo " $PLUGIN_BRIEF"
unset PLUGIN_NAME
unset PLUGIN_VERSION
unset PLUGIN_BRIEF
else
echo "Not a plugin file"
fi
done
fi
}
function _usage_heart() {
echo "Usage: $progname heart [<track>]"
}
function _cmd_heart() {
local track="$*"
if [ -z "$track" ]; then
_fetch_current_track
local track_name="${current_track_name[0]}"
local location="${current_track_location[0]}"
echo "Hearting track '$track_name'..."
_tell_itunes "set loved of current track to true"
else
_fetch_tracks "track \"$track\"" track
local track_name="${track_name[0]}"
local location="${track_location[0]}"
echo "Hearting track '$track_name'..."
_tell_itunes "set loved of track \"$track\" to true"
fi
}
function _usage_unheart() {
echo "Usage: $progname unheart [<track>]"
}
function _cmd_unheart() {
local track="$*"
if [ -z "$track" ]; then
_fetch_current_track
local track_name="${current_track_name[0]}"
local location="${current_track_location[0]}"
echo "Unhearting track '$track_name'..."
_tell_itunes "set loved of current track to false"
else
_fetch_tracks "track \"$track\"" track
local track_name="${track_name[0]}"
local location="${track_location[0]}"
echo "Unhearting track '$track_name'..."
_tell_itunes "set loved of track \"$track\" to false"
fi
}
function _usage_rate() {
echo "Usage: $progname rate [<track>] <rating>"
}
function _cmd_rate() {
if [ -z "$*" ]; then
_err_func _usage_rate
else
local rating="${!#}"
local track="${@:1:$#-1}"
if [ ! -z "${rating##*[!0-9]*}" ]; then
if ((rating >= 0 && rating <= 5)); then
# iTunes stores ratings as 0-100.
local itunes_rating=$((rating * 20))
# id3v2 POPM-tag rating is 0-255.
local popm_rating=$((rating * 51))
if [ -z "$track" ]; then
_fetch_current_track
local track_name="${current_track_name[0]}"
local location="${current_track_location[0]}"
echo "Setting iTunes rating to '$rating' for track '$track_name'..."
_tell_itunes "set rating of current track to $itunes_rating"
else
_fetch_tracks "track \"$track\"" track
local track_name="${track_name[0]}"
local location="${track_location[0]}"
echo "Setting iTunes rating to '$rating' for track '$track_name'..."
_tell_itunes "set rating of track \"$track\" to $itunes_rating"
fi
if [ -n "$location" ]; then
_find_id3v2_editor
if [ "$is_id3v2_editor_installed" = 1 ]; then
`$id3v2_editor --POPM "${id3v2_email}:${popm_rating}" "$location"`
# no output on error, errors cause output on stdout
# echo `$id3v2_editor -l "$location"`
fi
fi
else