forked from ericmandel/js9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs9
executable file
·314 lines (297 loc) · 11.5 KB
/
js9
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
#!/bin/bash
#set -x
# values will be filled in by mkjs9 during the build
if [ x${JS9_SRCDIR} = x ]; then
JS9_SRCDIR=""
fi
if [ x${JS9_INSTALLDIR} = x ]; then
JS9_INSTALLDIR=""
fi
# make sure js9Msg sees these, if possible
if [ x${JS9_SRCDIR} != x ]; then
export JS9_SRCDIR
fi
if [ x${JS9_INSTALLDIR} != x ]; then
export JS9_INSTALLDIR
fi
# look for js9 script in directory running this script
# this is for desktop js9 used without building anything
#
# in case the user has changed the js9 script, try to figure out the right one
# is this file a link?
BFILE="${BASH_SOURCE[0]}"
LFILE="$( readlink $BFILE )"
if [ x"$LFILE" != x ]; then
XFILE="$LFILE"
else
XFILE="$BFILE"
fi
# is the containing directory a link?
BDIR="$( dirname "${XFILE}" )"
LDIR="$( readlink "$BDIR" )"
if [ x"$LDIR" != x ]; then
XDIR="$LDIR"
else
XDIR="$BDIR"
fi
# any more links we need to know about?
DIR="$( cd "${XDIR}" >/dev/null 2>&1 && pwd )"
# look for the script and set the run directory
if [ -r ${DIR}/js9 ]; then
export JS9_RUNDIR=${DIR}
fi
# get OS
OS=`uname -s`
# error routine
error () {
echo "ERROR: $@" >&2
exit 1
}
usage(){
XEQ=`basename $0`
printf "$XEQ: communication with the JS9 Desktop app and browser-based web pages\n\n"
printf " usage:\n"
printf " # no args: start JS9 app or JS9 web page in a browser\n"
printf " %s\n" $XEQ
printf " # send a command to an existing JS9 app or web page\n"
printf " %s [switches] [cmd] [args]\n" $XEQ
printf " # start the JS9 Desktop app and load images\n"
printf " %s -a [switches] [image1 image2 ...]\n" $XEQ
printf " # start the JS9 Desktop app and load images with json opts\n"
printf " %s -a [switches] [image1 opts1 image2 ...]\n" $XEQ
printf " # start the JS9 Desktop app and load images with switch opts\n"
printf " %s -a [switches] [image1 --opt1 val1 --opt2 val2 image2 ...]\n" $XEQ
printf " # or without -a if no switches specified:\n"
printf " %s [image1, image2, ...]\n" $XEQ
printf " # start a JS9 web page in a browser and load images\n"
printf " %s -b [switches] [image1, image2, ...]\n" $XEQ
printf " # convert JS9 region file to DS9 region file\n"
printf " %s -r js9RegionFile\n" $XEQ
printf " \n"
printf " general switches:\n"
printf " --help # print this message\n"
printf " \n"
printf " app (-a) specific switches:\n"
printf " -a # start JS9 app (requires Electron.js)\n"
printf " --cmds [js9 commands] # JS9 commands to execute after loading images\n"
printf " --cmdfile [file] # file of commands to execute after loading images\n"
printf " --deb [true|false] # bring up Chrome debugger\n"
printf " --eval [true|false] # enable eval (def: false, not recommended)\n"
printf " --height [h] # height of app\n"
printf " --helper [true|false] # whether to start up helper app (def: true)\n"
printf " --hostfs [true|false] # enable access to host file system (def: false)\n"
printf " --merge [dir/[webpage]] # merge tools from dir, opt: display web page \n"
printf " --renameid [oid1:nid1,..] # rename ids of the JS9 display\n"
printf " --savedir [path] # dir for saved files (skip save dialog box)\n"
printf " --title [id] # rename the id of the JS9 display\n"
printf " -w|--webpage [url] # url or full path of web page to open\n"
printf " --width [w] # width of app\n"
printf " \n"
printf " browser (-b) specific switches:\n"
printf " -b|--browser # start in browser\n"
printf " -w|--webpage [url] # url or full path of web page to open\n"
printf " \n"
printf " command and browser (-b) switches:\n"
printf " -d|--debug # output debugging info\n"
printf " -h|--host|--helper [host] # helper host (def: localhost)\n"
printf " -i|--id [id] # client JS9 id (def: JS9)\n"
printf " -m|--multi # send to multiple clients\n"
printf " -|-p|--pipe # read argument list from stdin\n"
printf " --pageid [id] # unique page id from server\n"
printf " -t|--timeout # timeout for browser startup\n"
printf " -v|--verify # output verification info\n"
printf " \n"
printf " send commands to an existing JS9 app or web page:\n"
printf " %s help # list available commands\n" $XEQ
printf " %s cmap heat # change colormap to 'heat'\n" $XEQ
printf " %s regions circle # create a circle region\n" $XEQ
printf " %s --id myJS9 regions # get regions for this instance\n" $XEQ
printf " %s -s # read commands from stdin\n" $XEQ
printf " \n"
printf " start app or browser, and load an image:\n"
printf " # start the js9 app and load foo.fits\n"
printf " # (requires Electron.js app to be installed)\n"
printf " %s -a foo.fits\n" $XEQ
printf " # start the js9 app, load foo.fits, with switch opts\n"
printf " %s -a foo.fits --colormap heat --scale log\n" $XEQ
printf " # start the js9 app, load foo.fits, with json opt\n"
printf " %s -a foo.fits '{\"colormap\":\"heat\", \"scale\":\"log\"}'\n" $XEQ
printf " # start the js9 app, load foo.fits, execute the js9 commands\n"
printf " # contained in the foo.js script file\n"
printf " %s -a --cmdfile foo.js foo.fits\n" $XEQ
printf " # start the js9 app and load foo.fits into myjs9.html page\n"
printf " %s -a --webpage ~/myjs9.html foo.fits\n" $XEQ
printf " # start the js9 app, enable node for direct access to file system\n"
printf " %s -a --node true foo.fits\n" $XEQ
printf " # start browser and load foo.fits into the myjs9.html page\n"
printf " %s -b -w ~/myjs9.html foo.fits\n" $XEQ
printf " # display js9.html web page (or \$JS9_WEBPAGE) in \$JS9_BROWSER\n"
printf " %s -b foo.fits\n" $XEQ
printf " # ... then carry on as usual\n"
printf " %s cmap heat\n" $XEQ
printf " \n"
printf " environment variables:\n"
printf " JS9_SRCDIR # where to find files, if you don't build/install\n"
printf " JS9_BROWSER # browser to use with -b (chrome|firefox|safari)\n"
printf " JS9_WEBPAGE # url or path of web page to use (app or browser)\n"
printf " JS9_TO_JS9MSG # exec faster js9msg if only sending msgs\n"
printf " \n"
printf " notes:\n"
printf " Make sure the relative paths to the js9 files in your web page are correct!"
printf "\n"
printf "\n"
printf " --browser uses \$JS9_BROWSER, or Safari on Mac, else Chrome\n"
if [ x"$OS" = xDarwin ]; then
printf "\n When using Desktop JS9 with Electron.app, you can codesign it to avoid"
printf "\n repeated requests by macOS to allow incoming connections:\n"
printf "\n sudo codesign --force --deep --sign - /pathto/Electron.app/Contents/MacOS/Electron\n\n"
fi
}
# quick check for --help
if [ $# = 1 -a "$1" = "--help" ]; then
usage
exit 0
fi
# js9msg (exported by desktop app): no args => display help and exit
# (since we can't really start up the app)
if [ $# = 0 -a x`basename $XFILE` != "xjs9" ]; then
usage
exit 0
fi
# argument preprocessing
if [ $# -ge 0 ]; then
for arg in "$@"; do
case $arg in
-a) DOAPP=true; JS9_TO_JS9MSG=false; ALLFILES=false;;
-b) DOBROWSER=true; JS9_TO_JS9MSG=false; ALLFILES=false;;
-|-p|--pipe) DOPIPE=true; JS9_TO_JS9MSG=false; ALLFILES=false;;
-r) DOREG=true; JS9_TO_JS9MSG=false; ALLFILES=false;;
-av|-va) DOAPP=true;DOVERIFY=true; JS9_TO_JS9MSG=false; ALLFILES=false;;
-h|--host|--helper) DOHOST=true; JS9_TO_JS9MSG=false; ALLFILES=false;;
-s) set --;;
-v|--verify) DOVERIFY=true;;
*) f=`echo $arg | sed 's/\[.*\]//'`
if [[ ! "$f" =~ ^https?:// && ! -r "$f" ]]; then
ALLFILES=false
fi;;
esac
done
fi
# if all args are files, its the app
if [ x$ALLFILES != xfalse ]; then
DOAPP=true
JS9_TO_JS9MSG=false
fi
# see if we can use js9msg (wget or curl), which is faster
if [ x"$JS9_TO_JS9MSG" != xfalse ]; then
command -v js9msg 1>/dev/null 2>&1
if [ $? = 0 ]; then
command -v wget curl 1>/dev/null 2>&1
if [ $? = 0 ]; then
if [ x$DOVERIFY = xtrue ]; then
echo "exec'ing: js9msg $@"
fi
exec js9msg "$@"
fi
fi
fi
# Node or Electron will be used ...
# look for Node.js program (for messaging, but not for app)
command -v node 1>/dev/null 2>&1
if [ $? = 0 ]; then
NODE_EXE="node"
fi
# look for Electron program (for messaging or for app)
case $OS in
Linux) TRY="electron"
command -v "$TRY" 1>/dev/null 2>&1
if [ $? = 0 ]; then
# https://github.com/electron/electron/issues/17972
# (but see end of discussion, its unclear whether this is needed)
ELECTRON_EXE="$TRY --no-sandbox"
fi
;;
Darwin) TRIES="${JS9_ELECTRON_APP} ${HOME}/Applications/Electron.app/Contents/MacOS/Electron /Applications/Electron.app/Contents/MacOS/Electron /usr/local/lib/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron"
for TRY in $TRIES; do
command -v "$TRY" 1>/dev/null 2>&1
if [ $? = 0 ]; then
ELECTRON_EXE="$TRY"
break;
fi
done
;;
esac
# use Electron or use Node, depending on the mode we are in
if [ x$DOAPP = xtrue ]; then
if [ x"$ELECTRON_EXE" != x ]; then
JS9_XEQ="$ELECTRON_EXE"
else
error "desktop js9 requires Electron.js (https://www.electronjs.org)"
fi
MSGSCRIPT="js9Electron.js"
else
if [ x"$JS9_XEQ" = x ]; then
if [ x"$NODE_EXE" != x ]; then
JS9_XEQ="$NODE_EXE"
elif [ x"$ELECTRON_EXE" != x ]; then
JS9_XEQ="$ELECTRON_EXE"
else
error "js9 scripting requires Node.js (https://nodejs.org) or Electron.js (https://www.electronjs.org)"
fi
fi
if [ x$DOREG = xtrue ]; then
MSGSCRIPT="js9Regions.js"
else
MSGSCRIPT="js9Msg.js"
fi
fi
# make sure we can find the JS9 messaging script
if [ -r "./${MSGSCRIPT}" ]; then
JS9_MSGSCRIPT="./${MSGSCRIPT}"
elif [ -r "${JS9_RUNDIR}/${MSGSCRIPT}" ]; then
JS9_MSGSCRIPT="${JS9_RUNDIR}/${MSGSCRIPT}"
elif [ -r "${JS9_INSTALLDIR}/${MSGSCRIPT}" ]; then
JS9_MSGSCRIPT="${JS9_INSTALLDIR}/${MSGSCRIPT}"
elif [ -r "${JS9_SRCDIR}/${MSGSCRIPT}" ]; then
JS9_MSGSCRIPT="${JS9_SRCDIR}/${MSGSCRIPT}"
else
error "ERROR: can't find ${MSGSCRIPT} (install JS9, or set \$JS9_SRCDIR )"
fi
export JS9_MSGSCRIPT
# make sure we have a browser defined
if [ x$JS9_BROWSER = x ]; then
case $OS in
# see known issues
# Darwin) JS9_BROWSER="safari" ;;
Darwin) JS9_BROWSER="chrome" ;;
*) JS9_BROWSER="chrome" ;;
esac
fi
export JS9_BROWSER
# run node or Electron with the msg script
if [ x$DOVERIFY = xtrue ]; then
echo "executing: $JS9_XEQ" "$JS9_MSGSCRIPT" $@
fi
# we need to quote the remaining arguments to deal with spaces in filenames
# https://superuser.com/questions/403263/how-to-pass-bash-script-arguments-to-a-subshell
declare -a XARGS
COUNT=$#
for ((INDEX=0; INDEX<COUNT; ++INDEX))
do
XARG="$(printf "%q" "$1")"
XARGS[INDEX]="$(printf "%q" "$XARG")"
shift
done
# eval the command so that strings are handled properly
if [ x"$DOAPP" = xtrue ]; then
if [ x$DOVERIFY = xtrue ]; then
# verify means we want all output from electron
eval "$JS9_XEQ" "$JS9_MSGSCRIPT" $ARGS ${XARGS[*]}
else
# else trash the output (too many electron messages)
eval "$JS9_XEQ" "$JS9_MSGSCRIPT" $ARGS ${XARGS[*]} >/dev/null 2>&1
fi
else
# if not using electron, we want to see all output
eval "$JS9_XEQ" "$JS9_MSGSCRIPT" $ARGS ${XARGS[*]}
fi