This repository was archived by the owner on Nov 4, 2019. It is now read-only.
forked from ghansson/mac-iphoto-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiphoto
668 lines (498 loc) · 13.7 KB
/
iphoto
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#!/usr/bin/perl
use strict;
use warnings;
=head1 NAME
iphoto - a command line shell for iPhoto
=head1 SYNOPSIS
# start the shell
% iphoto
# run a single command
% iphoto merge album1 album2
=head1 DESCRIPTION
THIS IS EXPERIMENTAL SOFTWARE! USE AT YOUR OWN RISK!
IT'S ALSO UNMAINTAINED!
This script provides a shell for iPhoto. Make a backup of your photo
library, or use a test library, to play with this script.
=head1 COMMANDS
These commands interact with iPhoto, and some of them will change your
iPhoto library, including deleting photos forever.
The "show" command makes iPhoto display something, while the "print"
command makes the shell display something. For instance, "show boat"
will tell iPhoto to display the album "boat" just as if you had selected
it from iPhoto's albums pane. The command "print albums" will print on
the terminal (not the printer!) the list of albums.
Although some photos may disappear from albums or the photo library, they
are simply in the Trash library, and you do not delete them until you use
the "empty" command. However, since I am still developing this script,
I have disabled that command. You can still use the command, but it
will not empty the trash. To enable the "empty" command, find the
"empty_trash" subroutine and modify the source according to the comment
in the subroutine.
=over 4
=item debug
turn on extra output
=item empty
empty the trash (you cannot recover these photos).
=item help
display available commands
=item make ALBUM_NAME
make an album named ALBUM_NAME
=item merge ALBUM1 ALBUM2
move everything from ALBUM2 to ALBUM1, and remove ALBUM2
=item move ALBUM
move the selected photos to ALBUM
=item quit
stop the shell
=item print [ album | albums | photo | photos | selection ]
Rrint information about the specified object. What these actually
print changes everytime I look at the source, so you have to try
them to see.
=item remove ALBUM
Remove the album. The photos stay in the photo library.
=item show ALBUM
Tell iPhoto to display ALBUM
=item trash
Put the selected photos in the trash
=item version
Print the shell version
=item view [ import | organize | edit | book ]
Change the view. The default is "organize", and using any unknown
view changes it to "organize".
=back
=head1 SOURCE AVAILABILITY
This source is part of a SourceForge project which always has the
latest sources in CVS, as well as all of the previous releases.
http://sourceforge.net/projects/brian-d-foy/
If, for some reason, I disappear from the world, one of the other
members of the project can shepherd this module appropriately.
=head1 AUTHOR
brian d foy, E<lt>bdfoy@cpan.orgE<gt>
=head1 COPYRIGHT
Copyright 2004, brian d foy, All rights reserved.
You may use this program under the same terms as Perl itself.
=cut
use File::Spec;
use FindBin;
use Mac::Glue qw(:all);
use Text::ParseWords;
use UNIVERSAL qw(isa);
my $command = shift @ARGV;
my $iPhoto = Mac::Glue->new( "iPhoto" );
my $Debug = 1;
my %Commands = (
debug => sub { &debug },
empty => sub { &empty_trash },
'exists' => sub { &album_exists },
help => sub { &help },
keyword => {
list => sub { &keyword_list },
add => sub { &keyword_add },
remove => sub { &keyword_remove },
},
make => sub { &make_albums },
merge => sub { &merge_albums },
move => sub { &move_selection },
# prompt => sub { &set_prompt },
quit => sub { exit },
'print' => {
album => sub { &print_album },
albums => sub { &print_albums },
photo => sub { &print_photo },
photos => sub { &print_photos },
selection => sub { &print_selection },
view => sub { &print_view },
},
reload => sub { &restart_script }, # broken
remove => sub { &remove_album },
# remove => sub { &remove_photo },
'select' => sub { &select },
show => sub { &show_album },
slideshow => sub { &slideshow }, # broken
trash => sub { &trash_selection },
version => sub { &version },
view => sub { &view }, # broken
);
my %Print = (
album => sub { &print_album },
albums => sub { &print_albums },
photo => sub { &print_photo },
photos => sub { &print_photos },
selection => sub { &print_selection },
view => sub { &print_view },
);
if( @ARGV ) { _command( @ARGV ) }
else { _shell() }
sub _shell
{
my $prompt = "iPhoto> ";
while( 1 )
{
print "$prompt";
my $answer = <STDIN>;
chomp( $answer );
my @bits = "ewords('\s+', 0, $answer );
_command( @bits );
}
}
sub _selection
{
my $selection = $iPhoto->prop( "selection" );
unless( defined $selection )
{
_print_warn( "No photos selected." );
return;
}
my $count = $selection->count( each => 'item' );
_print_debug( "Selection has $count items" );
return $selection;
}
# get command
# is it a key?
# are there more arguments?
# NO - is there a null sub? use it
# YES - is the value a hash? repeat
sub _get_subroutine
{
my $hash = \%Commands;
my @commands = ();
while( 1 )
{
if( isa( $hash, 'HASH' ) )
{
if( exists $hash->{ $_[0] } )
{
$hash = $hash->{ $_[0] };
push @commands, shift;
redo;
}
else { last }
}
else { last }
}
_print_debug( "commands are @commands" );
require Data::Dumper;
print Data::Dumper::Dumper( $hash );
return ( $hash, @_ );
}
sub _command
{
my $original = @_;
my( $subroutine, @args ) = _get_subroutine( @_ );
eval { $subroutine->( @args ) };
if( $@ )
{
_print_warn( "error in $command: $@" );
_print_normal( "continuing" );
}
}
sub select
{
my @numbers = @_;
$iPhoto->select( $iPhoto->obj( photo => "every" )
);
}
sub album_exists
{
my $name = shift;
my $album = $iPhoto->obj(
album => whose( name => equals => $name )
);
my $exists = $iPhoto->exists( $album );
print "Album [$name] " , $exists ? '' : 'does not ', "exist",
$exists ? 's' : '', "\n";
$exists;
}
sub view
{
my $next = shift;
my $view = $iPhoto->prop( "view" );
my $current = $view->get;
_print_normal( "Current view is $current" );
$view->set( to => enum( $next ) );
my $now = $view->get;
_print_normal( "View is now $now" );
}
sub version #
{
my $script = File::Spec->catfile( $FindBin::Bin, $FindBin::Script );
_print_normal( "0.10" );
}
sub restart_script
{
require FindBin;
my $script = File::Spec->catfile( $FindBin::Bin, $FindBin::Script );
_print_debug( "Script is $script" ) if $Debug;
{ exec "perl", qq|$script| };
_print_warn( "Could not reload: $!" );
}
sub help #
{
_print_help( sort keys %Commands );
}
sub debug #
{
my $value = lc shift;
_print_help( "debug [on|off]" ) unless( $value =~ m/on|off/i );
if( $value eq 'on' ) { $Debug = 1 }
elsif( $value eq 'off' ) { $Debug = 0 }
_print_normal( "debug is " . ( $Debug ? "on" : "off" ) );
}
sub remove_album #
{
my @albums = @_;
foreach my $name ( @albums )
{
my $album = $iPhoto->obj(
album => whose( name => equals => $name )
);
_print_normal( qq|Removing album "$name"| );
$album->remove;
}
}
sub make_albums #
{
my @albums = @_;
foreach my $album ( @albums )
{
my $exists = album_exists( $album );
if( $exists )
{
_print_normal( "Skipping [$album], album exists" );
return;
}
_print_normal( qq|Creating album "$album"| );
$iPhoto->new_album( name => $album );
}
}
sub merge_albums #
{
my @albums = @_;
my $name = shift @albums;
my $exists = album_exists( $name );
unless( $exists )
{
_print_normal( "Merge command cancelled. " .
"Create album [$name] first" );
return;
}
my $target = $iPhoto->obj(
album => whose( name => equals => $name )
);
_print_debug( "Target album is $name" ) if $Debug;
foreach my $name ( @albums )
{
my $album = $iPhoto->obj(
album => whose( name => equals => $name )
);
my $count = $album->prop( "photos" )->count;
_print_normal( qq|Found $count photos in "$name"| );
for( my $index = $count; $index > 0; $index-- )
{
my $photo = $album->obj( photo => $index );
$iPhoto->add( $photo => to => $target );
}
_print_normal( qq|Removing album "$name"| );
$album->remove;
}
}
sub print_album #
{
my $albums = $iPhoto->prop( "albums" );
my $current = $iPhoto->prop( "current album" );
my $name = $iPhoto->prop( "name" )->get;
my $count = $current->prop( "photos" )->count;
_print_normal( "Album $name has $count photos" );
}
sub print_albums #
{
my $albums = $iPhoto->prop( "albums" );
my $current = $iPhoto->prop( "current album" )->prop( "name" )->get;
my $count = $albums->count;
_print_normal( "Found $count albums" );
for( my $index = 1; $index <= $count; $index++ )
{
my $album = $albums->obj( item => $index );
my $name = $album->prop( "name" )->get;
my $arrow = $name eq $current ? " <---" : '';
_print_normal( $name . $arrow );
}
}
sub print_photo #
{
my $selection = _selection();
return unless defined $selection;
foreach my $photo ( $selection->get )
{
my $new_photo = _new_photo( $photo );
my $title = $new_photo->prop( "title" )->get;
my $comment = $new_photo->prop( "comment" )->get;
my $date = $new_photo->prop( "date" )->get;
my $height = $new_photo->prop( "height" )->get;
my $width = $new_photo->prop( "width" )->get;
my $filename = $new_photo->prop( "image_filename" )->get;
my $path = $new_photo->prop( "image_path" )->get;
my $size = -s $path;
$size =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
$comment ||= "(no comment)";
_print_normal( <<"HERE" );
$title
$date
$width x $height
$size bytes
$comment
HERE
}
}
sub print_photos #
{
my $album = $iPhoto->prop( "current_album" );
my $name = $album->prop( "name" )->get;
my $photos = $album->prop( "photos" );
my $count = $photos->count;
_print_normal( qq|Found $count photos in "$name"| );
for( my $index = 1; $index <= $count; $index++ )
{
my $photo = $album->obj( photo => $index );
my $title = $photo->prop( "title" )->get;
_print_normal( $title );
}
}
sub print_selection #
{
my $selection = _selection();
return unless definded $selection;
foreach my $photo ( $selection->get )
{
my $new_photo = _new_photo( $photo );
my $title = $new_photo->prop( "title" )->get;
_print_normal( $title );
}
}
sub print_view #
{
my $view = $iPhoto->prop( "view" )->get;
_print_normal( "View is $view" );
}
sub show_album #
{
my $name = shift;
my $exists = album_exists( $name );
return unless $exists;
my $album = $iPhoto->obj(
album => whose( name => equals => $name )
);
$iPhoto->select( $album );
}
sub move_selection #
{
my $name = shift;
my $target = $iPhoto->obj(
album => whose( name => equals => $name )
);
my $selection = _selection;
return unless defined $selection;
my $current = $iPhoto->prop( "current album" );
foreach my $photo ( $selection->get )
{
my $new_photo = _new_photo( $photo );
$iPhoto->add( $new_photo => to => $target );
$iPhoto->remove( $new_photo => from => $current );
}
}
sub remove_selection
{
my $selection = _selection;
return unless defined $selection;
my $trash = $iPhoto->prop( "trash album" );
foreach my $photo ( $selection->get )
{
my $new_photo = _new_photo( $photo );
$iPhoto->remove( $new_photo );
}
}
sub empty_trash #
{
my $trash = $iPhoto->prop( "trash album" );
my $count = $trash->prop( "photos" )->count;
_print_normal( "Trash has $count photos" );
_print_normal( "Emptying trash" .
( $Debug ? ' (not really, debugging)' : '' ) );
print "I won't empty the trash because you did not modify the source\n";
#uncomment the next line line to activate this feature.
#$iPhoto->empty_trash unless $Debug;
}
sub trash_selection
{
my $selection = _selection;
return unless defined $selection;
my $count = $selection->count( each => 'item' );
_print_normal( "Selection has $count photos" );
my $trash = $iPhoto->prop( "trash album" );
foreach my $photo ( map { _get_photo_from_selection( $_ ) } $selection->get )
{
$iPhoto->move( $photo => from => $trash );
}
}
sub slideshow
{
my $command = shift;
unless( $command =~ m/start|stop/ )
{
_print_warn( "Use: slideshow [start|stop]" );
return;
}
$command = "${command}_slideshow";
$iPhoto->$command;
}
sub keyword
{
my $selection = _selection();
return unless defined $selection;
my $command = shift;
my @keywords = @_;
if( not defined $command or $command eq "list" )
{
foreach my $photo ( $selection->get )
{
my $new_photo = _new_photo( $photo );
my @keywords = $new_photo->prop( "keyword" )->prop( "name" )->get;
my $title = $new_photo->prop( "title" )->get;
local $" = ", ";
_print_normal( "$title: @keywords" );
}
}
elsif( $command eq "add" )
{
my $keyword = $iPhoto->obj(
keyword => whose( name => equals => $keywords[0] )
);
my $name = $keyword->prop( "name" )->get;
_print_normal( "keyword name is $name" );
foreach my $photo ( $selection->get )
{
my $new_photo = _new_photo( $photo );
my $title = $new_photo->prop( "name" )->get;
_print_debug( "Processing photo: $title keyword: $keywords[0]" );
$photo->assign_keyword( string => $keywords[0] );
}
}
elsif( $command eq "remove" )
{
}
}
sub _print { chomp( my @s = @_ ); my $s = shift @s; for (@s){print "$s $_\n"} }
sub _print_help { _print( '...', @_ ) }
sub _print_warn { _print( '!!!', @_ ) }
sub _print_debug { _print( '---', @_ ) }
sub _print_normal { _print( '+++', @_ ) }
# this is a work around
sub _new_photo
{
my $photo = shift;
my $comp = AEGetKeyDesc($photo->{DESC}, keyAEKeyData);
my $doub = AECoerceDesc($comp, typeFloat);
my $id = $doub->get;
AEDisposeDesc $doub;
my $new_photo = $iPhoto->obj( photo => whose(ID => equals => $id) );
return $new_photo;
}