File size: 10,111 Bytes
34097e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { batchPlay } = require('photoshop').action
const { executeAsModal } = require('photoshop').core
const {
    cleanLayers,
    getLayerIndex,
    selectLayers,
    unSelectMarqueeCommand,
    unSelectMarqueeExe,
    getSelectionInfoExe,
    reSelectMarqueeExe,
} = require('../psapi')

const psapi = require('../psapi')

async function createNewLayerExe(layerName, opacity = 100) {
    await executeAsModal(async () => {
        await createNewLayerCommand(layerName, opacity)
    })
    const new_layer = await app.activeDocument.activeLayers[0]
    return new_layer
}

async function createNewLayerCommand(layerName, opacity = 100) {
    return await app.activeDocument.createLayer({
        name: layerName,
        opacity: opacity,
        mode: 'normal',
    })
}

async function deleteLayers(layers) {
    try {
        await cleanLayers(layers)
    } catch (e) {
        console.warn(e)
    }
}

async function getIndexCommand() {
    const command = {
        _obj: 'get',
        _target: [
            {
                _property: 'itemIndex',
            },
            {
                _ref: 'layer',
                _enum: 'ordinal',
                _value: 'targetEnum',
            },
        ],
    }
    const result = await batchPlay([command], {
        synchronousExecution: true,
        modalBehavior: 'execute',
    })

    return result
}

async function getIndexExe() {
    let index
    await executeAsModal(async () => {
        index = await getIndexCommand()
    })

    return index
}
const photoshop = require('photoshop')

const collapseFolderCommand = async (expand = false, recursive = false) => {
    let result
    try {
        result = await batchPlay(
            [
                {
                    _obj: 'set',
                    _target: {
                        _ref: [
                            { _property: 'layerSectionExpanded' },
                            {
                                _ref: 'layer',
                                _enum: 'ordinal',
                                _value: 'targetEnum',
                            },
                        ],
                    },
                    to: expand,
                    recursive,
                    _options: { dialogOptions: 'dontDisplay' },
                },
            ],
            { synchronousExecution: true }
        )
    } catch (e) {
        console.error(e.message)
    }
    return result
}
async function collapseFolderExe(layers, expand = false, recursive = false) {
    for (let layer of layers) {
        try {
            await executeAsModal(async () => {
                const is_visible = await layer.visible // don't change the visiblity of the layer when collapsing
                await selectLayers([layer])
                await collapseFolderCommand(expand, recursive)
                layer.visible = is_visible
            })
        } catch (e) {
            console.warn(e)
        }
    }
}

class Layer {
    static doesLayerExist(layer) {
        let b_exist = false
        try {
            if (typeof layer !== 'undefined' && layer && layer.name) {
                //it will throw an error if the layer has been deleted
                b_exist = true
                // return true
            }
            // b_exist = true
        } catch (e) {
            b_exist = false
            // console.warn(e)
        }
        return b_exist
    }
    static async getLayerInfo(layer) {
        const bounds = layer.bounds
        const height = bounds.bottom - bounds.top
        const width = bounds.right - bounds.left
        const layer_info = {
            height: height,
            width: width,
            left: bounds.left,
            right: bounds.right,
            top: bounds.top,
            bottom: bounds.bottom,
        }
        console.log('layer_info:', layer_info)
        return layer_info
    }
    static async scaleTo(layer, new_width, new_height) {
        await executeAsModal(async () => {
            try {
                const selection_info = await psapi.getSelectionInfoExe()
                await psapi.unSelectMarqueeExe()

                console.log('scaleLayer got called')
                // const activeLayer = getActiveLayer()
                // const activeLayer = await app.activeDocument.activeLayers[0]

                const layer_info = await this.getLayerInfo(layer)
                const scale_x_ratio = (new_width / layer_info.width) * 100
                const scale_y_ratio = (new_height / layer_info.height) * 100
                console.log('scale_x_y_ratio:', scale_x_ratio, scale_y_ratio)
                await layer.scale(scale_x_ratio, scale_y_ratio)
                await psapi.reSelectMarqueeExe(selection_info)
            } catch (e) {
                console.warn(e)
            }
        })
    }

