-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotate.raku
More file actions
executable file
·164 lines (145 loc) · 3.95 KB
/
Copy pathannotate.raku
File metadata and controls
executable file
·164 lines (145 loc) · 3.95 KB
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
#!/usr/bin/env raku
use lib 'lib';
use ExifTool;
sub MAIN(*@args, *%named-args) {
if ! @args {
usage();
return;
}
my $first = @args[0];
if $first eq 'read' {
@args.shift;
handle-read(@args);
}
elsif $first eq 'write' {
@args.shift;
handle-write(@args, %named-args);
}
elsif $first.IO.f {
interactive-mode($first.IO);
}
elsif $first.IO.d {
my @image-extensions = <jpg jpeg png tiff tif webp heic>;
for $first.IO.dir.grep({ .f && .extension && .extension.lc ~~ any(@image-extensions) }).sort -> $file {
interactive-mode($file);
}
}
else {
usage();
}
}
sub handle-read(@args) {
if ! @args {
usage();
return;
}
my $path = @args.shift;
my $file = $path.IO;
unless $file.f {
note "Error: File not found: $path";
exit 1;
}
say "Reading metadata from $path...";
my %meta = read-metadata($file, |@args);
if ( ! %meta ) {
say "No matching metadata found.";
}
else {
for %meta.sort(*.key) -> $pair {
say "{$pair.key}: {$pair.value}";
}
}
}
sub handle-write(@args, %named-args) {
if ! @args {
usage();
return;
}
my $path = @args.shift;
my $file = $path.IO;
unless $file.f {
note "Error: File not found: $path";
exit 1;
}
my %valid-tags = %named-args.grep({ .key !~~ /^ ['help'|'usage'] $/ });
unless %valid-tags {
note "Error: No tags provided to write. Use --TagName=Value";
exit 1;
}
say "Writing metadata to $path...";
try {
write-metadata($file, %valid-tags);
say "Successfully updated $path (backup created if first time).";
CATCH {
default {
note "Failed: $_";
exit 1;
}
}
}
}
sub interactive-mode(IO::Path $file) {
say "\n" ~ ("-" x 40);
say "File: {$file.basename}";
# Open the image in the default viewer/browser cross-platform
my $opener = do given $*DISTRO.name {
when 'macos' { 'open' }
when 'linux' { 'xdg-open' }
when 'mswin32' { 'start' }
default { Nil }
};
if $opener {
if $*DISTRO.name eq 'mswin32' {
# Windows 'start' often needs to be run through a shell
shell "$opener \"\" \"$file\"";
}
else {
run $opener, $file.absolute;
}
}
my %meta;
try {
%meta = read-metadata($file, 'XMP:Description');
CATCH {
default {
note "Skipping {$file.basename}: Could not read metadata (is it an image?)";
return;
}
}
}
if %meta{'XMP:Description'} -> $desc {
say "Current Description: $desc";
}
else {
say "Current Description: [None]";
}
my $prompt-text = "Enter new description (blank to skip, 'q' to quit): ";
my $new-desc = prompt($prompt-text);
if $new-desc eq 'q' {
say "Exiting.";
exit 0;
}
if $new-desc.trim -> $val {
say "Writing new description...";
try {
write-metadata($file, { 'XMP-dc:Description' => $val });
say "Success!";
CATCH {
default {
note "Error writing metadata: $_";
}
}
}
}
else {
say "No changes made.";
}
}
sub usage() {
say "Usage:";
say " annotate.raku <path> -- Interactive mode for image(s) or directory";
say " annotate.raku read <path> [<tags> ...] -- Read specific tags";
say " annotate.raku write <path> --TagName=Value -- Write specific tags";
say "\nNote: For 'write', named arguments must come BEFORE 'write'.";
say "Example: raku annotate.raku --XMP-dc:Description=\"Test\" write img.jpg";
}