-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenerate image set (Markdown).applescript
217 lines (184 loc) · 7.86 KB
/
Generate image set (Markdown).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
-- Generate a set of (Markdown) image links, that can be inserted into Markdown files.
-- The set will be saved into the clipboard and can be pasted into the desired Markdown
-- document.
--
-- author: Fabrizio Musacchio (https://www.fabriziomusacchio.com)
-- date: Feb 13, 2022
set GridChoices to {"grid 1 (100% width) + caption", "grid 1 (100% width)", "grid 2 (50% width) + caption", "grid 2 (50% width)", "grid 3 (33% width) + caption", "grid 3 (33% width)", "enter individual width + caption", "enter individual width"}
set TheGridChoice to choose from list GridChoices with prompt "Generating an image-set of currently selected images. The set will be copied to the clipboard and can be inserted into a Markdown file.
Please select a grid size, i.e., how many images to show per row. The '+ caption' option extracts the image annotations and adds them as captions under each image (for image widths >= 25%)." default items {"grid 1 (100% width) + caption"} with title "Choose image width"
if TheGridChoice is false then
error number -128
else if text of TheGridChoice is {"enter individual width"} then
set dialogResult to display dialog "Please enter an image width (in %)" buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel" default answer ("24") with title "Choose image width"
set InsertCaption to false --false true
set TheWidth to text returned of dialogResult
if dialogResult is false then error number -128
else if text of TheGridChoice is {"enter individual width + caption"} then
set dialogResult to display dialog "Please enter an image width (in %)" buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel" default answer ("24") with title "Choose image width"
set InsertCaption to true --false true
set TheWidth to text returned of dialogResult
if dialogResult is false then error number -128
else if text of TheGridChoice is {"grid 1 (100% width) + caption"} then
set InsertCaption to true --false true
set TheWidth to 100
else if text of TheGridChoice is {"grid 1 (100% width)"} then
set InsertCaption to false --false true
set TheWidth to 100
else if text of TheGridChoice is {"grid 2 (50% width) + caption"} then
set InsertCaption to true --false true
set TheWidth to 49
else if text of TheGridChoice is {"grid 2 (50% width)"} then
set InsertCaption to false --false true
set TheWidth to 49
else if text of TheGridChoice is {"grid 3 (33% width) + caption"} then
set InsertCaption to true --false true
set TheWidth to 32
else if text of TheGridChoice is {"grid 3 (33% width)"} then
set InsertCaption to false --false true
set TheWidth to 32
end if
tell application id "DNtp"
try
set theRecords to selected records
if theRecords = {} then error "Error: Please select at least one image file."
set NumberOfRecords to count of theRecords
set TheFullImageLink to "<center>
"
set TheImageLink to ""
set RunCounter to 0
repeat with thisRecord in theRecords
if the type of thisRecord is equal to picture then
set RunCounter to RunCounter + 1
try
set TheCaption to the comment of thisRecord
on error
set TheCaption to " "
end try
set TheImageName to thisRecord's name
set TheImageURL to "x-devonthink-item://" & thisRecord's uuid
if TheWidth is greater than 49 then
set TheGridSize to 1
else if TheWidth ² 49 and TheWidth > 32 then
set TheGridSize to 2
else if TheWidth ² 32 and TheWidth > 25 then
set TheGridSize to 3
else
set TheGridSize to 999
end if
if TheGridSize is equal to 1 then
-- the 100% width case:
if InsertCaption then
set TheImageLink to TheImageLink & TheFullImageLink & "[![" & TheImageName & "]]
" & TheCaption & "
</center>
[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
else
set TheImageLink to TheImageLink & TheFullImageLink & "[![" & TheImageName & "]]
</center>
[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
end if
else if TheGridSize is not 999 then
-- the 49%-32% and 32%-24% width cases:
if InsertCaption then
if RunCounter mod TheGridSize is not 0 then
if RunCounter mod TheGridSize is 1 then
set TheImageLink to TheImageLink & TheFullImageLink & "[![" & TheImageName & "]] "
set TheCaptionTmp to "
**Left**: " & TheCaption
set TheReferences to "[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
else
set TheImageLink to TheImageLink & "[![" & TheImageName & "]] "
set TheReferences to TheReferences & "[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
if TheGridSize is 3 then
set TheCaptionTmp to TheCaptionTmp & " Ð **Middle**: " & TheCaption
end if
end if
else
set TheImageLink to TheImageLink & "[![" & TheImageName & "]] "
set TheReferences to TheReferences & "[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
set TheCaptionTmp to TheCaptionTmp & " Ð **Right**: " & TheCaption
set TheImageLink to TheImageLink & TheCaptionTmp & "
</center>
" & TheReferences
end if
if RunCounter = NumberOfRecords and RunCounter mod TheGridSize is not 0 then
set TheImageLink to TheImageLink & TheCaptionTmp
-- set TheImageLink to TheImageLink & TheCaptionTmp & "
--</center>
--
--" & TheReferences
end if
else
if RunCounter mod TheGridSize is 1 then
set TheImageLink to TheImageLink & TheFullImageLink & "[![" & TheImageName & "]] "
set TheReferences to "[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
else if RunCounter mod TheGridSize is 2 then
set TheImageLink to TheImageLink & "[![" & TheImageName & "]] "
set TheReferences to TheReferences & "[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
else
set TheImageLink to TheImageLink & "[![" & TheImageName & "]]"
set TheReferences to TheReferences & "[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
set TheImageLink to TheImageLink & "
</center>
" & TheReferences
end if
end if
if RunCounter = NumberOfRecords and RunCounter mod TheGridSize is not 0 then
set TheImageLink to TheImageLink & "
</center>
" & TheReferences
end if
else
-- any other case (i.e., grid-widths smaller than 24%):
-- at the moment w/o a caption option.
if RunCounter is 1 then
set TheImageLink to TheImageLink & TheFullImageLink & "[![" & TheImageName & "]]"
set TheReferences to "[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
if InsertCaption then
set TheCaptionTmp to "
1\\. " & TheCaption
end if
else
set TheImageLink to TheImageLink & "[![" & TheImageName & "]]"
set TheReferences to TheReferences & "[" & TheImageName & "]: " & TheImageURL & " style=\"width:" & TheWidth & "%;\"
"
if InsertCaption then
set TheCaptionTmp to TheCaptionTmp & " Ð " & RunCounter & ". " & TheCaption
end if
end if
if RunCounter is NumberOfRecords then
if InsertCaption then
set TheImageLink to TheImageLink & TheCaptionTmp & "
</center>
" & TheReferences
else
set TheImageLink to TheImageLink & "
</center>
" & TheReferences
end if
end if
end if
if NumberOfRecords is greater than 1 and RunCounter is not equal to NumberOfRecords then
set TheImageLink to TheImageLink & "
"
end if
end if
end repeat
set TheFullImageLink to TheImageLink
set the clipboard to TheFullImageLink as string
TheFullImageLink
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell