-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleTeX4ht.applescript
executable file
·399 lines (369 loc) · 17.9 KB
/
SimpleTeX4ht.applescript
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
-- SimpleTeX4ht.applescript
-- SimpleTeX4ht
(* © Copyright 2004-2010 Yves GESNEL.
This file is part of SimpleTeX4ht.
SimpleTeX4ht is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
SimpleTeX4ht is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SimpleTeX4ht. If not, see <http://www.gnu.org/licenses/>. *)
(* ==== Properties ==== *)
global macFile
property progressWindow : null
(* ==== Event Handlers ==== *)
-- Preferences : attempt to make new default entries
on will finish launching theObject
try
tell user defaults
make new default entry at end of default entries with properties {name:"options", contents:{false, false}}
make new default entry at end of default entries with properties {name:"HTMLOptions", content:{false, false}}
make new default entry at end of default entries with properties {name:"otherModesOptions", content:{1}}
make new default entry at end of default entries with properties {name:"expertOptions", contents:{"", "", "", ""}}
make new default entry at end of default entries with properties {name:"tabName", contents:{"HTML"}}
end tell
end try
read_Preferences()
end will finish launching
on launched theObject
end launched
-- progress indicator is not visible
on opened theObject
end opened
-- Drag and drop
on awake from nib theObject
tell theObject to register drag types {"file names"}
end awake from nib
on drop theObject drag info dragInfo
set dropped to false
if "file names" is in types of pasteboard of dragInfo then
set preferred type of pasteboard of dragInfo to "file names"
set theFiles to contents of pasteboard of dragInfo
set theFile to (item 1 in theFiles)
set the_File to theFile as POSIX file
set FileKind to kind of (info for the_File)
set forbidden to {"Dossier", "Folder", "Carpeta", "Ordner", "Application", "Programm", "aplicación", "Volume", "Volumen"}
if FileKind is not in forbidden then
set macFile to (POSIX file theFile) as Unicode text
tell theObject to set content to return & " " & theFile as string
set dropped to true
end if
end if
return dropped
end drop
on conclude drop theObject drag info dragInfo
end conclude drop
-- Convert button’s action
on clicked theObject
set selectFile to (localized string "select" from table "Localized")
set selectFile to getPlainText(selectFile)
try
tell user defaults
set theSwitches to contents of default entry "options"
end tell
set tex4htFiles to item 1 of theSwitches
set openFile to item 2 of theSwitches
end try
if name of theObject is "Convert" then
if (content of text field "path" of window "Main") as string is " …" then
set macFile to choose file with prompt selectFile without invisibles
set theFile to POSIX path of (macFile as string)
set content of text field "path" of window "Main" to return & " " & theFile
end if
beginProcess()
if presenceTeX4ht() is false then
stopProcess()
set textError to (localized string "error" from table "Localized")
set textError to getPlainText(textError)
display dialog textError buttons {"OK"} default button 1 with icon 0
else
tell application "Finder"
set macDir to container of item macFile
set real_macName to name of item macFile
end tell
set unixDir to quoted form of the POSIX path of (macDir as string)
set theList to cleantext(real_macName)
set cleaned to item 1 of theList
set macName to item 2 of theList
if cleaned then
set macName to copyAndRename(macFile, macName, macDir)
end if
set shortName to removeExtension(macName)
set extension to "html" as string
if theObject is button "Convert" of tab view item "Expert" of tab view "tab" of window "Main" then
set option1 to contents of text field "option1" of tab view item "Expert" of tab view "tab" of window "Main"
set option2 to contents of text field "option2" of tab view item "Expert" of tab view "tab" of window "Main"
set option3 to contents of text field "option3" of tab view item "Expert" of tab view "tab" of window "Main"
set option4 to contents of text field "option4" of tab view item "Expert" of tab view "tab" of window "Main"
set option to "\"" & option1 & "\"" & " " & "\"" & option2 & "\"" & " " & "\"" & option3 & "\"" & " " & "\"" & option4 & "\""
else if theObject is button "Convert" of tab view item "HTML" of tab view "tab" of window "Main" then
if (state of button "multipages" of tab view item "HTML" of tab view "tab" of window "Main") is 1 and (state of button "XHTML" of tab view item "HTML" of tab view "tab" of window "Main") is 0 then
set option to "\"html,2,sections+\" \"\" \"\" \"-interaction=batchmode\""
else if (state of button "multipages" of tab view item "HTML" of tab view "tab" of window "Main") is 0 and (state of button "XHTML" of tab view item "HTML" of tab view "tab" of window "Main") is 0 then
set option to "\"\" \"\" \"\" \"-interaction=batchmode\""
else if (state of button "multipages" of tab view item "HTML" of tab view "tab" of window "Main") is 1 and (state of button "XHTML" of tab view item "HTML" of tab view "tab" of window "Main") is 1 then
set option to "\"xhtml,2,sections+\" \"\" \"\" \"-interaction=batchmode\""
else if (state of button "multipages" of tab view item "HTML" of tab view "tab" of window "Main") is 0 and (state of button "XHTML" of tab view item "HTML" of tab view "tab" of window "Main") is 1 then
set option to "\"xhtml\" \"\" \"\" \"-interaction=batchmode\""
end if
else if theObject is button "Convert" of tab view item "otherModes" of tab view "tab" of window "Main" then
if (current row of matrix "Matrix" of tab view item "otherModes" of tab view "tab" of window "Main") is 1 then
set option to "\"xhtml,ooffice\" \"ooffice/! -cmozhtf\" \"-coo\" \"-interaction=batchmode\""
set extension to "odt" as string
else if (current row of matrix "Matrix" of tab view item "otherModes" of tab view "tab" of window "Main") is 2 then
set option to "\"xhtml,mozilla\" \" -cmozhtf\" \"-cvalidate\" \"-interaction=batchmode\""
set extension to "xml" as string
else if (current row of matrix "Matrix" of tab view item "otherModes" of tab view "tab" of window "Main") is 3 then
set option to "\"xhtml,docbook\" \" -cunihtf\" \"\" \"-interaction=batchmode\""
set extension to "xml" as string
else if (current row of matrix "Matrix" of tab view item "otherModes" of tab view "tab" of window "Main") is 4 then
set option to "\"xhtml,docbook-mml\" \" -cunihtf\" \"\" \"-interaction=batchmode\""
set extension to "xml" as string
else if (current row of matrix "Matrix" of tab view item "otherModes" of tab view "tab" of window "Main") is 5 then
set option to "\"xhtml,tei\" \" -cunihtf\" \"\" \"-interaction=batchmode\""
set extension to "xml" as string
else if (current row of matrix "Matrix" of tab view item "otherModes" of tab view "tab" of window "Main") is 6 then
set option to "\"xhtml,tei-mml\" \" -cunihtf\" \"\" \"-interaction=batchmode\""
set extension to "xml" as string
end if
end if
htlatex(option, macName, unixDir, tex4htFiles, openFile, shortName, extension, cleaned)
if cleaned then
try
do shell script "cd " & unixDir & ";rm " & macName & " ;"
end try
end if
endProcess()
end if
end if
end clicked
-- Should quit after last window closed
on should quit after last window closed theObject
return true
end should quit after last window closed
on will quit theObject
save_Preferences()
end will quit
(* ==== Handlers ==== *)
on read_Preferences()
-- Read the expert options from the "SimpleTeX4ht.plist" in the ~/Library/Preferences directory and then sets those values in the UI elements.
try
set theHTMLOptions to contents of default entry "HTMLOptions" of user defaults
set theotherModesOptions to contents of default entry "otherModesOptions" of user defaults
set theExpertOptions to contents of default entry "expertOptions" of user defaults
set theTabName to contents of default entry "tabName" of user defaults as string
tell window "Main"
-- HTML Tab UI
set state of button "multipages" of tab view item "HTML" of tab view "tab" to item 1 of theHTMLOptions
set state of button "XHTML" of tab view item "HTML" of tab view "tab" to item 2 of theHTMLOptions
-- other Modes Tab UI
set current row of matrix "Matrix" of tab view item "otherModes" of tab view "tab" to item 1 of theotherModesOptions
-- Expert tab UI
set contents of text field "option1" of tab view item "Expert" of tab view "tab" to item 1 of theExpertOptions
set contents of text field "option2" of tab view item "Expert" of tab view "tab" to item 2 of theExpertOptions
set contents of text field "option3" of tab view item "Expert" of tab view "tab" to item 3 of theExpertOptions
set contents of text field "option4" of tab view item "Expert" of tab view "tab" to item 4 of theExpertOptions
end tell
-- Tab UI
tell tab view "tab" of window "Main"
set the current tab view item to tab view item theTabName
end tell
end try
end read_Preferences
on save_Preferences()
-- Get the contents of the UI elements
tell window "Main"
set theHTMLOptions to {state of button "multipages" of tab view item "HTML" of tab view "tab", state of button "XHTML" of tab view item "HTML" of tab view "tab"}
set theotherModesOptions to {current row of matrix "Matrix" of tab view item "otherModes" of tab view "tab"}
set theExpertOptions to {contents of text field "option1" of tab view item "Expert" of tab view "tab", contents of text field "option2" of tab view item "Expert" of tab view "tab", contents of text field "option3" of tab view item "Expert" of tab view "tab", contents of text field "option4" of tab view item "Expert" of tab view "tab"}
end tell
tell tab view "tab" of window "Main"
set the theTabName to the name of the current tab view item
end tell
-- Write out the preferences
--try
tell user defaults
set contents of default entry "HTMLOptions" to theHTMLOptions
set contents of default entry "otherModesOptions" to theotherModesOptions
set contents of default entry "expertOptions" to theExpertOptions
set contents of default entry "tabName" to theTabName
end tell
--end try
call method "synchronize" of object user defaults
end save_Preferences
-- Show progress indicator
on beginProcess()
set visible of window "Main" to false
-- Only load the progress nib once
if progressWindow is equal to null then
load nib "Progress"
show window "Progress"
set progressWindow to window "Progress"
else
show window "Progress"
end if
set visible of progress indicator "progress" of window "Progress" to true
repeat with i from 0 to 16
set content of progress indicator "progress" of window "Progress" to i
end repeat
end beginProcess
-- htlatex command
on htlatex(option, macName, unixDir, tex4htFiles, openFile, shortName, extension, cleaned)
try
set tempFolder to POSIX path of (path to temporary items)
do shell script "export PATH=$PATH:/usr/texbin:/usr/local/bin:/opt/local/bin:/opt/local/sbin:/sw/bin:/usr/local/teTeX/bin/powerpc-apple-darwin-current; cd " & unixDir & "; htlatex " & macName & " " & option & ";grep 'No pages of output.' " & shortName & ".log > " & tempFolder & "st4hTemp2;echo 0 >> " & tempFolder & "st4hTemp2"
on error
stopProcess()
end try
set thePath to ((path to temporary items as Unicode text) & "st4hTemp2")
set test to read file thePath
do shell script "rm " & tempFolder & "st4hTemp2"
set test to (first character of test) as string
delay 1
if test is "N" then
stopProcess()
if cleaned then
try
do shell script "cd " & unixDir & ";rm " & macName & " ;"
end try
end if
set conversionError to (localized string "conversionError" from table "Localized")
set conversionError to getPlainText(conversionError)
display dialog conversionError buttons {"OK"} default button 1 with icon 0
else
try
if tex4htFiles is true and openFile is true then
do shell script "cd " & unixDir & ";rm " & shortName & "*.4*;rm " & shortName & ".aux;rm " & shortName & ".dvi;rm " & shortName & ".idv;rm " & shortName & ".lg;rm " & shortName & ".log;rm " & shortName & "*.tmp;rm " & shortName & ".xref; open " & shortName & "." & extension & ""
else if tex4htFiles is false and openFile is true then
do shell script "cd " & unixDir & ";open " & shortName & "." & extension & ""
else if tex4htFiles is true and openFile is false then
do shell script "cd " & unixDir & ";rm " & shortName & "*.4*;rm " & shortName & ".aux;rm " & shortName & ".dvi;rm " & shortName & ".idv;rm " & shortName & ".lg;rm " & shortName & ".log;rm " & shortName & "*.tmp;rm " & shortName & ".xref"
end if
end try
end if
end htlatex
-- getPlainText converts Unicode to plain text
on getPlainText(fromUnicodeString)
set styledText to fromUnicodeString as string
set styledRecord to styledText as record
return «class ktxt» of styledRecord
end getPlainText
-- Check for TeX4ht
on presenceTeX4ht()
set tempFolder to POSIX path of (path to temporary items)
do shell script "export PATH=$PATH:/usr/texbin:/usr/local/bin:/opt/local/bin:/opt/local/sbin:/sw/bin:/usr/local/teTeX/bin/powerpc-apple-darwin-current; cd " & tempFolder & "; which htlatex > st4hTemp1; echo 0 >> st4hTemp1" -- echo 0 avoid a blank temporary file
set thePath to ((path to temporary items as Unicode text) & "st4hTemp1")
set test to read file thePath
do shell script "rm " & tempFolder & "st4hTemp1"
set test to (first character of test) as string
repeat with i from 16 to 33
delay 0.1
set content of progress indicator "progress" of window "Progress" to i
end repeat
if test is not "/" then return false
if test is "/" then return true
end presenceTeX4ht
-- Remove file extension
on removeExtension(shortName)
set shortName to ¬
(the reverse of every character of shortName) as string
set x to the offset of "." in shortName
set shortName to (characters (x + 1) thru -1 of shortName) as string
set shortName to (the reverse of every character of shortName) as string
return shortName
end removeExtension
-- Clean special characters from a string like $:'/?;&"#%><(){}\~`^\|*
on cleantext(this_text)
set cleaned to false
set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
set the extra_chars to "-._+!@=[]"
set the a_chars to "àáâäã"
set the e_chars to "éèêë"
set the i_chars to "îïìí"
set the o_chars to "õôöóò"
set the u_chars to "ûüùú"
set the y_chars to "ÿý"
set the n_chars to "ñ"
set the c_chars to "ç"
set the acceptable_characters to the standard_characters & the extra_chars
set the cleaned_text to ""
repeat with this_char in this_text
if this_char is in the acceptable_characters then
set the cleaned_text to (the cleaned_text & this_char)
else
if this_char is in the a_chars then
set the cleaned_text to (the cleaned_text & "a")
else if this_char is in the e_chars then
set the cleaned_text to (the cleaned_text & "e")
else if this_char is in the i_chars then
set the cleaned_text to (the cleaned_text & "i")
else if this_char is in the o_chars then
set the cleaned_text to (the cleaned_text & "o")
else if this_char is in the u_chars then
set the cleaned_text to (the cleaned_text & "u")
else if this_char is in the y_chars then
set the cleaned_text to (the cleaned_text & "y")
else if this_char is in the n_chars then
set the cleaned_text to (the cleaned_text & "n")
else if this_char is in the c_chars then
set the cleaned_text to (the cleaned_text & "c")
else
set the cleaned_text to (the cleaned_text & "_") as string
end if
set cleaned to true
end if
end repeat
-- if the first character is "-" then change this value to"_"
if the first character of the cleaned_text is "-" then
set the cleaned_text to "_" & (get text 2 thru -1 of the cleaned_text)
set cleaned to true
end if
return {cleaned, the cleaned_text}
end cleantext
-- Copy and rename. Check if a filename already exists. A sequential index is added before the extension if necessary.
on copyAndRename(macFile, macName, macDir)
tell application "Finder"
set macFileCopy to duplicate macFile
set PathToCheck to ((macDir as string) & macName)
if item PathToCheck exists then
set sequential to 0
set default_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set root_name to the first text item of macName
set suffix_name to the last text item of macName
repeat
set the macName to the (the root_name & the sequential & "." & the suffix_name) as string
set newPathToCheck to ((macDir as string) & macName)
if (item newPathToCheck exists) = false then exit repeat
set sequential to sequential + 1
end repeat
set AppleScript's text item delimiters to default_delimiters
end if
set the (name of macFileCopy) to macName
end tell
return macName
end copyAndRename
-- Stop progress indicator immediately
on stopProcess()
tell progress indicator "progress" of window "Progress" to stop
set visible of progress indicator "progress" of window "Progress" to false
set visible of window "Progress" to false
set content of text field "path" of window "Main" to " …"
set visible of window "Main" to true
end stopProcess
-- Stop progress indicator
on endProcess()
repeat with i from 33 to 100
delay 1.0E-5
set content of progress indicator "progress" of window "Progress" to i
end repeat
tell progress indicator "progress" of window "Progress" to stop
set visible of progress indicator "progress" of window "Progress" to false
set visible of window "Progress" to false
set content of text field "path" of window "Main" to " …"
set visible of window "Main" to true
end endProcess