@kitschpatrol/aphex 0.1.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/aphex-swift CHANGED
Binary file
@@ -2,224 +2,258 @@
2
2
  -- It's an unsavory approach, but no other means of automation seems to yield higher quality edited image exports (so far)
3
3
  -- Arguments are passed in their order of appearance in the GUI, and expect values exactly matching the UI strings in Photos.app
4
4
  -- Accepts an individual photo UUID, or an album UUID (use aphex-swift to look up identifiers / UUIDs)
5
+
6
+ -- How long to wait for each UI element before giving up, so a stall fails with the step's name instead of hanging
7
+ property stepTimeoutSeconds : 15
8
+
5
9
  on run { uuid, exportDirectory, photoKind, jpegQuality, tiffBitDepth, colorProfile, photoSize, maxSizeType, maxSizeValue, includeMetadata, includeLocation , fileName, sequentialPrefix, subfolderFormat}
6
-
10
+
7
11
  tell application "System Events"
8
12
  set wasRunning to (name of processes) contains "Photos"
9
13
  end tell
10
-
11
- tell application "Photos"
12
- activate
13
-
14
- -- Reset selection and view
15
- tell application "System Events"
16
- tell process "Photos"
17
- repeat until exists menu bar 1
18
- delay 0.25
19
- end repeat
20
- click menu item "Library" of menu 1 of menu item "Photos" of menu 1 of menu bar item "View" of menu bar 1
21
- delay 0.1
22
- click menu item "Deselect All" of menu 1 of menu bar item "Edit" of menu bar 1
14
+
15
+ -- Whether this run has opened the export dialogs, which it then has to close again if it fails
16
+ set openedExportSheet to false
17
+
18
+ try
19
+ tell application "Photos"
20
+ activate
21
+
22
+ -- Reset selection and view
23
+ tell application "System Events"
24
+ tell process "Photos"
25
+ -- The menu bar appears before a freshly launched Photos has populated its menus
26
+ set waitDeadline to (current date) + (my stepTimeoutSeconds)
27
+ repeat while my libraryMenuItem() is missing value
28
+ my failIfPastDeadline(waitDeadline, "the View > Library menu item")
29
+ delay 0.25
30
+ end repeat
31
+ if exists sheet 1 of window 1 then
32
+ error "Photos already has a dialog open. Close it and try again."
33
+ end if
34
+ click my libraryMenuItem()
35
+ delay 0.1
36
+ click menu item "Deselect All" of menu 1 of menu bar item "Edit" of menu bar 1
37
+ end tell
23
38
  end tell
24
- end tell
25
-
26
- -- Select photo or album for export by UUID
27
- try
28
- spotlight media item id (uuid)
29
- on error
39
+
40
+ -- Select photo or album for export by UUID
41
+ -- Photos 12 (macOS 27) leaves Deselect All disabled here, so a UUID that isn't found would
42
+ -- leave the previous selection in place to be exported instead. Count what was found.
43
+ set foundCount to 0
30
44
  try
31
- spotlight album id (uuid)
45
+ spotlight media item id (uuid)
46
+ set foundCount to 1
47
+ on error
48
+ try
49
+ spotlight album id (uuid)
50
+ set foundCount to count of media items of album id (uuid)
51
+ end try
32
52
  end try
33
- end try
34
-
35
- if (count of selection) is 0 then
36
- log "No photos to export"
37
- if not wasRunning then
38
- tell application "Photos" to quit
53
+
54
+ if foundCount is 0 or (count of selection) is 0 then
55
+ error "No photo or album found with UUID " & uuid & ", or the album is empty."
39
56
  end if
