@ajsmonty/adobe-mcp 1.1.2 → 1.5.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/CHANGELOG.md +19 -0
- package/README.md +361 -119
- package/SECURITY.md +24 -1
- package/adapters/ae_more.mjs +208 -0
- package/adapters/an.mjs +96 -0
- package/adapters/assets.mjs +194 -0
- package/adapters/blender.mjs +230 -0
- package/adapters/id.mjs +250 -0
- package/adapters/ppro.mjs +4 -3
- package/adapters/ppro_more.mjs +393 -0
- package/adapters/ps_ai_more.mjs +277 -0
- package/adapters/substance.mjs +193 -0
- package/adapters/texlib.mjs +193 -0
- package/bridges/blender/adobe_mcp_bridge.py +132 -0
- package/cep/mcp-bridge/CSXS/manifest.xml +36 -2
- package/cep/mcp-bridge/main.js +48 -20
- package/cli/doctor.mjs +234 -0
- package/cli/index.mjs +67 -0
- package/cli/install.mjs +149 -0
- package/core/barrows.mjs +16 -0
- package/core/blender_pbr.py +148 -0
- package/core/bridge.mjs +166 -77
- package/core/harness.jsx +11 -6
- package/core/harness.py +34 -0
- package/core/helpers.mjs +196 -0
- package/core/platform.mjs +209 -0
- package/core/pybridge.mjs +256 -0
- package/core/telemetry.mjs +80 -0
- package/core/texlib.mjs +279 -0
- package/core/win/run_com.js +33 -0
- package/core/workspace.mjs +1 -1
- package/knowledge/knowledge.mjs +8 -5
- package/knowledge/lessons.jsonl +19 -0
- package/package.json +56 -3
- package/scripts/ae/Cinematic_3D_Composite.jsx +173 -0
- package/scripts/blender/Creature_Walk_Cinematic.py +569 -0
- package/scripts/substance/Creature_Skin_LookDev.py +148 -0
- package/server.mjs +92 -27
- package/workflow/setup.mjs +106 -0
- package/workflow/vfx.mjs +136 -0
- package/workflow/workflow.mjs +66 -21
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// After Effects: template filling, data-driven versions, render queue / Media Encoder.
|
|
2
|
+
// Ported from the Barrows line of adobe-mcp (contributions by Derrin Byrne).
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import { runJSX } from "../core/bridge.mjs";
|
|
7
|
+
import { lit, esPath, loadRows, safeName } from "../core/helpers.mjs";
|
|
8
|
+
|
|
9
|
+
// Shared ExtendScript helpers (ES3).
|
|
10
|
+
const AE_LIB = `
|
|
11
|
+
function findComp(name) {
|
|
12
|
+
for (var i = 1; i <= app.project.numItems; i++) {
|
|
13
|
+
var it = app.project.item(i);
|
|
14
|
+
if (it instanceof CompItem && it.name === name) return it;
|
|
15
|
+
}
|
|
16
|
+
var names = [];
|
|
17
|
+
for (var j = 1; j <= app.project.numItems; j++) if (app.project.item(j) instanceof CompItem) names.push(app.project.item(j).name);
|
|
18
|
+
throw new Error('Comp "' + name + '" not found. Comps: ' + names.slice(0, 40).join(', '));
|
|
19
|
+
}
|
|
20
|
+
var __footCache = {};
|
|
21
|
+
function importFootage(p) {
|
|
22
|
+
if (__footCache[p]) return __footCache[p];
|
|
23
|
+
var f = new File(p);
|
|
24
|
+
if (!f.exists) throw new Error('File not found: ' + p);
|
|
25
|
+
for (var i = 1; i <= app.project.numItems; i++) {
|
|
26
|
+
var it = app.project.item(i);
|
|
27
|
+
if (it instanceof FootageItem && it.file && it.file.fsName === f.fsName) { __footCache[p] = it; return it; }
|
|
28
|
+
}
|
|
29
|
+
var item = app.project.importFile(new ImportOptions(f));
|
|
30
|
+
__footCache[p] = item;
|
|
31
|
+
return item;
|
|
32
|
+
}
|
|
33
|
+
function setText(layer, value) {
|
|
34
|
+
var st = layer.property('ADBE Text Properties').property('ADBE Text Document');
|
|
35
|
+
if (st.numKeys > 0) {
|
|
36
|
+
for (var k = 1; k <= st.numKeys; k++) { var tdk = st.keyValue(k); tdk.text = String(value); st.setValueAtKey(k, tdk); }
|
|
37
|
+
} else { var td = st.value; td.text = String(value); st.setValue(td); }
|
|
38
|
+
}
|
|
39
|
+
function applyFields(comp, textMap, footMap, report) {
|
|
40
|
+
for (var ln in textMap) {
|
|
41
|
+
var L = comp.layer(ln);
|
|
42
|
+
if (!L) { report.missing.push(ln); continue; }
|
|
43
|
+
if (!(L instanceof TextLayer)) { report.missing.push(ln + ' (not a text layer)'); continue; }
|
|
44
|
+
setText(L, textMap[ln]); report.text++;
|
|
45
|
+
}
|
|
46
|
+
for (var fn in footMap) {
|
|
47
|
+
var F = comp.layer(fn);
|
|
48
|
+
if (!F) { report.missing.push(fn); continue; }
|
|
49
|
+
if (!footMap[fn]) continue;
|
|
50
|
+
F.replaceSource(importFootage(footMap[fn]), false); report.footage++;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function queueComp(comp, opts) {
|
|
54
|
+
var rq = app.project.renderQueue.items.add(comp);
|
|
55
|
+
if (opts.render_settings_template) {
|
|
56
|
+
try { rq.applyTemplate(opts.render_settings_template); }
|
|
57
|
+
catch (e) { throw new Error('Render settings template "' + opts.render_settings_template + '" not found. Available: ' + rq.templates.join(', ')); }
|
|
58
|
+
}
|
|
59
|
+
var om = rq.outputModule(1);
|
|
60
|
+
if (opts.output_module_template) {
|
|
61
|
+
try { om.applyTemplate(opts.output_module_template); }
|
|
62
|
+
catch (e2) { throw new Error('Output module template "' + opts.output_module_template + '" not found. Available: ' + om.templates.join(', ')); }
|
|
63
|
+
}
|
|
64
|
+
var folder = new Folder(opts.output_folder);
|
|
65
|
+
if (!folder.exists) folder.create();
|
|
66
|
+
om.file = new File(opts.output_folder + '/' + (opts.file_name || comp.name));
|
|
67
|
+
return { comp: comp.name, output: om.file.fsName };
|
|
68
|
+
}
|
|
69
|
+
function startQueue(mode) {
|
|
70
|
+
if (mode === 'render') { app.project.renderQueue.render(); return 'rendered'; }
|
|
71
|
+
if (mode === 'ame') { app.project.renderQueue.queueInAME(true); return 'sent to Media Encoder (rendering there)'; }
|
|
72
|
+
if (mode === 'ame_queue') { app.project.renderQueue.queueInAME(false); return 'sent to Media Encoder queue (not started)'; }
|
|
73
|
+
return 'queued in After Effects (not started)';
|
|
74
|
+
}
|
|
75
|
+
`;
|
|
76
|
+
|
|
77
|
+
const renderSchema = {
|
|
78
|
+
output_folder: z.string().describe("Folder for rendered files (created if missing)."),
|
|
79
|
+
output_module_template: z.string().optional().describe("AE output module template name, e.g. 'H.264 - Match Render Settings - 15 Mbps'. Omit for AE's default."),
|
|
80
|
+
render_settings_template: z.string().optional().describe("AE render settings template name, e.g. 'Best Settings'."),
|
|
81
|
+
start: z.enum(["none", "render", "ame", "ame_queue"]).optional()
|
|
82
|
+
.describe("none = just queue (default); render = render now in AE (blocks); ame = send to Media Encoder and start; ame_queue = send to Media Encoder without starting."),
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export function register(server, { text, errText, registerAsset }) {
|
|
86
|
+
server.registerTool(
|
|
87
|
+
"ae_template_fill",
|
|
88
|
+
{
|
|
89
|
+
title: "Fill an After Effects template comp",
|
|
90
|
+
description:
|
|
91
|
+
"Set text layers and swap footage/image layers in a comp by layer name — the reliable way to customise " +
|
|
92
|
+
"a template. Optionally duplicate the comp first so the template stays untouched (note: duplicating a " +
|
|
93
|
+
"comp does not duplicate precomps inside it; edit precomps directly if the layers live there).",
|
|
94
|
+
inputSchema: {
|
|
95
|
+
comp: z.string().describe("Comp name."),
|
|
96
|
+
duplicate_as: z.string().optional().describe("Duplicate the comp under this name and edit the copy."),
|
|
97
|
+
text: z.record(z.string()).optional().describe("{ layerName: newText }"),
|
|
98
|
+
footage: z.record(z.string()).optional().describe("{ layerName: absolutePathToReplacementFile }"),
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
async ({ comp, duplicate_as, text: textMap, footage }) => {
|
|
102
|
+
try {
|
|
103
|
+
const jsx = `${AE_LIB}
|
|
104
|
+
var c = findComp(${lit(comp)});
|
|
105
|
+
if (${lit(duplicate_as || null)}) { c = c.duplicate(); c.name = ${lit(duplicate_as || "")}; }
|
|
106
|
+
var rep = { text: 0, footage: 0, missing: [] };
|
|
107
|
+
applyFields(c, ${lit(textMap || {})}, ${lit(Object.fromEntries(Object.entries(footage || {}).map(([k, v]) => [k, esPath(v)])))}, rep);
|
|
108
|
+
return { comp: c.name, text_layers_set: rep.text, footage_replaced: rep.footage, layers_not_found: rep.missing };`;
|
|
109
|
+
return text(await runJSX("ae", jsx, 300000));
|
|
110
|
+
} catch (e) {
|
|
111
|
+
return errText(e);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
server.registerTool(
|
|
117
|
+
"ae_versions_from_data",
|
|
118
|
+
{
|
|
119
|
+
title: "Make After Effects versions from a spreadsheet",
|
|
120
|
+
description:
|
|
121
|
+
"Data-driven versioning: for each row of a CSV/TSV (or inline rows), duplicate a template comp, fill " +
|
|
122
|
+
"its text and footage layers from columns, and optionally queue/render every version. Use for price " +
|
|
123
|
+
"changes, store/region variants, multiple languages, promo lists. Test with max_rows: 1 first.",
|
|
124
|
+
inputSchema: {
|
|
125
|
+
comp: z.string().describe("Template comp name."),
|
|
126
|
+
csv_path: z.string().optional().describe("Path to a .csv or .tsv with a header row."),
|
|
127
|
+
rows: z.array(z.record(z.string())).optional().describe("Inline rows instead of csv_path."),
|
|
128
|
+
text_columns: z.record(z.string()).optional().describe("{ layerName: columnName } for text layers."),
|
|
129
|
+
footage_columns: z.record(z.string()).optional().describe("{ layerName: columnName } where the column holds a file path."),
|
|
130
|
+
name_column: z.string().optional().describe("Column used to name each version (default: row number)."),
|
|
131
|
+
name_prefix: z.string().optional().describe("Prefix for version names (default: template comp name)."),
|
|
132
|
+
max_rows: z.number().int().optional().describe("Only process the first N rows (good for a test run)."),
|
|
133
|
+
render: z.object(renderSchema).optional().describe("Queue every version for rendering."),
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
async ({ comp, csv_path, rows, text_columns, footage_columns, name_column, name_prefix, max_rows, render }) => {
|
|
137
|
+
try {
|
|
138
|
+
let data = loadRows({ rows, csv_path });
|
|
139
|
+
if (max_rows) data = data.slice(0, max_rows);
|
|
140
|
+
const jobs = data.map((r, i) => {
|
|
141
|
+
const name = `${name_prefix ?? comp} - ${safeName(name_column ? r[name_column] : String(i + 1).padStart(3, "0"))}`;
|
|
142
|
+
const t = {}, f = {};
|
|
143
|
+
for (const [layer, col] of Object.entries(text_columns || {})) {
|
|
144
|
+
if (!(col in r)) throw new Error(`Column "${col}" not in data. Columns: ${Object.keys(r).join(", ")}`);
|
|
145
|
+
t[layer] = r[col];
|
|
146
|
+
}
|
|
147
|
+
for (const [layer, col] of Object.entries(footage_columns || {})) if (r[col]) f[layer] = esPath(r[col]);
|
|
148
|
+
return { name, text: t, footage: f };
|
|
149
|
+
});
|
|
150
|
+
const jsx = `${AE_LIB}
|
|
151
|
+
var tpl = findComp(${lit(comp)});
|
|
152
|
+
var jobs = ${lit(jobs)}, render = ${lit(render ? { ...render, output_folder: esPath(render.output_folder) } : null)};
|
|
153
|
+
var out = [], missing = {};
|
|
154
|
+
var folder = app.project.items.addFolder(${lit(`${comp} versions`)});
|
|
155
|
+
for (var i = 0; i < jobs.length; i++) {
|
|
156
|
+
var c = tpl.duplicate(); c.name = jobs[i].name; c.parentFolder = folder;
|
|
157
|
+
var rep = { text: 0, footage: 0, missing: [] };
|
|
158
|
+
applyFields(c, jobs[i].text, jobs[i].footage, rep);
|
|
159
|
+
for (var m = 0; m < rep.missing.length; m++) missing[rep.missing[m]] = true;
|
|
160
|
+
var entry = { comp: c.name };
|
|
161
|
+
if (render) entry.output = queueComp(c, { output_folder: render.output_folder, output_module_template: render.output_module_template,
|
|
162
|
+
render_settings_template: render.render_settings_template, file_name: c.name }).output;
|
|
163
|
+
out.push(entry);
|
|
164
|
+
}
|
|
165
|
+
var started = render ? startQueue(render.start || 'none') : null;
|
|
166
|
+
var miss = []; for (var k in missing) miss.push(k);
|
|
167
|
+
return { versions: out.length, project_folder: folder.name, items: out, layers_not_found: miss, render_status: started };`;
|
|
168
|
+
const timeout = render?.start === "render" ? 6 * 3600 * 1000 : 600000;
|
|
169
|
+
return text(await runJSX("ae", jsx, timeout));
|
|
170
|
+
} catch (e) {
|
|
171
|
+
return errText(e);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
server.registerTool(
|
|
177
|
+
"ae_queue_render",
|
|
178
|
+
{
|
|
179
|
+
title: "Queue After Effects comps for rendering",
|
|
180
|
+
description:
|
|
181
|
+
"Add one or more comps to the render queue with chosen output module / render settings templates and " +
|
|
182
|
+
"an output folder, then optionally render now or hand them to Adobe Media Encoder. If a template name " +
|
|
183
|
+
"is wrong, the error lists the available templates.",
|
|
184
|
+
inputSchema: { comps: z.array(z.string()).min(1).describe("Comp names."), ...renderSchema },
|
|
185
|
+
},
|
|
186
|
+
async ({ comps, output_folder, output_module_template, render_settings_template, start }) => {
|
|
187
|
+
try {
|
|
188
|
+
const jsx = `${AE_LIB}
|
|
189
|
+
var names = ${lit(comps)}, out = [];
|
|
190
|
+
for (var i = 0; i < names.length; i++) out.push(queueComp(findComp(names[i]), { output_folder: ${lit(esPath(output_folder))},
|
|
191
|
+
output_module_template: ${lit(output_module_template || null)}, render_settings_template: ${lit(render_settings_template || null)} }));
|
|
192
|
+
return { queued: out, status: startQueue(${lit(start || "none")}) };`;
|
|
193
|
+
const r = await runJSX("ae", jsx, start === "render" ? 6 * 3600 * 1000 : 300000);
|
|
194
|
+
if (start === "render" && registerAsset)
|
|
195
|
+
for (const q of r.queued || []) {
|
|
196
|
+
const dir = path.dirname(q.output);
|
|
197
|
+
try {
|
|
198
|
+
const f = fs.readdirSync(dir).find((n) => n.startsWith(path.basename(q.output)));
|
|
199
|
+
if (f) registerAsset({ app: "ae", kind: "video", path: path.join(dir, f), meta: { comp: q.comp } });
|
|
200
|
+
} catch { /* ignore */ }
|
|
201
|
+
}
|
|
202
|
+
return text(r);
|
|
203
|
+
} catch (e) {
|
|
204
|
+
return errText(e);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
);
|
|
208
|
+
}
|
package/adapters/an.mjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Animate adapter (JSFL, handed to Animate on the command line; result via file).
|
|
2
|
+
// Ported from the Barrows line of adobe-mcp (contributions by Derrin Byrne).
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { runJSFL, lit, esPath } from "../core/helpers.mjs";
|
|
6
|
+
|
|
7
|
+
export function register(server, { text, errText, registerAsset }) {
|
|
8
|
+
server.registerTool(
|
|
9
|
+
"an_run_jsfl",
|
|
10
|
+
{
|
|
11
|
+
title: "Run JSFL in Adobe Animate",
|
|
12
|
+
description:
|
|
13
|
+
"Execute JSFL (Animate's JavaScript API) inside Animate and return the result. Runs as a function body: " +
|
|
14
|
+
"end with `return <value>`. Key DOM: fl.getDocumentDOM(), doc.getTimeline(), timeline.layers[].frames[], " +
|
|
15
|
+
"doc.library.items, doc.exportPNG / exportVideo / publish, FLfile for files (file:/// URIs — convert with " +
|
|
16
|
+
"FLfile.platformPathToURI). Animate must be open.",
|
|
17
|
+
inputSchema: {
|
|
18
|
+
code: z.string(),
|
|
19
|
+
timeout_seconds: z.number().optional().describe("Default 120."),
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
async ({ code, timeout_seconds }) => {
|
|
23
|
+
try {
|
|
24
|
+
return text({ ok: true, result: (await runJSFL(code, (timeout_seconds ?? 120) * 1000)) ?? null });
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return errText(e);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
server.registerTool(
|
|
32
|
+
"an_get_state",
|
|
33
|
+
{
|
|
34
|
+
title: "Inspect open Animate documents",
|
|
35
|
+
description: "Lists open documents; for the current one: stage size, frame rate, document type, scenes, layers with frame counts, and library items.",
|
|
36
|
+
inputSchema: {},
|
|
37
|
+
},
|
|
38
|
+
async () => {
|
|
39
|
+
try {
|
|
40
|
+
const code = `
|
|
41
|
+
var out = { documents: [], current: null };
|
|
42
|
+
for (var i = 0; i < fl.documents.length; i++) out.documents.push(fl.documents[i].name);
|
|
43
|
+
var d = fl.getDocumentDOM();
|
|
44
|
+
if (d) {
|
|
45
|
+
var tl = d.getTimeline(), layers = [];
|
|
46
|
+
for (var l = 0; l < tl.layers.length; l++) layers.push({ name: tl.layers[l].name, type: tl.layers[l].layerType, frames: tl.layers[l].frameCount });
|
|
47
|
+
var lib = [];
|
|
48
|
+
for (var k = 0; k < d.library.items.length && k < 300; k++) lib.push({ name: d.library.items[k].name, type: d.library.items[k].itemType });
|
|
49
|
+
var scenes = []; for (var s = 0; s < d.timelines.length; s++) scenes.push(d.timelines[s].name);
|
|
50
|
+
out.current = { name: d.name, path: d.pathURI || null, width: d.width, height: d.height, fps: d.frameRate,
|
|
51
|
+
type: d.type || null, scenes: scenes, frames: tl.frameCount, layers: layers, library: lib };
|
|
52
|
+
}
|
|
53
|
+
return out;`;
|
|
54
|
+
return text(await runJSFL(code, 120000));
|
|
55
|
+
} catch (e) {
|
|
56
|
+
return errText(e);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
server.registerTool(
|
|
62
|
+
"an_export",
|
|
63
|
+
{
|
|
64
|
+
title: "Export from Adobe Animate",
|
|
65
|
+
description:
|
|
66
|
+
"Export the current Animate document: 'png_sequence' (every frame), 'png_frame' (current frame), " +
|
|
67
|
+
"'video' (MOV/MP4 via Animate's video export), or 'publish' (runs File → Publish with the document's " +
|
|
68
|
+
"own publish settings, e.g. HTML5 Canvas).",
|
|
69
|
+
inputSchema: {
|
|
70
|
+
kind: z.enum(["png_sequence", "png_frame", "video", "publish"]),
|
|
71
|
+
output_path: z.string().optional().describe("Required except for publish. For png_sequence give the first file name, e.g. .../frames/shot.png"),
|
|
72
|
+
transparent: z.boolean().optional().describe("Video: transparent background where supported."),
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
async ({ kind, output_path, transparent }) => {
|
|
76
|
+
try {
|
|
77
|
+
if (kind !== "publish" && !output_path) throw new Error("output_path is required.");
|
|
78
|
+
const code = `
|
|
79
|
+
var d = fl.getDocumentDOM(); if (!d) throw new Error('No Animate document is open.');
|
|
80
|
+
var kind = ${lit(kind)}, p = ${lit(output_path ? esPath(output_path) : null)};
|
|
81
|
+
if (p) { var dir = p.substring(0, p.lastIndexOf('/')); FLfile.createFolder(FLfile.platformPathToURI(dir)); }
|
|
82
|
+
var uri = p ? FLfile.platformPathToURI(p) : null, ok;
|
|
83
|
+
if (kind === 'png_sequence') ok = d.exportPNG(uri, true, false);
|
|
84
|
+
else if (kind === 'png_frame') ok = d.exportPNG(uri, true, true);
|
|
85
|
+
else if (kind === 'video') ok = d.exportVideo(uri, false, ${lit(!!transparent)}, false, 0);
|
|
86
|
+
else { d.publish(); ok = true; }
|
|
87
|
+
return { document: d.name, kind: kind, output: p, ok: ok !== false };`;
|
|
88
|
+
const r = await runJSFL(code, 1800000);
|
|
89
|
+
if (output_path) registerAsset?.({ app: "an", kind: path.extname(output_path).slice(1) || kind, path: output_path, meta: { doc: r.document } });
|
|
90
|
+
return text(r);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
return errText(e);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
);
|
|
96
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// Asset tools (what people use Adobe Bridge for): batch rename + XMP metadata.
|
|
2
|
+
// Ported from the Barrows line of adobe-mcp (contributions by Derrin Byrne).
|
|
3
|
+
// Renaming is done directly on disk. Metadata uses Adobe's XMP library inside whichever
|
|
4
|
+
// Adobe app is already open (Photoshop, Illustrator, InDesign or After Effects).
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { runJSX } from "../core/bridge.mjs";
|
|
9
|
+
import { lit, esPath, walk, runningApps } from "../core/helpers.mjs";
|
|
10
|
+
|
|
11
|
+
const XMP_LIB = `
|
|
12
|
+
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
|
|
13
|
+
var NS = XMPConst.NS_DC;
|
|
14
|
+
function arr(x, prop) { var o = []; var n = x.countArrayItems(NS, prop); for (var i = 1; i <= n; i++) o.push(String(x.getArrayItem(NS, prop, i))); return o; }
|
|
15
|
+
function loc(x, prop) { try { var v = x.getLocalizedText(NS, prop, null, 'x-default'); return v ? String(v) : ''; } catch (e) { return ''; } }
|
|
16
|
+
function readMeta(p) {
|
|
17
|
+
var xf = new XMPFile(p, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ), x = xf.getXMP(); xf.closeFile();
|
|
18
|
+
if (!x) return { file: p, title: '', description: '', keywords: [], creator: [], copyright: '' };
|
|
19
|
+
return { file: p, title: loc(x, 'title'), description: loc(x, 'description'), keywords: arr(x, 'subject'), creator: arr(x, 'creator'), copyright: loc(x, 'rights') };
|
|
20
|
+
}
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
async function pickHost(host) {
|
|
24
|
+
if (host) return host;
|
|
25
|
+
const open = await runningApps();
|
|
26
|
+
for (const k of ["ps", "ai", "id", "ae"]) if (open[k]) return k;
|
|
27
|
+
throw new Error("Metadata needs one of Photoshop, Illustrator, InDesign or After Effects to be open (it uses their XMP library). Open one and try again.");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function collect(files, folder, recursive, exts) {
|
|
31
|
+
let out = [...(files || [])];
|
|
32
|
+
if (folder) {
|
|
33
|
+
const re = exts?.length ? new RegExp(`\\.(${exts.map((e) => e.replace(/^\./, "")).join("|")})$`, "i") : null;
|
|
34
|
+
out = out.concat(walk(folder, { maxDepth: recursive ? 10 : 0, filter: (f) => !re || re.test(f) }));
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function register(server, { text, errText }) {
|
|
40
|
+
server.registerTool(
|
|
41
|
+
"assets_batch_rename",
|
|
42
|
+
{
|
|
43
|
+
title: "Batch rename files",
|
|
44
|
+
description:
|
|
45
|
+
"Rename many files using a pattern, like Adobe Bridge's Batch Rename. Tokens: {name} original name, " +
|
|
46
|
+
"{n} counter (pad with {n:3}), {ext} extension, {parent} folder name, {date} YYYY-MM-DD (file modified), " +
|
|
47
|
+
"plus find/replace (regex). dry_run (default true) shows the before/after list; set false to rename. " +
|
|
48
|
+
"Never overwrites an existing file.",
|
|
49
|
+
inputSchema: {
|
|
50
|
+
folder: z.string().optional(),
|
|
51
|
+
files: z.array(z.string()).optional(),
|
|
52
|
+
extensions: z.array(z.string()).optional().describe("Only these extensions, e.g. ['jpg','png']."),
|
|
53
|
+
recursive: z.boolean().optional(),
|
|
54
|
+
pattern: z.string().optional().describe("New name pattern, default '{name}'. Example: 'XMAS26_{parent}_{n:3}'."),
|
|
55
|
+
find: z.string().optional().describe("Regex to find in the name (applied before the pattern)."),
|
|
56
|
+
replace: z.string().optional(),
|
|
57
|
+
case: z.enum(["keep", "lower", "upper"]).optional(),
|
|
58
|
+
start_number: z.number().int().optional(),
|
|
59
|
+
dry_run: z.boolean().optional(),
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
async (a) => {
|
|
63
|
+
try {
|
|
64
|
+
const list = collect(a.files, a.folder, a.recursive, a.extensions).sort((x, y) => x.localeCompare(y, undefined, { numeric: true }));
|
|
65
|
+
if (!list.length) throw new Error("No files matched.");
|
|
66
|
+
let n = a.start_number ?? 1;
|
|
67
|
+
const re = a.find ? new RegExp(a.find, "g") : null;
|
|
68
|
+
const plan = list.map((f) => {
|
|
69
|
+
const ext = path.extname(f), dir = path.dirname(f);
|
|
70
|
+
let name = path.basename(f, ext);
|
|
71
|
+
if (re) name = name.replace(re, a.replace ?? "");
|
|
72
|
+
const st = fs.statSync(f);
|
|
73
|
+
let out = (a.pattern || "{name}")
|
|
74
|
+
.replace(/\{name\}/g, name)
|
|
75
|
+
.replace(/\{parent\}/g, path.basename(dir))
|
|
76
|
+
.replace(/\{date\}/g, st.mtime.toISOString().slice(0, 10))
|
|
77
|
+
.replace(/\{ext\}/g, ext.slice(1))
|
|
78
|
+
.replace(/\{n(?::(\d+))?\}/g, (_, w) => String(n).padStart(Number(w || 0), "0"));
|
|
79
|
+
n++;
|
|
80
|
+
if (a.case === "lower") out = out.toLowerCase();
|
|
81
|
+
if (a.case === "upper") out = out.toUpperCase();
|
|
82
|
+
out = out.replace(/[\\/:*?"<>|]/g, "_");
|
|
83
|
+
return { from: f, to: path.join(dir, out + ext.toLowerCase()) };
|
|
84
|
+
});
|
|
85
|
+
const targets = new Set(), problems = [];
|
|
86
|
+
for (const p of plan) {
|
|
87
|
+
const key = p.to.toLowerCase();
|
|
88
|
+
if (targets.has(key)) problems.push(`Two files would both become ${path.basename(p.to)}`);
|
|
89
|
+
targets.add(key);
|
|
90
|
+
if (p.to.toLowerCase() !== p.from.toLowerCase() && fs.existsSync(p.to) && !plan.some((q) => q.from.toLowerCase() === key))
|
|
91
|
+
problems.push(`${path.basename(p.to)} already exists`);
|
|
92
|
+
}
|
|
93
|
+
const changes = plan.filter((p) => p.from !== p.to);
|
|
94
|
+
if (a.dry_run !== false || problems.length)
|
|
95
|
+
return text({ dry_run: true, files: plan.length, would_rename: changes.length, problems,
|
|
96
|
+
preview: changes.slice(0, 100).map((p) => `${path.basename(p.from)} → ${path.basename(p.to)}`),
|
|
97
|
+
note: problems.length ? "Fix the problems before renaming." : "Run again with dry_run: false to rename." });
|
|
98
|
+
// two-phase rename so swaps/collisions inside the batch are safe
|
|
99
|
+
const temps = changes.map((p, i) => {
|
|
100
|
+
const t = path.join(path.dirname(p.from), `.__rename_${process.pid}_${i}`);
|
|
101
|
+
fs.renameSync(p.from, t);
|
|
102
|
+
return { ...p, t };
|
|
103
|
+
});
|
|
104
|
+
for (const p of temps) fs.renameSync(p.t, p.to);
|
|
105
|
+
return text({ renamed: temps.length });
|
|
106
|
+
} catch (e) {
|
|
107
|
+
return errText(e);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
server.registerTool(
|
|
113
|
+
"assets_read_metadata",
|
|
114
|
+
{
|
|
115
|
+
title: "Read file metadata (XMP)",
|
|
116
|
+
description:
|
|
117
|
+
"Read title, description, keywords, creator and copyright from images, PDFs, videos and Adobe files. " +
|
|
118
|
+
"Uses Adobe's XMP library inside an open Adobe app (Photoshop, Illustrator, InDesign or After Effects).",
|
|
119
|
+
inputSchema: {
|
|
120
|
+
files: z.array(z.string()).optional(),
|
|
121
|
+
folder: z.string().optional(),
|
|
122
|
+
extensions: z.array(z.string()).optional(),
|
|
123
|
+
host: z.enum(["ps", "ai", "id", "ae"]).optional().describe("Which open app to use (default: first one open)."),
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
async (a) => {
|
|
127
|
+
try {
|
|
128
|
+
const list = collect(a.files, a.folder, false, a.extensions).slice(0, 500);
|
|
129
|
+
if (!list.length) throw new Error("No files.");
|
|
130
|
+
const host = await pickHost(a.host);
|
|
131
|
+
const jsx = `${XMP_LIB}
|
|
132
|
+
var fs = ${lit(list.map(esPath))}, out = [];
|
|
133
|
+
for (var i = 0; i < fs.length; i++) { try { out.push(readMeta(new File(fs[i]).fsName)); } catch (e) { out.push({ file: fs[i], error: String(e) }); } }
|
|
134
|
+
return out;`;
|
|
135
|
+
return text({ via: host, files: await runJSX(host, jsx, 300000) });
|
|
136
|
+
} catch (e) {
|
|
137
|
+
return errText(e);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
server.registerTool(
|
|
143
|
+
"assets_write_metadata",
|
|
144
|
+
{
|
|
145
|
+
title: "Write file metadata (XMP)",
|
|
146
|
+
description:
|
|
147
|
+
"Set title, description, keywords (replace or add), creator and copyright on many files at once — " +
|
|
148
|
+
"e.g. tag a campaign's assets. Only fields you pass are changed. Uses an open Adobe app's XMP library. " +
|
|
149
|
+
"Files open in an Adobe app should be closed first.",
|
|
150
|
+
inputSchema: {
|
|
151
|
+
files: z.array(z.string()).optional(),
|
|
152
|
+
folder: z.string().optional(),
|
|
153
|
+
extensions: z.array(z.string()).optional(),
|
|
154
|
+
title: z.string().optional(),
|
|
155
|
+
description: z.string().optional(),
|
|
156
|
+
keywords: z.array(z.string()).optional(),
|
|
157
|
+
keyword_mode: z.enum(["add", "replace"]).optional().describe("Default add."),
|
|
158
|
+
creator: z.string().optional(),
|
|
159
|
+
copyright: z.string().optional(),
|
|
160
|
+
host: z.enum(["ps", "ai", "id", "ae"]).optional(),
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
async (a) => {
|
|
164
|
+
try {
|
|
165
|
+
const list = collect(a.files, a.folder, false, a.extensions).slice(0, 2000);
|
|
166
|
+
if (!list.length) throw new Error("No files.");
|
|
167
|
+
const host = await pickHost(a.host);
|
|
168
|
+
const set = { title: a.title ?? null, description: a.description ?? null, keywords: a.keywords ?? null,
|
|
169
|
+
mode: a.keyword_mode || "add", creator: a.creator ?? null, copyright: a.copyright ?? null };
|
|
170
|
+
const jsx = `${XMP_LIB}
|
|
171
|
+
var fs = ${lit(list.map(esPath))}, S = ${lit(set)}, ok = 0, failed = [];
|
|
172
|
+
for (var i = 0; i < fs.length; i++) {
|
|
173
|
+
try {
|
|
174
|
+
var xf = new XMPFile(new File(fs[i]).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE), x = xf.getXMP() || new XMPMeta();
|
|
175
|
+
if (S.title !== null) x.setLocalizedText(NS, 'title', null, 'x-default', S.title);
|
|
176
|
+
if (S.description !== null) x.setLocalizedText(NS, 'description', null, 'x-default', S.description);
|
|
177
|
+
if (S.copyright !== null) x.setLocalizedText(NS, 'rights', null, 'x-default', S.copyright);
|
|
178
|
+
if (S.creator !== null) { x.deleteProperty(NS, 'creator'); x.appendArrayItem(NS, 'creator', S.creator, 0, XMPConst.ARRAY_IS_ORDERED); }
|
|
179
|
+
if (S.keywords !== null) {
|
|
180
|
+
var have = {}; if (S.mode === 'replace') x.deleteProperty(NS, 'subject'); else { var cur = arr(x, 'subject'); for (var c = 0; c < cur.length; c++) have[cur[c]] = 1; }
|
|
181
|
+
for (var k = 0; k < S.keywords.length; k++) if (!have[S.keywords[k]]) { x.appendArrayItem(NS, 'subject', S.keywords[k], 0, XMPConst.ARRAY_IS_UNORDERED); have[S.keywords[k]] = 1; }
|
|
182
|
+
}
|
|
183
|
+
if (xf.canPutXMP(x)) { xf.putXMP(x); xf.closeFile(XMPConst.CLOSE_UPDATE_SAFELY); ok++; }
|
|
184
|
+
else { xf.closeFile(); failed.push(fs[i] + ': this file type cannot hold XMP'); }
|
|
185
|
+
} catch (e) { failed.push(fs[i] + ': ' + e); }
|
|
186
|
+
}
|
|
187
|
+
return { updated: ok, failed: failed };`;
|
|
188
|
+
return text({ via: host, ...(await runJSX(host, jsx, 600000)) });
|
|
189
|
+
} catch (e) {
|
|
190
|
+
return errText(e);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
}
|