| 1 | package Mac::AppleScript::Glue::Application; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Mac::AppleScript::Glue::Application - an application to send AppleScript to | ||
| 6 | |||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | use Mac::AppleScript::Glue; | ||
| 11 | use Mac::AppleScript::Glue::Application; | ||
| 12 | |||
| 13 | my $finder = new Mac::AppleScript::Glue::Application('Finder'); | ||
| 14 | |||
| 15 | my $version = $finder->version; | ||
| 16 | |||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Objects of this module are used to send events to an application. | ||
| 21 | |||
| 22 | See L<Mac::AppleScript::Glue> for full information on how to use this | ||
| 23 | package. | ||
| 24 | |||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | ###################################################################### | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | use base qw(Mac::AppleScript::Glue::Object); | ||
| 34 | |||
| 35 | ###################################################################### | ||
| 36 | ###################################################################### | ||
| 37 | # methods | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | =over 4 | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | ###################################################################### | ||
| 46 | |||
| 47 | =item new($app_name [, $machine ]) | ||
| 48 | |||
| 49 | Creates a new application object for the C<$app_name> application. | ||
| 50 | |||
| 51 | If C<$machine> is specified, the specified computer will be sent | ||
| 52 | events instead of the local machine. This uses Apple Remote Events, | ||
| 53 | which you'll have to have enabled on the remote machine before events | ||
| 54 | can be sent to it. (Look in the B<Sharing> system preference or | ||
| 55 | control panel on the remote machine.) | ||
| 56 | |||
| 57 | NOTE: Documentation on remote events is a little vague; it seems that | ||
| 58 | you specify the remote machine as a URL like C<< eppc://<host> >>, | ||
| 59 | where C<< <host> >> is either a hostname or IP address. "EPPC" stands | ||
| 60 | for Event Program to Program Communication, apparently. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | # implemented via $AUTOLOAD | ||
| 65 | |||
| 66 | ###################################################################### | ||
| 67 | |||
| 68 | sub _init { | ||
| 69 | my ($self, $args) = @_; | ||
| 70 | |||
| 71 | $self->{_app_name} = shift @$args; | ||
| 72 | $self->{_machine} = shift @$args; | ||
| 73 | |||
| 74 | return $self->SUPER::_init($args); | ||
| 75 | } | ||
| 76 | |||
| 77 | ###################################################################### | ||
| 78 | |||
| 79 | =item app_name | ||
| 80 | |||
| 81 | Returns the name of the application connected to this object. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | # implemented via $AUTOLOAD | ||
| 86 | |||
| 87 | ###################################################################### | ||
| 88 | |||
| 89 | =item ref | ||
| 90 | |||
| 91 | Returns the AppleScript object reference for this application. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub ref { | ||
| 96 | my ($self) = @_; | ||
| 97 | |||
| 98 | my $ref = 'application "' . $self->app_name . '"'; | ||
| 99 | |||
| 100 | if ($self->machine) { | ||
| 101 | $ref .= ' of machine "' . $self->machine . '"'; | ||
| 102 | } | ||
| 103 | |||
| 104 | return $ref; | ||
| 105 | } | ||
| 106 | |||
| 107 | ###################################################################### | ||
| 108 | |||
| 109 | =item run(@script) | ||
| 110 | |||
| 111 | Sends a user-specified AppleScript to this application. The script | ||
| 112 | can be a simple one-liner or a multi-line script; in either case, | ||
| 113 | script lines should not contain newlines. If the script returns a | ||
| 114 | value, it will be parsed into Perl data structures. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub run { | ||
| 119 | my ($self, @script) = @_; | ||
| 120 | |||
| 121 | return $self->SUPER::run( | ||
| 122 | "tell " . $self->ref, | ||
| 123 | @script, | ||
| 124 | "end tell", | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | |||
| 128 | ###################################################################### | ||
| 129 | |||
| 130 | =item objref($ref) | ||
| 131 | |||
| 132 | =item objref($class => $string) | ||
| 133 | |||
| 134 | Creates an object reference to use as a standalone object. If you | ||
| 135 | have a complete object reference (like C<application "Finder">), you | ||
| 136 | can pass that as a single argument: | ||
| 137 | |||
| 138 | $app->objref('application "Finder"'); | ||
| 139 | |||
| 140 | You can also have C<objref()> make up the object reference for you out | ||
| 141 | of a class and string: | ||
| 142 | |||
| 143 | $app->objref(application => 'Finder'); | ||
| 144 | |||
| 145 | This is simply a shortcut to calling Mac::AppleScript::Glue::Object's | ||
| 146 | C<new> method. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub objref { | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | my $ref; | ||
| 154 | |||
| 155 | if (@_ > 1) { | ||
| 156 | my ($class, $string) = (shift, shift); | ||
| 157 | $class =~ s/_/ /g; | ||
| 158 | $ref = "$class \"$string\""; | ||
| 159 | |||
| 160 | } else { | ||
| 161 | $ref = shift; | ||
| 162 | } | ||
| 163 | |||
| 164 | return new Mac::AppleScript::Glue::Object( | ||
| 165 | ref => $ref, | ||
| 166 | app => $self, | ||
| 167 | ); | ||
| 168 | } | ||
| 169 | |||
| 170 | ###################################################################### | ||
| 171 | |||
| 172 | =back | ||
| 173 | |||
| 174 | =head1 SEE ALSO | ||
| 175 | |||
| 176 | L<Mac::AppleScript::Glue> | ||
| 177 | |||
| 178 | L<Mac::AppleScript::Glue::Application> | ||
| 179 | |||
| 180 | |||
| 181 | =head1 AUTHOR | ||
| 182 | |||
| 183 | John Labovitz E<lt>johnl@johnlabovitz.comE<gt> | ||
| 184 | |||
| 185 | |||
| 186 | =head1 COPYRIGHT | ||
| 187 | |||
| 188 | Copyright (c) 2002 John Labovitz. All rights reserved. This program is | ||
| 189 | free software; you can redistribute it and/or modify it under the same | ||
| 190 | terms as Perl itself. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | 1; |