40
- return
41
- end if
42
- end tell
43
-
44
- -- Create export destination directory if necessary
45
- -- try
46
- -- do shell script "mkdir -p " & quoted form of exportDirectory
47
- -- on error errMsg number errNum
48
- -- display alert "Error creating directory" message errMsg & " (Error " & errNum & ")"
49
- -- end try
50
-
51
- -- Store initial state for logging
52
- set initialFiles to filesInExportDirectory(exportDirectory)
53
-
54
- tell application "System Events"
55
- tell process "Photos"
56
-
57
- -- Export command
58
- repeat until exists menu bar 1
59
- delay 0.1
60
- end repeat
61
- click menu item 1 of menu "Export" of menu item "Export" of menu "File" of menu bar item "File" of menu bar 1
62
-
63
- repeat until exists sheet 1 of window 1
64
- delay 0.1
65
- end repeat
66
-
67
- -- Set export preferences
68
- tell sheet 1 of window 1
69
- tell group "Photos"
70
-
71
- -- Ensure the disclosure is open
72
- if not (exists pop up button "Color Profile:") then
73
- click UI element 3
74
- delay 0.5
75
- end if
76
-
77
- tell pop up button "Photo Kind:"
78
- if value is not photoKind then
79
- click
80
- click menu item photoKind of menu 1
57
+ end tell
58
+
59
+ -- Store initial state for logging
60
+ set initialFiles to filesInExportDirectory(exportDirectory)
61
+
62
+ tell application "System Events"
63
+ tell process "Photos"
64
+
65
+ -- Export command
66
+ set openedExportSheet to true
67
+ click menu item 1 of menu "Export" of menu item "Export" of menu "File" of menu bar item "File" of menu bar 1
68
+
69
+ set waitDeadline to (current date) + (my stepTimeoutSeconds)
70
+ repeat until exists sheet 1 of window 1
71
+ my failIfPastDeadline(waitDeadline, "the export options dialog")
72
+ delay 0.1
73
+ end repeat
74
+
75
+ -- Set export preferences
76
+ tell sheet 1 of window 1
77
+ tell group "Photos"
78
+
79
+ -- Ensure the disclosure is open
80
+ if not (exists pop up button "Color Profile:") then
81
+ click UI element 3
81
82
  delay 0.5
82
83
  end if
83
- end tell
84
-
85
- if photoKind is "JPEG" then
86
- tell pop up button "JPEG Quality:"
87
- if value is not jpegQuality then
84
+
85
+ tell pop up button "Photo Kind:"
86
+ if value is not photoKind then
88
87
  click
89
- click menu item jpegQuality of menu 1
88
+ click menu item photoKind of menu 1
90
89
  delay 0.5
91
90
  end if
92
91
  end tell
93
- end if
94
-
95
- if photoKind is "TIFF" then
96
- tell checkbox "16 Bit"
97
- if (value as boolean) is not (tiffBitDepth is 16) then
92
+
93
+ if photoKind is "JPEG" then
94
+ tell pop up button "JPEG Quality:"
95
+ if value is not jpegQuality then
96
+ click
97
+ click menu item jpegQuality of menu 1
98
+ delay 0.5
99
+ end if
100
+ end tell
101
+ end if
102
+
103
+ if photoKind is "TIFF" then
104
+ tell checkbox "16 Bit"
105
+ -- Arguments arrive as text, so compare as a number
106
+ if (value as boolean) is not ((tiffBitDepth as integer) is 16) then
107
+ click
108
+ end if
109
+ end tell
110
+ end if
111
+
112
+ tell pop up button "Color Profile:"
113
+ if value is not colorProfile then
98
114
  click
115
+ click menu item colorProfile of menu 1
116
+ delay 0.5
99
117
  end if
100
118
  end tell
101
- end if
102
-
103
- tell pop up button "Color Profile:"
104
- if value is not colorProfile then
105
- click
106
- click menu item colorProfile of menu 1
107
- delay 0.5
119
+
120
+ tell pop up button "Size:"
121
+ if value is not photoSize then
122
+ click
123
+ click menu item photoSize of menu 1
124
+ delay 0.5
125
+ end if
126
+ end tell
127
+
128
+ if photoSize is "Custom" then
129
+ tell group 1
130
+
131
+ tell pop up button "Max"
132
+ if value is not maxSizeType then
133
+ click
134
+ click menu item maxSizeType of menu 1
135
+ delay 0.5
136
+ end if
137
+ end tell
138
+
139
+ tell text field 1
140
+ if value is not maxSizeValue then
141
+ set value of attribute "AXFocused" to true
142
+ set value of attribute "AXValue" to maxSizeValue as string
143
+ perform action "AXConfirm"
144
+ end if
145
+ end tell
146
+
147
+ end tell
108
148
  end if
149
+
109
150
  end tell
110
-
111
- tell pop up button "Size:"
112
- if value is not photoSize then
113
- click
114
- click menu item photoSize of menu 1
115
- delay 0.5
116
- end if
151
+
152
+ tell group "Info"
153
+ tell checkbox "Title, Keywords, and Caption"
154
+ if (value as boolean) is not (includeMetadata as boolean) then
155
+ click
156
+ end if
157
+ end tell
158
+ tell checkbox "Location Information"
159
+ if (value as boolean) is not (includeLocation as boolean) then
160
+ click
161
+ end if
162
+ end tell
117
163
  end tell
118
-
119
- if photoSize is "Custom" then
120
- tell group 1
121
-
122
- tell pop up button "Max"
123
- if value is not maxSizeType then
124
- click
125
- click menu item maxSizeType of menu 1
126
- delay 0.5
127
- end if
128
- end tell
129
-
164
+
165
+ tell group "File Naming"
166
+ tell pop up button "File Name:"
167
+ if value is not fileName then
168
+ click
169
+ click menu item fileName of menu 1
170
+ delay 0.5
171
+ end if
172
+ end tell
173
+ if fileName is "Sequential" then
174
+ -- why not "Sequential Prefix:"?
130
175
  tell text field 1
