Stupid Mac::Glue Tricks: UI Scripting

| | Comments (0)
You can use Apple's UI scripting with Mac::Glue.

Experienced users might notice that you need to set the target app to be frontmost, and the method used here is to use Mac::Glue, which you can't do with non-scriptable apps. This should be coming in a future version of Mac::Glue, but until then, you can use various other methods to set the frontmost app. And of course, this is still quite useful for many scriptable apps, since many scriptable apps don't have full scripting support.

#!/usr/bin/perl
use Mac::Glue;
 
my $app = new Mac::Glue 'System Events';
my $safari = new Mac::Glue 'Safari';
$safari->activate;
 
$app->obj(
    menu_item       => 'Close Tab',
    menu            => 'File',
    menu_bar_item   => 'File',
    menu_bar        => 1,
    process         => 'Safari'
)->click;

Also, you really should have the UI Element Inspector for this. I think it is included with Panther's dev tools, but I am not sure. Regardless, this is the important portion of its output, to create the above script:

<AXApplication: "Safari">
  <AXMenuBar: "">
  <AXMenuBarItem: "File">
   <AXMenu: "File">
    <AXMenuItem: "Close Tab">

use.perl.org

Leave a comment

<pudge/*> (pronounced "PudgeGlob") is thousands of posts over many years by Pudge.

"It is the common fate of the indolent to see their rights become a prey to the active. The condition upon which God hath given liberty to man is eternal vigilance; which condition if he break, servitude is at once the consequence of his crime and the punishment of his guilt."

About this Entry

This page contains a single entry by pudge published on May 16, 2004 9:42 PM.

Handmade Music was the previous entry in this site.

Sunday Thoughts is the next entry in this site.

Find recent content on the main index or look in the archives to find all content.