Skip to content

Commit 2df71b1

Browse files
authored
Merge pull request #1694 from emmertex/feat/naming-metadata-tokens
#1584: file-name tokens for every stored metadata field
2 parents 8bfc91c + a1bfc63 commit 2df71b1

26 files changed

Lines changed: 393 additions & 12 deletions

File tree

‎crates/engine/src/cmd/library.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2806,7 +2806,7 @@ pub fn specs() -> Vec<CommandSpec> {
28062806
"Import Photos",
28072807
["File"],
28082808
Some("Cmd+Shift+I"),
2809-
"{paths: [file or folder (recursive)], mode?: add|copy|move (add = reference the files in place; copy = into the library's Originals/YYYY/YYYY-MM-DD/; move = as copy, then each original and its XMP sidecars are removed from the source, only after the copy is verified (a hard link on the same volume, else copied, synced and compared byte for byte) and its catalog record is saved; failed, duplicate and unchecked files keep their sources; a taken name gets -1, -2…; undo removes the photos from the library but leaves the files at the destination), destination?: folder for copies / moves, organize?: date (YYYY/YYYY-MM-DD) | month (YYYY/YYYY-MM) | flat | a folder template, e.g. `{date:%Y}/{date:%Y%m%d}` → 2026/20260114 (the template's `/` make the folders, each level expanded with the rename tokens and made a safe folder name: never outside the destination; must be relative, no `..`; a level with missing metadata is `unknown`); dated by capture time, else the import time, rename?: file-name template for copies, original extension added (tokens: {name} {num} {seq} {seq:N} {date} {date:%Y%m%d} {folder} {camera} {lens} {iso} {rating} {title} {creator} {ext}; photo.renameTokens explains each; blank = keep names), renameStart?: 1 (must fit this platform; later files are reported failed if the sequence runs out), metadataPreset?: name, dng?: bool (copy raws as DNG; copy only), local?: bool (browsing: the photos stay out of the library, like library.browse; not with move), album?: albumId, albumName?: new album, preset?: presetId, presetProfile?: bool (a preset's camera profile replaces the raw's; default false: each raw keeps its body's camera profile through the presets), presets?: [presetId] (a stack applied in order after `preset`, one History step each; a browsed photo joining the library gets the presets, keywords and metadata preset too, on top of its own culling and edits, and copied or moved its record follows its file), recipe?: [{id: a plain develop control (develop.controls; no white balance, geometry or crop), value}] (a batch-edit recipe: each control is set to its value, clamped to the control's range, on every photo this import adds, after its sidecar, camera default and preset, as one extra History step named Import adjustments; a photo the library already has (a duplicate, one restored from Recently Deleted, one joined from a browsed folder) is not changed; a control a recipe may not name is refused before anything is imported; preset.saveRecipe keeps a recipe for reuse), keywords?: [..], cameraProfile?: auto (default: each body's own camera profile, else the camera's default) | {all: camera profile id} (every raw of that profile's camera) | {perBody: {body (as profiles.bodies lists it): camera profile id}}, for this import only; a raw whose XMP sidecar sets develop settings keeps its own, onDeleted?: restore|skip|fresh (a file that is in Recently Deleted: restore (default) = bring the photo back with its edits, relinked to the file when its own is gone; skip = leave it there and say so, fresh = delete the trashed record and import the file as new), importDuplicates?: bool (false by default = Lightroom's Don't Import Suspected Duplicates: a file whose content the library already has is skipped; true imports it as a new photo; a path already in the library, or a photo in Recently Deleted, is still handled as above)} → {imported, duplicates: [{path, existing, reason, existingDeleted?}], restored?: [photoId], failed, skipped: {unreadable, unsupported} (what the folder walk left out, as library.importPreview counts it; a file named in paths is always tried, so it is never counted here), warnings?: [{path, code: cameraProfile|uprightGeometry|unmappedXmp, profile?, mode?: off|auto|level|vertical|full|guided|unknown, fields?}], moved?: [{from, to, sidecars?}], kept?: [{path, reason}] (move: sources left in place and why), album?}",
2809+
"{paths: [file or folder (recursive)], mode?: add|copy|move (add = reference the files in place; copy = into the library's Originals/YYYY/YYYY-MM-DD/; move = as copy, then each original and its XMP sidecars are removed from the source, only after the copy is verified (a hard link on the same volume, else copied, synced and compared byte for byte) and its catalog record is saved; failed, duplicate and unchecked files keep their sources; a taken name gets -1, -2…; undo removes the photos from the library but leaves the files at the destination), destination?: folder for copies / moves, organize?: date (YYYY/YYYY-MM-DD) | month (YYYY/YYYY-MM) | flat | a folder template, e.g. `{date:%Y}/{date:%Y%m%d}` → 2026/20260114 (the template's `/` make the folders, each level expanded with the rename tokens and made a safe folder name: never outside the destination; must be relative, no `..`; a level with missing metadata is `unknown`); dated by capture time, else the import time, rename?: file-name template for copies, original extension added (tokens: {name} {num} {seq} {seq:N} {date} {date:%Y%m%d} {folder} {camera} {lens} {iso} {rating} {title} {creator} {caption} {headline} {category} {keywords} {copyright} {job_title} {location} {city} {state} {country} {serial} {alt_text} {extended_description} {description_writer} {shutter} {aperture} {focal} {ext}; photo.renameTokens explains each; blank = keep names), renameStart?: 1 (must fit this platform; later files are reported failed if the sequence runs out), metadataPreset?: name, dng?: bool (copy raws as DNG; copy only), local?: bool (browsing: the photos stay out of the library, like library.browse; not with move), album?: albumId, albumName?: new album, preset?: presetId, presetProfile?: bool (a preset's camera profile replaces the raw's; default false: each raw keeps its body's camera profile through the presets), presets?: [presetId] (a stack applied in order after `preset`, one History step each; a browsed photo joining the library gets the presets, keywords and metadata preset too, on top of its own culling and edits, and copied or moved its record follows its file), recipe?: [{id: a plain develop control (develop.controls; no white balance, geometry or crop), value}] (a batch-edit recipe: each control is set to its value, clamped to the control's range, on every photo this import adds, after its sidecar, camera default and preset, as one extra History step named Import adjustments; a photo the library already has (a duplicate, one restored from Recently Deleted, one joined from a browsed folder) is not changed; a control a recipe may not name is refused before anything is imported; preset.saveRecipe keeps a recipe for reuse), keywords?: [..], cameraProfile?: auto (default: each body's own camera profile, else the camera's default) | {all: camera profile id} (every raw of that profile's camera) | {perBody: {body (as profiles.bodies lists it): camera profile id}}, for this import only; a raw whose XMP sidecar sets develop settings keeps its own, onDeleted?: restore|skip|fresh (a file that is in Recently Deleted: restore (default) = bring the photo back with its edits, relinked to the file when its own is gone; skip = leave it there and say so, fresh = delete the trashed record and import the file as new), importDuplicates?: bool (false by default = Lightroom's Don't Import Suspected Duplicates: a file whose content the library already has is skipped; true imports it as a new photo; a path already in the library, or a photo in Recently Deleted, is still handled as above)} → {imported, duplicates: [{path, existing, reason, existingDeleted?}], restored?: [photoId], failed, skipped: {unreadable, unsupported} (what the folder walk left out, as library.importPreview counts it; a file named in paths is always tried, so it is never counted here), warnings?: [{path, code: cameraProfile|uprightGeometry|unmappedXmp, profile?, mode?: off|auto|level|vertical|full|guided|unknown, fields?}], moved?: [{from, to, sidecars?}], kept?: [{path, reason}] (move: sources left in place and why), album?}",
28102810
always,
28112811
|s, p| {
28122812
let req = import_params(s, p)?;

‎crates/engine/src/cmd/manage.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ pub fn specs() -> Vec<CommandSpec> {
294294
cmd!(query "photo.renameTokens", "Rename Template Tags", [], None, "{} → {tokens: [{tag, aliases, meaning, example}], dateDirectives: [{directive, meaning}], notes: [..], sample}: the file-name template tags shared by photo.rename, library.import (rename) and app.export (naming); examples are for a sample photo", always, |_, _| {
295295
Ok(crate::rename::token_help_json())
296296
}),
297-
cmd!(query "photo.renamePreview", "Rename Preview", [], None, "{template: e.g. `{date}_{name}`, `Trip-{seq:3}` (tokens: {name} {num} {seq} {seq:N} {date} {date:%Y%m%d} {folder} {camera} {lens} {iso} {rating} {title} {creator} {ext} {text:Label}; photo.renameTokens explains each), start?: first sequence number (1), texts?: {label: text} (what each {text:Label} becomes), ids?}: renames the files on disk (sidecars too, never overwriting: collisions get -1, -2…); undoable", has_selection, |s, p| {
297+
cmd!(query "photo.renamePreview", "Rename Preview", [], None, "{template: e.g. `{date}_{name}`, `Trip-{seq:3}` (tokens: {name} {num} {seq} {seq:N} {date} {date:%Y%m%d} {folder} {camera} {lens} {iso} {rating} {title} {creator} {caption} {headline} {category} {keywords} {copyright} {job_title} {location} {city} {state} {country} {serial} {alt_text} {extended_description} {description_writer} {shutter} {aperture} {focal} {ext} {text:Label}; photo.renameTokens explains each), start?: first sequence number (1), texts?: {label: text} (what each {text:Label} becomes), ids?}: renames the files on disk (sidecars too, never overwriting: collisions get -1, -2…); undoable", has_selection, |s, p| {
298298
let (ids, template, start) = rename_args(s, p, "photo.renamePreview", false)?;
299299
Ok(serde_json::to_value(s.plan_rename(&ids, &template, start)).unwrap_or_default())
300300
}),
@@ -303,7 +303,7 @@ pub fn specs() -> Vec<CommandSpec> {
303303
"Rename Photos",
304304
[],
305305
None,
306-
"{template: e.g. `{date}_{name}`, `Trip-{seq:3}` (tokens: {name} {num} {seq} {seq:N} {date} {date:%Y%m%d} {folder} {camera} {lens} {iso} {rating} {title} {creator} {ext} {text:Label}; photo.renameTokens explains each), start?: first sequence number (1), texts?: {label: text} (what each {text:Label} becomes), ids?}: renames the files on disk (sidecars too, never overwriting: collisions get -1, -2…); undoable",
306+
"{template: e.g. `{date}_{name}`, `Trip-{seq:3}` (tokens: {name} {num} {seq} {seq:N} {date} {date:%Y%m%d} {folder} {camera} {lens} {iso} {rating} {title} {creator} {caption} {headline} {category} {keywords} {copyright} {job_title} {location} {city} {state} {country} {serial} {alt_text} {extended_description} {description_writer} {shutter} {aperture} {focal} {ext} {text:Label}; photo.renameTokens explains each), start?: first sequence number (1), texts?: {label: text} (what each {text:Label} becomes), ids?}: renames the files on disk (sidecars too, never overwriting: collisions get -1, -2…); undoable",
307307
has_selection,
308308
|s, p| {
309309
let (ids, template, start) = rename_args(s, p, "photo.rename", true)?;

‎crates/engine/src/rename.rs‎

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
//! without extension), `{num}` (the number at the end of the name: `IMG_0042` → `0042`), `{seq}` /
66
//! `{seq:3}` (sequence number, zero-padded to 3 digits), `{date}` / `{date:%Y%m%d}` (capture date;
77
//! `%Y %y %m %B %b %d %H %M %S`), `{folder}` (the original's folder name), `{camera}`, `{lens}`, `{iso}`,
8-
//! `{rating}`, `{title}`, `{creator}`, `{ext}` (extension, without the dot). Unknown tokens stay as
9-
//! typed. Characters that aren't allowed in file names become `-`. The original extension is always
10-
//! kept.
8+
//! `{rating}`, `{title}`, `{creator}`, `{ext}` (extension, without the dot), and the metadata fields
9+
//! the library stores (`{caption}`, `{headline}`, `{category}`, `{keywords}`, `{copyright}`,
10+
//! `{job_title}`, `{location}`, `{city}`, `{state}`, `{country}`, `{serial}`, `{alt_text}`,
11+
//! `{extended_description}`, `{description_writer}`, `{shutter}`, `{aperture}`, `{focal}`; issue
12+
//! #1584: freely usable metadata fields as file-name tags, for markers other apps don't use).
13+
//! Unknown tokens stay as typed. Characters that aren't allowed in file names become `-`. The
14+
//! original extension is always kept.
1115
//!
1216
//! Safety:
1317
//! - a target that exists on disk, or that another photo in the batch gets, receives a `-1`, `-2`…
@@ -139,6 +143,23 @@ pub const TOKENS: &[TokenHelp] = &[
139143
TokenHelp { tag: "{rating}", aliases: &[], meaning: "Star rating (0–5)" },
140144
TokenHelp { tag: "{title}", aliases: &[], meaning: "Title metadata" },
141145
TokenHelp { tag: "{creator}", aliases: &[], meaning: "Creator metadata" },
146+
TokenHelp { tag: "{caption}", aliases: &[], meaning: "Caption metadata" },
147+
TokenHelp { tag: "{headline}", aliases: &[], meaning: "Headline metadata" },
148+
TokenHelp { tag: "{category}", aliases: &[], meaning: "Category metadata" },
149+
TokenHelp { tag: "{keywords}", aliases: &[], meaning: "Keywords, separated by spaces" },
150+
TokenHelp { tag: "{copyright}", aliases: &[], meaning: "Copyright notice metadata" },
151+
TokenHelp { tag: "{job_title}", aliases: &[], meaning: "Creator's job title" },
152+
TokenHelp { tag: "{location}", aliases: &[], meaning: "Location (sublocation)" },
153+
TokenHelp { tag: "{city}", aliases: &[], meaning: "City" },
154+
TokenHelp { tag: "{state}", aliases: &[], meaning: "State or province" },
155+
TokenHelp { tag: "{country}", aliases: &[], meaning: "Country" },
156+
TokenHelp { tag: "{serial}", aliases: &[], meaning: "Camera body serial number" },
157+
TokenHelp { tag: "{alt_text}", aliases: &[], meaning: "Alt text (accessibility)" },
158+
TokenHelp { tag: "{extended_description}", aliases: &[], meaning: "Long description (accessibility)" },
159+
TokenHelp { tag: "{description_writer}", aliases: &[], meaning: "Caption writer metadata" },
160+
TokenHelp { tag: "{shutter}", aliases: &[], meaning: "Shutter speed (1/250)" },
161+
TokenHelp { tag: "{aperture}", aliases: &[], meaning: "f-number, number only (8, 2.8)" },
162+
TokenHelp { tag: "{focal}", aliases: &[], meaning: "Focal length (24mm)" },
142163
TokenHelp { tag: "{ext}", aliases: &[], meaning: "Original extension without the dot" },
143164
TokenHelp {
144165
tag: "{text:Couple initials}",
@@ -267,6 +288,23 @@ pub fn sample_photo() -> Photo {
267288
p.meta.iso = Some(400);
268289
p.meta.title = "Harbour".into();
269290
p.meta.creator = "Ann Lee".into();
291+
p.meta.caption = "The ferry leaves at dawn.".into();
292+
p.meta.headline = "First light on the pier".into();
293+
p.meta.category = "TRA".into();
294+
p.meta.keywords = vec!["harbour".into(), "dawn".into()];
295+
p.meta.copyright = "© 2026 Ann Lee".into();
296+
p.meta.contact.job_title = "Photographer".into();
297+
p.meta.location = "South Pier".into();
298+
p.meta.city = "Fleetwood".into();
299+
p.meta.state = "Lancashire".into();
300+
p.meta.country = "England".into();
301+
p.meta.camera_serial = "0123456789".into();
302+
p.meta.alt_text = "A fishing boat at the pier at dawn".into();
303+
p.meta.extended_description = "A red-hulled fishing boat moored at South Pier, Fleetwood, in first light.".into();
304+
p.meta.description_writer = "A. Lee".into();
305+
p.meta.shutter = "1/250".into();
306+
p.meta.aperture = Some(8.0);
307+
p.meta.focal_mm = Some(24.0);
270308
p
271309
}
272310

@@ -431,6 +469,24 @@ pub fn expand_tokens_with(
431469
"rating" => p.rating.to_string(),
432470
"title" => p.meta.title.clone(),
433471
"creator" => p.meta.creator.clone(),
472+
"caption" => p.meta.caption.clone(),
473+
"headline" => p.meta.headline.clone(),
474+
"category" => p.meta.category.clone(),
475+
"keywords" => p.meta.keywords.join(" "),
476+
"copyright" => p.meta.copyright.clone(),
477+
"job_title" => p.meta.contact.job_title.clone(),
478+
"location" => p.meta.location.clone(),
479+
"city" => p.meta.city.clone(),
480+
"state" => p.meta.state.clone(),
481+
"country" => p.meta.country.clone(),
482+
"serial" => p.meta.camera_serial.clone(),
483+
"alt_text" => p.meta.alt_text.clone(),
484+
"extended_description" => p.meta.extended_description.clone(),
485+
"description_writer" => p.meta.description_writer.clone(),
486+
"shutter" => p.meta.shutter.trim().to_string(),
487+
// the number only: "f/2.8" would lose its slash in a file name
488+
"aperture" => p.meta.aperture.filter(|a| a.is_finite() && *a > 0.0).map(|a| format!("{a:.1}").replace(".0", "")).unwrap_or_default(),
489+
"focal" => p.meta.focal_mm.filter(|f| f.is_finite() && *f > 0.0).map(|f| format!("{f:.0}mm")).unwrap_or_default(),
434490
"ext" => ext.to_string(),
435491
// (a text not filled in by `fill_texts`: nothing)
436492
"text" if arg.is_some() => String::new(),
@@ -2269,6 +2325,35 @@ pub(crate) mod tests {
22692325
assert_eq!(expand_tokens("{seq}|{seq:2}", &p, 7, 3), "007|07");
22702326
}
22712327

2328+
/// The metadata fields the library stores are file-name tags (issue #1584): every new token
2329+
/// expands from its field, the technical ones name files (no slash), and a missing field
2330+
/// leaves an empty gap.
2331+
#[test]
2332+
fn metadata_tokens_name_files_from_the_stored_fields() {
2333+
let p = sample_photo();
2334+
assert_eq!(expand_tokens("{caption}|{headline}|{category}", &p, 1, 1), "The ferry leaves at dawn.|First light on the pier|TRA");
2335+
assert_eq!(expand_tokens("{keywords}", &p, 1, 1), "harbour dawn");
2336+
assert_eq!(expand_tokens("{copyright}|{job_title}", &p, 1, 1), "© 2026 Ann Lee|Photographer");
2337+
assert_eq!(expand_tokens("{location}|{city}|{state}|{country}", &p, 1, 1), "South Pier|Fleetwood|Lancashire|England");
2338+
assert_eq!(expand_tokens("{serial}", &p, 1, 1), "0123456789");
2339+
assert_eq!(
2340+
expand_tokens("{alt_text}|{extended_description}|{description_writer}", &p, 1, 1),
2341+
"A fishing boat at the pier at dawn|A red-hulled fishing boat moored at South Pier, Fleetwood, in first light.|A. Lee"
2342+
);
2343+
// the technical tokens keep file names usable (an f/ would become a - in every name)
2344+
assert_eq!(expand_tokens("{shutter}|{aperture}|{focal}", &p, 1, 1), "1/250|8|24mm");
2345+
let mut q = sample_photo();
2346+
q.meta.aperture = Some(2.8);
2347+
q.meta.focal_mm = Some(24.5);
2348+
assert_eq!(expand_tokens("{aperture}|{focal}", &q, 1, 1), "2.8|24mm");
2349+
assert_eq!(expand("SKU-{keywords}", &q, 1), "SKU-harbour dawn.CR3");
2350+
// a field the photo doesn't have leaves an empty gap, and empty keeps the name
2351+
let mut empty = sample_photo();
2352+
empty.meta = Default::default();
2353+
assert_eq!(expand_tokens("{caption}|{serial}|{aperture}|{focal}", &empty, 1, 1), "|||");
2354+
assert_eq!(expand("{caption}", &empty, 1), "IMG_0042.CR3");
2355+
}
2356+
22722357
/// `{seq:N}` pads to at most 9 digits whatever N a template asks for (a template can come from a preset file, a
22732358
/// script or MCP): at, just below and just above the cap, a caller width past it, and widths that would need
22742359
/// gigabytes. The huge widths come last, so an uncapped width fails on the small ones before allocating.

0 commit comments

Comments
 (0)