131
- if value is not maxSizeValue then
176
+ if value is not sequentialPrefix then
132
177
  set value of attribute "AXFocused" to true
133
- set value of attribute "AXValue" to maxSizeValue as string
178
+ set value of attribute "AXValue" to sequentialPrefix
134
179
  perform action "AXConfirm"
135
180
  end if
136
181
  end tell
137
-
138
- end tell
139
- end if
140
-
141
- end tell
142
-
143
- tell group "Info"
144
- tell checkbox "Title, Keywords, and Caption"
145
- if (value as boolean) is not (includeMetadata as boolean) then
146
- click
147
- end if
148
- end tell
149
- tell checkbox "Location Information"
150
- if (value as boolean) is not (includeLocation as boolean) then
151
- click
152
- end if
153
- end tell
154
- end tell
155
-
156
- tell group "File Naming"
157
- tell pop up button "File Name:"
158
- if value is not fileName then
159
- click
160
- click menu item fileName of menu 1
161
- delay 0.5
162
182
  end if
163
- end tell
164
- if fileName is "Sequential" then
165
- -- why not "Sequential Prefix:"?
166
- tell text field 1
167
- if value is not sequentialPrefix then
168
- set value of attribute "AXFocused" to true
169
- set value of attribute "AXValue" to sequentialPrefix
170
- perform action "AXConfirm"
183
+ tell pop up button "Subfolder Format:"
184
+ if value is not subfolderFormat then
185
+ click
186
+ click menu item subfolderFormat of menu 1
187
+ delay 0.5
171
188
  end if
172
189
  end tell
173
- end if
174
- tell pop up button "Subfolder Format:"
175
- if value is not subfolderFormat then
176
- click
177
- click menu item subfolderFormat of menu 1
178
- delay 0.5
179
- end if
180
190
  end tell
191
+
192
+ delay 0.5
193
+ click button "Export"
194
+
181
195
  end tell
182
-
183
- delay 0.5
184
- click button "Export"
185
-
186
- end tell
187
-
188
- -- Set export location
189
- repeat until exists sheet 1 of window 1
190
- delay 0.1
191
- end repeat
192
-
193
- tell sheet 1 of window 1
194
- keystroke "g" using {command down, shift down}
195
- repeat until exists sheet 1
196
+
197
+ -- Set export location
198
+ set waitDeadline to (current date) + (my stepTimeoutSeconds)
199
+ repeat until exists sheet 1 of window 1
200
+ my failIfPastDeadline(waitDeadline, "the export folder dialog")
196
201
  delay 0.1
197
202
  end repeat
198
- tell sheet 1
199
- keystroke exportDirectory
200
- key code 76
203
+
204
+ -- Keystrokes go to whichever app is frontmost, so make sure it's Photos
205
+ set frontmost to true
206
+
207
+ tell sheet 1 of window 1
208
+ keystroke "g" using {command down, shift down}
209
+ set waitDeadline to (current date) + (my stepTimeoutSeconds)
210
+ repeat until exists sheet 1
211
+ my failIfPastDeadline(waitDeadline, "the Go to Folder dialog")
212
+ delay 0.1
213
+ end repeat
214
+ tell sheet 1
215
+ keystroke exportDirectory
216
+ key code 76
217
+ end tell
201
218
  end tell
202
-
219
+
220
+ set waitDeadline to (current date) + (my stepTimeoutSeconds)
221
+ repeat while my sheetButton("Export") is missing value
222
+ my failIfPastDeadline(waitDeadline, "the Export button of the export folder dialog")
223
+ delay 0.1
224
+ end repeat
225
+
203
226
  -- Getting stuck here...
204
227
  delay 1
205
- click button "Export"
228
+ click my sheetButton("Export")
229
+
230
+ -- Wait for the export to finish, but allow a few second for the progress indicator to appear
231
+ set startTime to (current date)
232
+ repeat until (not (exists progress indicator 1 of group 2 of toolbar 1 of window 1)) and ((current date) - startTime) > 2
233
+ delay 0.25
234
+ end repeat
235
+
206
236
  end tell
207
-
208
- -- Wait for the export to finish, but allow a few second for the progress indicator to appear
209
- set startTime to (current date)
210
- repeat until (not (exists progress indicator 1 of group 2 of toolbar 1 of window 1)) and ((current date) - startTime) > 2
211
- delay 0.25
212
- end repeat
213
-
214
237
  end tell
