Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Perl / October 2004



Tip: Looking for answers? Try searching our database.

Setting the name of screenshots (etc)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rich Morin - 25 Oct 2004 07:16 GMT
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...
Signature

email: rdm@cfcl.com; phone: +1 650-873-7841
http://www.cfcl.com        - Canta Forda Computer Laboratory
http://www.cfcl.com/Meta   - The FreeBSD Browser, Meta Project, etc.
http://www.ptf.com/dossier - Prime Time Freeware's DOSSIER series

Peter N Lewis - 25 Oct 2004 08:09 GMT
>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
[quoted text clipped - 9 lines]
>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/>
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.