argv has asked for the wisdom of the Perl Monks concerning the following question:

I use photoshop (and bridge) extensively in my photography business, but I'm also a proficient perl programmer, which I use to develop automation scripts that do a large number of batch processes that are beyond the capabilities of the aforementioned apps.

What I'd like to do is marry the two, and write perl scripts that can take advantage of some of the image processing capabilities of Photoshop. Alas, it turns out that PS and Bridge have scripting capabilities that, according to their documentation, allow you to use any scripting language that will send Apple events back to the host application. Presumably, this is to not just allow the Adobe app to invoke the script in the first place, but to allow the script to send events to the application so it can perform any UI features, such as prompting for input, presenting error messages, or what have you. (my assumption)

I see nothing obvious on Mac OSX that provides a perl module or API, and the only thing on cpan.org appears to be Mac::AETE::App, which is vaguely defined as something that reads an "event dictionary" from an application. Before I start treading down a tumultuous road, I'm wondering if anyone out there has done much scripting on a mac using Apple events... or, if you've heard of someone else doing it that you can point me to, so I can see examples that might save me a steeper learning curve.

Replies are listed 'Best First'.
Re: perl sending Apple events?
by BigLug (Chaplain) on Oct 25, 2005 at 23:57 UTC
    Use Mac::Glue .. it creates a perl module that interfaces with apple events for any application you give it.

    From the docs:
    Raw Mac::AppleEvents method

    use Mac::AppleEvents; use Mac::Errors '$MacError'; $evt = AEBuildAppleEvent('aevt', 'odoc', typeApplSignature, 'M +ACS', kAutoGenerateReturnID, kAnyTransactionID, "'----': obj{want:type(prop), from:'null'()," . "form:prop, seld:type(macs)}" ) or die $MacError; $rep = AESend($evt, kAEWaitReply) or die $MacError; AEDisposeDesc($evt); AEDisposeDesc($rep);
    Easier Mac::AppleEvents::Simple method
    use Mac::AppleEvents::Simple; do_event(qw(aevt odoc MACS), "'----': obj{want:type(prop), from:'null'()," . "form:prop, seld:type(macs)}" );
    Cool Mac::Glue method
    use Mac::Glue; my $glue = Mac::Glue->new('Finder'); $glue->open( $glue->prop('System Folder') );

    UPDATE: Please stop upvoting this node .. it's my highest for quite a while and all I did was cut-and-paste the pod! Quit it! Now!
Re: perl sending Apple events?
by BorgCopyeditor (Friar) on Oct 25, 2005 at 22:28 UTC
    Raw Apple Events are not that friendly to work with. You'll probably be better off using AppleScript. There's a command-line utility called osascript that might be the most direct route you could take.

    #!/usr/bin/perl -w
    `osascript -e 'say "All glory to the hypno-toad!" using "Zarvox"'`;

    Quoting can get a bit hairy, and AppleScript has many problems, but it is the most direct way of getting access to UI elements, etc., under OS X.

    BCE
    --Your punctuation skills are insufficient!

      This gets my vote for the 2005 "Best line of AppleScript" prize
Re: perl sending Apple events?
by Skeeve (Parson) on Oct 25, 2005 at 22:24 UTC

    I'm not sure, whether or not, this will help, but maybe osascript (man osascript) is something you could use to couple perl and PS (or any scriptable application).

    osascript can take any Applescript source and can execute it. So if your perl generates applescript, it can manage scriptable applications.

    For example:

    open (OSASCRIPT , '| osascript -'); print OSASCRIPT <<OSASCRIPT; tell application "iTunes" if the player state is playing then pause return "Pause" else play return "Play" end if end tell OSASCRIPT close OSASCRIPT;


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: perl sending Apple events?
by Moron (Curate) on Oct 26, 2005 at 12:18 UTC
    It seems to me that the Mac issue might be a red herring...so I present the case for an alternative approach:

    Neither Photoshop nor Perl are dependent on this platform. Furthermore, there is a mammoth open source competitor to photoshop, called The Gimp (Gnu image manipulation program). If you look at the list of sources, these include a Gtk+ toolkit. So even if the ready-made CPAN modules (see CPAN:Gimp) miss something, you probably can hand-roll anything you need via CPAN:Gtk+. The Gimp can read all the formats Photoshop is capable of creating (but not vice versa for the Gimp internal formats).

    Update: Gtk stands for Gimp Toolkit and is widely used (directly or indirectly) for about all graphics manipulation in the open source community.

    -M

    Free your mind

      It's not a red herring .. the OP asked about Mac.

      Nor is the GIMP any use to graphics professionals. It may be of use to interface designers, but without a calibrated CMYK mode, it's not much use.

      This doesn't go near the fact that it's clunky and needs a major overhaul before it's going to rival Photoshop for usability.

        I know it's not related to the poster's question, but I take issue with your statement that gimp is useless for graphics professionals. The studio I work for uses FilmGimp (for 16bit per channel color) extensively for processing frames for cinema. CMYK isn't an issue if it doesn't go to print.
        To clarify my meaning, none of the software under discussion has platform dependency - there is no compunction to seek a specifically Mac-based solution so the Mac itself is a red herring in this context that the OP and some subsequent posters may or may not have been chasing.

        We are talking about using perl to do the manipulation, not the gimp or photoshop user interface. By the same token, the issue of presentation on the screen or paper (RGB is the colour scheme for a screen versus CMYK the colour scheme for inks in a colour printer) is also irrelevant to the op - such "calibration" only has meaning in tuning the relevant colour scales for your printing ink, not for manipulation from perl of the foregoing image which may or may not have gone through CYMK calibration phase of the process (as you please).

        Your suggestion that the Gimp does not support CMYK is in this context misleading: Although the Gimp's own interface may be lacking in CYMK manipulation, as I already made abundantly clear, such gaps can be filled in using cpan gtk+ directly.

        The OP can still use photoshop to test the final product of his Perl/Gtk+ implementation if he finds the Gimp interface inferior - I never suggested he use Gimp to replace photoshop entirely.

        Finally, if the requirement were to specifically relate to programmed manipulation of CYMK, perl/Gtk+ could achieve that and as far as I know, there is no obvious way with Photoshop - and that's no CYMK herring.

        -M

        Free your mind

[Off Topic] Re: perl sending Apple events?
by duckyd (Hermit) on Oct 26, 2005 at 14:34 UTC
    A little off topic, bit possibly of interest to those reading this thread, CamelBones (a Perl Cocoa framework) is pretty neat (see also this MacSlash article)