-
Notifications
You must be signed in to change notification settings - Fork 0
/
mscore-to-eps.sh
executable file
·215 lines (181 loc) · 5.18 KB
/
mscore-to-eps.sh
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
#! /bin/sh
INTER_FORMAT=pdf
#EPS_TOOL=inkscape
EPS_TOOL=pdftops
# MIT License
#
# Copyright (c) 2016 Josef Friedrich <josef@friedrich.rocks>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FIRST_RELEASE=2017-08-13
VERSION=1.0
PROJECT_PAGES="https://github.com/Josef-Friedrich/mscore-to-eps.sh"
SHORT_DESCRIPTION='Convert MuseScore files (*.mscz, *.mscx) into the EPS file format.'
USAGE="Usage: mscore-to-eps.sh [-hnsv] [<path>]
$SHORT_DESCRIPTION
Convert MuseScore files to eps using 'pdfcrop' and 'pdftops' or
'Inkscape'. If <path> is omitted, all MuseScore files in the
current working directory are converted. <path> can be either a
directory or a MuseScore file.
DEPENDENCIES
'pdfcrop' (included in TeXlive) and 'pdftops' (Poppler tools) or
'Inkscape'
OPTIONS
-h, --help
Show this help message.
-n, --no-clean
Do not remove / clean intermediate *.$INTER_FORMAT files
-s, --short-description
Show a short description / summary.
-v, --version
Show the version number of this script.
"
# Exit codes
# Invalid option: 2
# Missing argument: 3
# No argument allowed: 4
_getopts() {
while getopts ':hnsv-:' OPT ; do
case $OPT in
h) echo "$USAGE" ; exit 0 ;;
n) OPT_NO_CLEAN=1 ;;
s) echo "$SHORT_DESCRIPTION" ; exit 0 ;;
v) echo "$VERSION" ; exit 0 ;;
\?) echo "Invalid option “-$OPTARG”!" >&2 ; exit 2 ;;
:) echo "Option “-$OPTARG” requires an argument!" >&2 ; exit 3 ;;
-)
LONG_OPTARG="${OPTARG#*=}"
case $OPTARG in
help) echo "$USAGE" ; exit 0 ;;
no-clean) OPT_NO_CLEAN=1 ;;
short-description) echo "$SHORT_DESCRIPTION" ; exit 0 ;;
version) echo "$VERSION" ; exit 0 ;;
help*|no-clean*|short-description*|version*)
echo "No argument allowed for the option “--$OPTARG”!" >&2
exit 4
;;
'') break ;; # "--" terminates argument processing
*) echo "Invalid option “--$OPTARG”!" >&2 ; exit 2 ;;
esac
;;
esac
done
GETOPTS_SHIFT=$((OPTIND - 1))
}
_mscore() {
if [ "$(uname)" = "Darwin" ]; then
/Applications/MuseScore\ 2.app/Contents/MacOS/mscore \
--export-to "$1".$INTER_FORMAT "$2"
else
echo "Export to $1"
mscore --export-to "$1".$INTER_FORMAT "$2"
fi
}
_inkscape() {
if [ "$(uname)" = "Darwin" ]; then
INKSCAPE=/Applications/Inkscape.app/Contents/Resources/bin/inkscape
else
INKSCAPE=inkscape
fi
$INKSCAPE \
--export-area-drawing \
--without-gui \
--export-eps="$1".eps "$1".$INTER_FORMAT
}
_pdf_pages() {
pdfinfo "$1" | grep 'Pages:' | awk '{print $2}'
}
_pdftops() {
pdfcrop "$1" "$1"
if [ "$2" -gt 0 ]; then
pdftops -eps -f "$2" -l "$2" "$1" "$(echo "$1" | sed "s/\.pdf/_$2\.eps/g")"
else
pdftops -eps "$1"
fi
}
_to_eps() {
if [ "$EPS_TOOL" = 'inkscape' ]; then
_inkscape "$1" "$2" > /dev/null 2>&1
else
_pdftops "$1".pdf "$2" > /dev/null 2>&1
fi
}
_clean() {
if [ ! "$OPT_NO_CLEAN" = "1" ]; then
rm -f "$1".$INTER_FORMAT
fi
}
_do_file() {
SCORE="$(readlink -f "$1")"
BASENAME=$(echo "$FILE" | sed 's/\.mscx//g' | sed 's/\.mscy//g')
_mscore "$BASENAME" "$SCORE" > /dev/null 2>&1
PAGES=$(_pdf_pages "$BASENAME.$INTER_FORMAT")
if [ "$PAGES" -gt 1 ]; then
I=1
while [ "$I" -le "$PAGES" ]; do
_to_eps "$BASENAME" "$I"
I=$((I + 1))
done
else
_to_eps "$BASENAME"
fi
_clean "$BASENAME"
}
_check_for_executable() {
if ! command -v "$1" > /dev/null 2>&1 ; then
echo "Missing binary “$1”!" >&2
exit 2
fi
}
## This SEPARATOR is required for test purposes. Please don’t remove! ##
if [ $(uname) = 'Darwin' ]; then
if command -v greadlink > /dev/null ; then
unalias readlink > /dev/null 2>&1
alias readlink=greadlink
else
echo "ERROR: GNU utils required for Mac. You may use
homebrew to install them: brew install coreutils gnu-sed"
exit 1
fi
fi
_getopts $@
shift $GETOPTS_SHIFT
_check_for_executable mscore
_check_for_executable pdfcrop
_check_for_executable pdfinfo
_check_for_executable pdftops
FILE="$1"
if [ -f "$FILE" ]; then
_do_file "$FILE"
exit 0
fi
if [ -d "$FILE" ]; then
FILES=$(find "$FILE" -iname '*.mscz' -or -iname '*.mscx')
elif [ -z "$FILE" ]; then
FILES=$(find . -iname '*.mscz' -or -iname '*.mscx')
fi
if [ "$FILES" = '' ]; then
echo 'No files to convert found!'
_usage
exit 1
fi
for FILE in $FILES; do
_do_file "$FILE"
done