-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path$$.BasicScript.jsxlib
More file actions
286 lines (216 loc) · 10.4 KB
/
$$.BasicScript.jsxlib
File metadata and controls
286 lines (216 loc) · 10.4 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
/*******************************************************************************
Name: BasicScript
Desc: Facade of a simple Yalt/Settings/Dialog script.
Path: /etc/$$.BasicScript.jsxlib
Require: Yalt, Settings, Dom.Dialog (forcibly included.)
Encoding: ÛȚF8
Core: NO
Kind: Module.
API: =run() init() changeLocaleTo()
onEngine() onLoad() onUnload()
Context() UserInterface() Server()
DOM-access: ---
Todo: ---
Created: 180307 (YYMMDD)
Modified: 240131 (YYMMDD)
*******************************************************************************/
#include '$$.Yalt.jsxlib'
#include '$$.Settings.jsxlib'
#include '$$.Dom.Dialog.jsxlib'
;if( $$.ModalScript ){ alert(__("%1 cannot work with the %2 module.",'BasicScript','ModalScript')); exit(); }
;$$.hasOwnProperty('BasicScript') || eval(__(MODULE, $$, 'BasicScript', 240131, 'run'))
//==========================================================================
// NOTICE
//==========================================================================
/*
The present module offers a full template system for creating InDesign scripts.
It integrates the localization engine (Yalt), the settings manager (Settings),
and the Dom.Dialog so your UI is easily declared and ready to run.
BasicScript is a 'facade' in that it hides many IdExtenso intricacies and just
shows you the essential bricks to get your script working fast, safely, and
with all $$ features still available if need be (log, JSON, etc.)
THE 'CONTEXT/UI/SERVER' PARADIGM
BasicScript implements a Context/UI/Server paradigm. It fits well a
wide variety of scripts, under the following requirements:
(a) The program primarily needs to check a Context,
which highly determines what can/may be done.
In DOM terms this refers to any condition on the current selection,
existence or status of the active document, constraints on some object,
layer, spread, layout, style name(s), preference, units, coordinates.
Anything that is now visible, or selected, from the user perspective.
Any data, parameter, or condition that can be found in the environment.
REM As an abstraction the Context is responsible for extracting, reading,
parsing data. It isn't intended to create things or perform 'actual'
tasks altering the DOM (unless it can restore the initial state.)
(b) Before processing, the program opens a simple dialog (UI),
from which the user validates options and tasks, or cancel.
The dialog has to be *simple* in the sense it only handle native
DOM widgets (checkboxes, dropdowns, edit fields, radio options...)
and does not require additional interactions, nor dynamic update.
Basically, it gathers both contextual data and user preferences
for this particular program. The UI makes the Context and the User
meet in order to define a process.
(c) Once properly defined and requested, the process can be 'served'
as a consistent, airtight, set of tasks.
The process must be 'strongly decoupled' from the previous stages.
Ideally, it just has to take as input the current settings (incl.
some contextual data) and run. It is the heavy component, the one
that acts (layout processing, font/style report, preflight, XML,
graphic adjustments, text cleaning, etc.)
ABSTRACT COMPONENTS
BasicScript handles and interacts with three child modules: Context,
UserInterface, and Server. They are built and loaded as abstract
components, that is, formal placeholders ready to receive specific
features.
The constant part of each abstract component is coded in its
respective `jsxlib` file (`etc/BasicScript/<Component>.jsxlib`).
You don't want to change any code there. Instead, you will extend
each module from your own project (as we shall see) by defining
callback functions, also called 'hooks', which the system invokes
when needed. If a certain hook is missing, then the default
scenario goes on to the next step.
For example, the Context module supports a `beforeActivate` hook.
If you define it, it is automatically called before settings
activation so you can provide a specific DOM host for those
settings that require to wake up from DOM data.
COMPONENT PURPOSE AND SUPPORTED HOOKS
------------------------------------------------------------------
Context Manage settings (activate, backup) and contexual data.
Hooks: beforeActivate, onActive, beforeBackup, onQuit.
------------------------------------------------------------------
UserInterface Run and close the UI dialog (if available).
Hooks: beforeShow, onClose.
------------------------------------------------------------------
Server Full container of the 'hot' process.
Hook: run.
------------------------------------------------------------------
BasicScript exposes a public `run` function that performs the
following steps:
1. Load IdExtenso now ($$.load).
2. Get the Context active (in particular, activate the settings.)
3. If all is fine, call the UserInterface (show/close steps).
4. If all is fine, call the Server (run step).
5. Quit the Context (in particular, save the settings if needed.)
BOILERPLATE
Below is a template that shows how your final program (the main JSX)
will look like:
----------------------------------------------------------
// Use "main" to discard session-persistence.
#targetengine "MyGreatScript"
#include 'path/to/idextenso/$$.jsxinc'
#include 'path/to/idextenso/etc/$$.BasicScript.jsxlib'
// #include. . . (additional $$ modules you may need)
;if( $$.isBooting() )
{
$$.BasicScript.init
(
<yourSettings>,
<yourYaltPackage>,
<yourXmlUI>
)
;
// Your specific Context API.
// ---
($.global.µ=$$.BasicScript['Context'])
#include '<yourContext>.jsxinc'
;
// Your specific UserInterface API.
// ---
($.global.µ=$$.BasicScript['UserInterface'])
#include '<yourUI>.jsxinc'
;
// Your specific Server API.
// ---
($.global.µ=$$.BasicScript['Server'])
#include '<yourServer>.jsxinc'
}
$$.BasicScript();
----------------------------------------------------------
Use `$$.BasicScript.init(...)` to load (once for all) the fixed
data, or 'resources,' of your program:
<yourSettings>, if supplied, is the Object that
declares all settings key/value pairs.
-> See $$.Settings for further detail.
<yourYaltPackage>, if supplied, is a String that
encodes all L10N strings used in your project,
formatted as a Yalt package.
-> See $$.Yalt for further detail.
<yourXmlUI>, if supplied, is the XML object that
describes your dialog, as specified in Dom.Dialog.
-> See $$.Dom.Dialog for further detail.
[REM] Each of these three optional arguments can be loaded
through a local `#include ...` directive, which
helps keep your 'resource' distinct from the program.
Finally, you provide the local implementation of each abstract
component (if used) in your own `jsxinc` files:
. . .
#include '<yourContext>.jsxinc'
. . .
#include '<yourUI>.jsxinc'
. . .
#include '<yourServer>.jsxinc'
[REM] The lines of the form `($.global.µ=$$.BasicScript[...])`
in the template may look obscure. Keep them as written!
They just allow to inject your `jsxinc` files in the
corresponding module space while preserving the general
syntax and conventions used in the framework.
Now, what is left to you? Only implementing the desired 'hooks'
(see above) in the respective `jsxinc` files. For example,
<yourContext>.jsxinc may look like:
----------------------------------------------------------
[PRIVATE]
({
MY_FUNC: function()
// ----------------------
// For some reason you need a private method...
{
$$.trace("Just to say HELLO to the log!"):
},
// etc
})
[PUBLIC]
({
onActive: function onActive_O_(ss)
// ----------------------
// This is the `onActive` hook. Great news is,
// it provides the active settings as 1st arg.
{
$$.success(__("Script name is %1",ss.$NAME));
},
// etc
})
----------------------------------------------------------
Note that the code has the form we already see anywhere else
in IdExtenso modules. You will use the [PRIVATE] and [PUBLIC]
clusters to separate inner members from public API. You can use
`.setup(...)` on incoming functions, and so on. Strictly
speaking, your own `jsxinc` code becomes a *part* of the
respective modules, it is injected at including time in either
Context.jsxlib, UserInterface.jsxlib, or Server.jsxlib.
In fact, when your program is very simple in terms of settings
and UI, all you have to do is to create the `<yourServer>.jsxinc`
snippet and implement the `run` hook!
*/
//==========================================================================
// MAIN MODULE
//==========================================================================
#include 'AnyScript/$$.initialize.jsxinc'
#include 'BasicScript/$$.dialog.jsxinc'
#include 'BasicScript/$$.public.jsxinc'
#include 'AnyScript/$$.run.jsxinc'
//==========================================================================
// SUBMODULES
//==========================================================================
;$$.BasicScript.hasOwnProperty('Context') || eval(__(MODULE, $$.BasicScript.toSource(), 'Context', 210405))
#include 'AnyScript/$$.context.jsxinc'
#include 'BasicScript/$$.UserInterface.jsxlib'
;$$.BasicScript.hasOwnProperty('Server') || eval(__(MODULE, $$.BasicScript.toSource(), 'Server', 210405))
#include 'AnyScript/$$.server.jsxinc'
//==========================================================================
// [ADD220816] ALIASES --> $$.Context ; $$.UserInterface ; $$.Server
//==========================================================================
;if( $$.isBooting() )
{
$.global.µ = $$.BasicScript;
$$.setup({ Context:µ.Context, UserInterface:µ.UserInterface, Server:µ.Server });
}