    static async moveTo(layer, to_x, to_y) {
        try {
            await executeAsModal(async () => {
                try {
                    //translate doesn't work with selection active. so store the selection and then unselect. move the layer, then reselect the selection info
                    const selection_info = await psapi.getSelectionInfoExe()
                    await psapi.unSelectMarqueeExe()

                    const layer_info = await this.getLayerInfo(layer)
                    const top_dist = layer_info.top - to_y
                    const left_dist = layer_info.left - to_x
                    console.log('-left_dist, -top_dist', -left_dist, -top_dist)
                    await layer.translate(-left_dist, -top_dist)

                    await psapi.reSelectMarqueeExe(selection_info)
                } catch (e) {
                    console.warn(e)
                }
            })
        } catch (e) {
            console.warn(e)
        }
    }
    static resizeTo() {}
    static fitSelection() {}
    static async duplicateToDoc(layer, to_doc) {
        const dupLayer = await layer.duplicate(to_doc)
        // await selectLayers([dupLayer])
        return dupLayer
    }

    static async duplicateLayerExe(layer) {
        let layer_copy
        try {
            await executeAsModal(async () => {
                layer_copy = await layer.duplicate()
            })
        } catch (e) {
            console.warn('duplication error:', e)
        }
        return layer_copy
    }

    static {}
}

const hasBackgroundLayerDesc = () => ({
    _obj: 'get',
    _target: [
        { _property: 'hasBackgroundLayer' },
        {
            _ref: 'document',
            _enum: 'ordinal',
            _value: 'targetEnum',
        },
    ],
})
async function hasBackgroundLayer() {
    // check if a document has a background layer
    try {
        const result = await batchPlay([hasBackgroundLayerDesc()], {
            synchronousExecution: true,
            modalBehavior: 'execute',
        })
        const b_hasBackgroundLayer = result[0]?.hasBackgroundLayer
        return b_hasBackgroundLayer
    } catch (e) {
        console.warn(e)
    }
}
const makeBackgroundLayerDesc = () => ({
    _obj: 'make',
    _target: [
        {
            _ref: 'backgroundLayer',
        },
    ],
    using: {
        _ref: 'layer',
        _enum: 'ordinal',
        _value: 'targetEnum',
    },
    _options: { failOnMissingProperty: false, failOnMissingElement: false },
    // _options: {
    //     dialogOptions: 'dontDisplay',
    // },
})

const createSolidLayerDesc = (r, g, b) => ({
    _obj: 'make',
    _target: [
        {
            _ref: 'contentLayer',
        },
    ],
    using: {
        _obj: 'contentLayer',
        type: {
            _obj: 'solidColorLayer',
            color: {
                _obj: 'RGBColor',
                red: r,
                grain: g,
                blue: b,
            },
        },
    },
    _options: {
        dialogOptions: 'dontDisplay',
    },
})

const toggleBackgroundLayerDesc = () => ({
    _obj: 'show',
    null: [
        {
            _ref: 'layer',
            _property: 'background',
        },
    ],
    toggleOptionsPalette: true,
    _options: {
        dialogOptions: 'dontDisplay',
    },
})

async function toggleBackgroundLayerExe() {
    try {
        await executeAsModal(async () => {
            const result = await batchPlay([toggleBackgroundLayerDesc()], {
                synchronousExecution: true,
                modalBehavior: 'execute',
            })
            console.log('toggleBackgroundLayerExe result: ', result)
        })
    } catch (e) {
        console.warn(e)
    }
}

async function createBackgroundLayer(r = 255, g = 255, b = 255) {
    try {
        const has_background = await hasBackgroundLayer()
        if (has_background) {
            //no need to create a background layer
            return null
        }

        //reselect the selection area if it exist

        await executeAsModal(async () => {
            //store the selection area and then unselected
            const selectionInfo = await psapi.getSelectionInfoExe()
            await psapi.unSelectMarqueeExe()
            const active_layers = app.activeDocument.activeLayers

            // await createNewLayerCommand('background') //create layer
            //make the layer into background
            const result = await batchPlay(
                [createSolidLayerDesc(r, g, b), makeBackgroundLayerDesc()],
                {
                    synchronousExecution: true,
                    modalBehavior: 'execute',
                }
            )

            await psapi.reSelectMarqueeExe(selectionInfo)
            await psapi.selectLayersExe(active_layers)
        })
    } catch (e) {
        console.warn(e)
    }
}
async function fixImageBackgroundLayer() {
    //convert the background layer to a normal layer
    //create a new layer
    //convert the new layer to background
}
module.exports = {
    createNewLayerExe,
    deleteLayers,
    getIndexExe,
    collapseFolderExe,
    Layer,
    hasBackgroundLayer,
    createBackgroundLayer,
    createSolidLayerDesc,
    makeBackgroundLayerDesc,
    toggleBackgroundLayerExe,
}