215
- end tell
216
-
238
+ on error errorMessage number errorNumber
239
+ -- Leave Photos as it was found, so that a later export doesn't run into this one's dialogs
240
+ try
241
+ if openedExportSheet then
242
+ dismissExportSheets()
243
+ end if
244
+ if not wasRunning then
245
+ quitPhotos()
246
+ end if
247
+ end try
248
+ error errorMessage number errorNumber
249
+ end try
250
+
217
251
  -- Clean up
218
252
  if not wasRunning then
219
- tell application "Photos" to quit
253
+ quitPhotos()
220
254
  end if
221
-
222
- -- Log the exported files
255
+
256
+ -- Log the exported files
223
257
  set currentFiles to filesInExportDirectory(exportDirectory)
224
258
  repeat with aFile in currentFiles
225
259
  if (initialFiles does not contain aFile) then
@@ -227,10 +261,90 @@ on run { uuid, exportDirectory, photoKind, jpegQuality, tiffBitDepth, colorProfi
227
261
  log aFile
228
262
  end if
229
263
  end repeat
230
-
264
+
231
265
  end run
232
266
 
233
267
  -- Handler to get list of files excluding hidden ones
234
268
  on filesInExportDirectory(exportDirectory)
235
269
  return paragraphs of (do shell script "find " & quoted form of exportDirectory & " -type f \\! -name \".*\"")
236
270
  end filesInExportDirectory
271
+
272
+ -- Handler to find the menu item that shows the whole library, or missing value until Photos has populated its menus
273
+ -- Photos 10 (macOS 15) keeps it under View > Photos, Photos 12 (macOS 27) at the top level of the View menu
274
+ on libraryMenuItem()
275
+ tell application "System Events"
276
+ tell process "Photos"
277
+ try
278
+ set viewMenu to menu 1 of menu bar item "View" of menu bar 1
279
+ if exists menu item "Library" of menu 1 of menu item "Photos" of viewMenu then
280
+ return menu item "Library" of menu 1 of menu item "Photos" of viewMenu
281
+ end if
282
+ if exists menu item "Library" of viewMenu then
283
+ return menu item "Library" of viewMenu
284
+ end if
285
+ end try
286
+ end tell
287
+ end tell
288
+ return missing value
289
+ end libraryMenuItem
290
+
291
+ -- Handler to find a button of the dialog in front, or missing value if it has none of that name
292
+ -- The folder dialog of macOS 27 keeps its buttons in a split group, the other dialogs keep them at the top level
293
+ on sheetButton(buttonName)
294
+ tell application "System Events"
295
+ tell process "Photos"
296
+ try
297
+ if exists button buttonName of sheet 1 of window 1 then
298
+ return button buttonName of sheet 1 of window 1
299
+ end if
300
+ if exists button buttonName of splitter group 1 of sheet 1 of window 1 then
301
+ return button buttonName of splitter group 1 of sheet 1 of window 1
302
+ end if
303
+ end try
304
+ end tell
305
+ end tell
306
+ return missing value
307
+ end sheetButton
308
+
309
+ -- Handler to close the export dialogs that a failed export left open
310
+ on dismissExportSheets()
311
+ tell application "System Events"
312
+ tell process "Photos"
313
+ -- A pop up menu that is still open has to be closed first. Photos answers nothing else
314
+ -- while it waits for a choice, not even once the dialog under the menu is gone.
315
+ try
316
+ repeat with popUp in (every pop up button of group "Photos" of sheet 1 of window 1)
317
+ if exists menu 1 of popUp then
318
+ perform action "AXCancel" of menu 1 of popUp
319
+ delay 0.5
320
+ end if
321
+ end repeat
322
+ end try
323
+ -- Cancelling a dialog takes any dialog on top of it along
324
+ repeat 3 times
325
+ if not (exists sheet 1 of window 1) then exit repeat
326
+ try
327
+ click my sheetButton("Cancel")
328
+ end try
329
+ delay 0.5
330
+ end repeat
331
+ end tell
332
+ end tell
333
+ end dismissExportSheets
334
+
335
+ -- Handler to quit Photos and wait until it's gone, so a following run doesn't drive a Photos that is still shutting down
336
+ on quitPhotos()
337
+ tell application "Photos" to quit
338
+ set waitDeadline to (current date) + stepTimeoutSeconds
339
+ repeat while application "Photos" is running
340
+ failIfPastDeadline(waitDeadline, "Photos to quit")
341
+ delay 0.25
342
+ end repeat
343
+ end quitPhotos
344
+
345
+ -- Handler to abort the export once a UI wait has run past its deadline
346
+ on failIfPastDeadline(waitDeadline, stepDescription)
347
+ if (current date) > waitDeadline then
348
+ error "Timed out after " & stepTimeoutSeconds & " seconds waiting for " & stepDescription & "."
349
+ end if
350
+ end failIfPastDeadline