At 23:16 -0700 24/10/04, Rich Morin wrote:
I find it a nuisance to have to hunt down the "Picture #.pdf" file
to rename it.  It seems as if there should be a more direct way to
do this.  For instance, I'd be happy to have a Open Dialog come up.

Am I missing something obvious here?  I found a cute hack in "Mac
OS X Unleashed, 2e": build a wrapper script around the command
(/usr/sbin/screencapture).  Unfortunately, this still begs the
question of how to bring up a file dialog box, etc.  Help?

-r

P.S.  A Perlish solution would be Just Fine, but any reasonable
      ideas and comments are acceptable...

My annoyance was not with the lack of naming, but with the fact that screen shots come out at 300 dpi rather than being pixel accurate.


Eventually I gave up and did exactly as described, wrapped screencapture and used our Keyboard Maestro to map the command-shift keys to perl scripts instead.

The perl scripts each look something like this:

****
#!/usr/bin/perl -wT

use lib "/Users/peter/perl"
use warnings;
use strict;
#use diagnostics;

$ENV{PATH} = "/bin:/usr/bin";

use Peter::ScreenCapture;

Peter::ScreenCapture::screen_capture_selection_to_file();
****

And the ScreenCapture.pm follows. A tricky part is detecting a canceled capture (eg pressing the escape key) because screencapture does not give any indication. It uses GraphicConverter to save the file. Currently, it mimics the systems "Picture #.tiff", but if you change the AppleScript code for that to:

tell application "GraphicConverter"
        activate
        try
                save as window 1
        end try
end tell

then GC will prompt for a filename.

Enjoy,
   Peter.


**** package Peter::ScreenCapture;

use 5.006;
use strict;
use warnings;

require Exporter;

our @ISA = qw(Exporter);
our $VERSION = '1.00';

our @EXPORT_OK = qw(
        screen_capture_screen_to_file
        screen_capture_screen_to_clipboard
        screen_capture_selection_to_file
        screen_capture_selection_to_clipboard

        screen_capture_clipboard_to_file
);
our @EXPORT = qw(

);
our %EXPORT_TAGS = ( 'all' => [ @EXPORT_OK ] );

our $screencapture = '/usr/sbin/screencapture';

sub screen_capture_screen_to_file {
        if ( screen_capture_screen_to_clipboard() ) {
                screen_capture_clipboard_to_file();
        }
}

sub screen_capture_screen_to_clipboard {
        pbset( 'Screen Capture' );
        my $result = system( $screencapture, '-c' );
        return pbget() ne 'Screen Capture';
}

sub screen_capture_selection_to_file {
        if ( screen_capture_selection_to_clipboard() ) {
                screen_capture_clipboard_to_file();
        }
}

sub screen_capture_selection_to_clipboard {
        pbset( 'Screen Capture' );
        my $result = system( $screencapture, '-ci' );
        return pbget() ne 'Screen Capture';
}

sub pbset {
        my ( $what ) = @_;
        open( OUT, "|/usr/bin/pbcopy" ) or die;
        print OUT $what;
        close( OUT );
}

sub pbget {
        open( OUT, "/usr/bin/pbpaste|" ) or die;
        local undef $/;
        my $what = <OUT>;
        close( OUT );
        return $what;
}

sub screen_capture_clipboard_to_file {

        require Mac::OSA::Simple;

        my $n = 1;
        $ENV{HOME} =~ m!^(/\w+/\w+)$! or die "Bad Home Directory $ENV{HOME}";
        my $path = "$1/Desktop/Picture.tiff";
        while ( -e $path ) {
                $path =~ s!( ?\d*\.tiff)! $n.tiff! or die;
                $n++;
        }
  if ( open( my $fh, ">", $path ) ) {
    close( $fh );
  }

         Mac::OSA::Simple::applescript( <<EOM );
tell application "GraphicConverter"
        new image from clipboard
        save window 1 in alias (posix file "$path") as TIFF
        close window 1
end tell
EOM
}

1;
****

--
<http://www.stairways.com/>  <http://download.stairways.com/>

Reply via email to