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 / January 2007



Tip: Looking for answers? Try searching our database.

How to gather some basic info (front-most app and title)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim Bunce - 16 Nov 2006 22:32 GMT
Hello.

I'm hoping someone can point me in the right direction to get me started...

I want to write a perl script that can gather the following simple info:

    Has the screen blanker blanked the screen?
    What's the front-most application?
    What's it's window title?

I've been using a mac for a couple of years now but never needed to do
any mac-specific development. Mac-Glue and Mac-Carbon distributions
look wonderful (thanks Chris!) but somewhat daunting to the OSX novice.

I'm sure it's trivial. Hopefully someone can show me just how trivial ;-)

Tim.

p.s. I'm planning to implement a little script that'll log the info at
regular intervals and, from time to time, dump it into a SQLite db.
Another script will extract and summarize the info - including
determining what 'project' was being worked on - and report summary info
aggregated in various ways.
Tim Bunce - 21 Nov 2006 22:48 GMT
> Hello.
>
[quoted text clipped - 19 lines]
> determining what 'project' was being worked on - and report summary info
> aggregated in various ways.

FYI here's what I've put together so far from fragments of info gathered
from AppleScript docs and web:

    set win_name to ""
    tell application "System Events"
       set app_name to name of the first process whose frontmost is true
       -- is "ScreenSaverEngine" if screen saver is running
       -- is "SecurityAgent" if prompting for a password to unlock
    end tell

    try
       tell application app_name
        set win_count to count of windows
        if (win_count is greater than 0) then
           set win_name to name of window 1
        end if
       end tell
    end try

Tim.
Chris Nandor - 19 Jan 2007 03:08 GMT
Tim, sorry, I missed this post before.  I don't read the list too often
(through an RSS reader, and I've been neglecting most of what's in there
lately!).

Here's some code that will do what your AppleScript does:

  #!/usr/bin/perl
  use warnings;
  use strict;

  use Mac::Glue ':all';

  my $sysevt = new Mac::Glue 'System Events';
  my $app_name = $sysevt->prop(
     name => item => 1,
     application_process => whose(frontmost => equals => 1)
  )->get;

  my $app_glue = new Mac::Glue $app_name;
  my $window_name = $app_glue->prop(name => window => 1)->get;

As you can tell, that's basically what you were already doing.

There's actually a script I wrote called "happening" that was covered a bit
in an O'Reilly Mac OS X Hacks book.  Unfortunately, the Mac::Glue version
will only work for getting the frontmost window for applications where
you've already created a glue for it (with `gluemac` on the command line).  
The script runs in the background and sets my iChat status to my app and its
frontmost window, if I want it to.  Does lots of other similar things too,
like looking at what I am listening to or watching.

  http://dev.macperl.org/files/scripts/happening

Cheers,

--Chris

In article
<20061121224835.GA5239@ip-192-168-1-101.internal.terminal.newtec.be>,

> > Hello.
> >
[quoted text clipped - 40 lines]
>
> Tim.

Signature

Chris Nandor                      pudge@pobox.com    http://pudge.net/
Open Source Technology Group       pudge@ostg.com     http://ostg.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



©2008 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.