-
Notifications
You must be signed in to change notification settings - Fork 75
/
index.js
executable file
·99 lines (94 loc) · 2.46 KB
/
index.js
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
document.getElementById("btn-layer-from-text").addEventListener("click", createLayerFromText);
document.getElementById("btn-layer-from-file").addEventListener("click", createLayerFromFile);
async function createLayerFromText() {
const mytext = document.getElementById("mytext").value;
if (mytext.trim()) {
const result = await makeTextLayer(mytext.trim());
if (result[0].message) { // a message in the result means error
showAlert(result[0].message);
}
else {
const layerID = result[0].layerID;
console.log(`after maketextLayer, id = ${layerID}`);
}
}
else {
showAlert("Please enter something in the text field first.");
}
}
async function createLayerFromFile() {
const fs = require("uxp").storage.localFileSystem; // always needed to access the filesystem
const myFile = await fs.getFileForOpening({ types: ["txt"] });
if (!myFile) {
showAlert("No file was selected.");
return;
}
const fileContents = await myFile.read();
const result = await makeTextLayer(fileContents);
if (result[0].message) { // a message in the result means error
showAlert(result[0].message);
}
}
async function showAlert(message) {
const app = require('photoshop').app;
await app.showAlert(message);
}
// make a text layer in the active document
// Since there's no built-in API to make a text layer (yet),
// We do it with a batchPlay call.
async function makeTextLayer(theText) {
const batchCommands = {
"_obj": "make",
"_target": [
{
"_ref": "textLayer"
}
],
"using": {
"_obj": "textLayer",
"textKey": theText,
"textShape": [
{
"_obj": "textShape",
"char": {
"_enum": "char",
"_value": "box"
},
"bounds": {
"_obj": "rectangle",
"top": 100,
"left": 100,
"bottom": 400,
"right": 500
}
}
],
"textStyleRange": [
{
"_obj": "textStyleRange",
"from": 0,
"to": theText.length,
"textStyle": {
"_obj": "textStyle",
"fontName": "Myriad Pro",
"fontStyleName": "Bold",
"size": {
"_unit": "pointsUnit",
"_value": 36
},
"color": {
"_obj": "RGBColor",
"red": 100,
"green": 0,
"blue": 240
},
}
}
],
"_isCommand": true
}
};
return await require("photoshop").core.executeAsModal(async () => {
await require('photoshop').action.batchPlay([batchCommands], {});
}, { commandName: "Make New Text Layer" });
}