| 1 | package Mac::AppleScript::Glue; | ||
| 2 | $VERSION = '0.03'; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Mac::AppleScript::Glue - allows AppleScript to be written in Perl | ||
| 7 | |||
| 8 | =head1 VERSION | ||
| 9 | |||
| 10 | version 0.06 | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use Mac::AppleScript::Glue; | ||
| 15 | |||
| 16 | my $finder = new Mac::AppleScript::Glue::Application('Finder'); | ||
| 17 | |||
| 18 | $finder->insertion_location->open; | ||
| 19 | |||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This module allows you to write Perl code in object-oriented syntax to | ||
| 24 | control Mac applications. The module does not actually execute Apple | ||
| 25 | Events, but actually translates Perl code to AppleScript code and | ||
| 26 | causes it to be executed. | ||
| 27 | |||
| 28 | |||
| 29 | =head2 Quick start | ||
| 30 | |||
| 31 | The following AppleScript opens the "current" folder in the Finder: | ||
| 32 | |||
| 33 | tell application "Finder" | ||
| 34 | open insertion location | ||
| 35 | end tell | ||
| 36 | |||
| 37 | To do this in Perl, you first include the module: | ||
| 38 | |||
| 39 | use Mac::AppleScript::Glue; | ||
| 40 | |||
| 41 | Then you create an object you'll use to talk to the Finder application: | ||
| 42 | |||
| 43 | my $finder = new Mac::AppleScript::Glue::Application('Finder'); | ||
| 44 | |||
| 45 | And finally you issue the compound statement: | ||
| 46 | |||
| 47 | # open the Finder's "insertion location" in a new window | ||
| 48 | $finder->insertion_location->open; | ||
| 49 | |||
| 50 | You can save the result of a statement: | ||
| 51 | |||
| 52 | # get the Finder's "insertion location" | ||
| 53 | my $loc = $finder->insertion_location; | ||
| 54 | |||
| 55 | And if that result is not a scalar, list, or hash (more on this | ||
| 56 | later), you can use that result as an object to do further work: | ||
| 57 | |||
| 58 | # now open that in a new window | ||
| 59 | $loc->open; | ||
| 60 | |||
| 61 | You can set attributes: | ||
| 62 | |||
| 63 | my $folder = $finder->make_new_folder; | ||
| 64 | |||
| 65 | $folder->set(name => 'My folder'); | ||
| 66 | |||
| 67 | If you need to get a particular element of an object, put the | ||
| 68 | identifier as an argument to the thing that names the element list: | ||
| 69 | |||
| 70 | my $window = $finder->windows(1); | ||
| 71 | |||
| 72 | If you need to specify parameters of a command, use a hash for the | ||
| 73 | parameters, where each key/value pair corresponds to a parameter name | ||
| 74 | and value: | ||
| 75 | |||
| 76 | my $epson_files = $finder->files(whose_name_contains => 'epson'); | ||
| 77 | |||
| 78 | You can specify both identifiers and parameters: | ||
| 79 | |||
| 80 | my $epson_files = $finder->files(1, whose_name_contains => 'epson'); | ||
| 81 | |||
| 82 | Finally, there are cases where you need to create an object reference, | ||
| 83 | rather than obtaining one from an application. To do this, you can | ||
| 84 | use an application object to create a | ||
| 85 | Mac::AppleScript::Glue::Object(3pm) that refers to both the object | ||
| 86 | reference and the application to which that reference should belong: | ||
| 87 | |||
| 88 | my $folder = $finder->objref('folder "Applications"'); | ||
| 89 | |||
| 90 | Then, you can use that as you normally would: | ||
| 91 | |||
| 92 | # open the "Applications" folder | ||
| 93 | $folder->open; | ||
| 94 | |||
| 95 | If you don't need a full-fledged object, you can simply specify a | ||
| 96 | parameter of a I<reference> to a scalar containing a string: | ||
| 97 | |||
| 98 | # open the "Applications" folder | ||
| 99 | $finder->open(\'folder "Applications"'); | ||
| 100 | |||
| 101 | This is also what you should use if you need to pass an AppleScript | ||
| 102 | "constant" along: | ||
| 103 | |||
| 104 | $folder->duplicate(replacing => \'true'); | ||
| 105 | |||
| 106 | But an easier way is to enclose the name of the constant in | ||
| 107 | angle-brackets; the module will know to use it verbatim rather than | ||
| 108 | trying to quote it: | ||
| 109 | |||
| 110 | $folder->duplicate(replacing => '<true>'); | ||
| 111 | |||
| 112 | |||
| 113 | =head2 Return values | ||
| 114 | |||
| 115 | If you issue a statement that will return a value, like C<insertion | ||
| 116 | location>, the result of that statement is always a scalar. The | ||
| 117 | actual contents of this scalar depends on the sort of statement. It | ||
| 118 | will be one of: | ||
| 119 | |||
| 120 | =over 4 | ||
| 121 | |||
| 122 | =item regular scalar | ||
| 123 | |||
| 124 | A number or a string. This is what you'd expect in Perl -- like 1, or | ||
| 125 | "foo". | ||
| 126 | |||
| 127 | =item object reference | ||
| 128 | |||
| 129 | An object reference is a textual string that AppleScript uses to | ||
| 130 | describe both the class and context of a "thing". For example, the | ||
| 131 | C<insertion location> statement might return an object reference of: | ||
| 132 | |||
| 133 | folder "Desktop" of folder "johnl" of folder "Users" of startup | ||
| 134 | disk of application "Finder" | ||
| 135 | |||
| 136 | When Mac::AppleScript::Glue(3pm) sees this sort of reference, it puts the | ||
| 137 | whole object reference string into an object of type | ||
| 138 | Mac::AppleScript::Glue::Object(3pm) (see | ||
| 139 | L<Mac::AppleScript::Glue::Object>). It also stores in this object the | ||
| 140 | application object that created the object. By doing this, the | ||
| 141 | Mac::Application::Glue::Object(3pm) can be used by itself to access or | ||
| 142 | modify other data. | ||
| 143 | |||
| 144 | =item array or hash reference | ||
| 145 | |||
| 146 | If the statement returned an AppleScript "list" or "record", the | ||
| 147 | result will be a Perl array- or hash-reference, respectively. This | ||
| 148 | could contain simple scalars, or a combination of any of the result | ||
| 149 | types; it can also be nested. | ||
| 150 | |||
| 151 | Note that you'll have to dereference the references to use the | ||
| 152 | elements: | ||
| 153 | |||
| 154 | for my $window (@{ $finder->windows }) { | ||
| 155 | ... | ||
| 156 | } | ||
| 157 | |||
| 158 | or: | ||
| 159 | |||
| 160 | my $props = $finder->properties; | ||
| 161 | |||
| 162 | while (my ($key, $val) = each %{$props}) { | ||
| 163 | ... | ||
| 164 | } | ||
| 165 | |||
| 166 | =back | ||
| 167 | |||
| 168 | |||
| 169 | =head2 Notes | ||
| 170 | |||
| 171 | For multi-word AppleScript terms like C<insertion location>, use the | ||
| 172 | underscore character (_) in place of each space character. | ||
| 173 | |||
| 174 | You generally need to reverse the parts of a statement when | ||
| 175 | translating AppleScript to Perl. In AppleScript, C<open insertion | ||
| 176 | location> really sends the "open" message to the object represented by | ||
| 177 | "insertion location". This maps to the Perl syntax | ||
| 178 | C<< insertion_location->open() >>. | ||
| 179 | |||
| 180 | Unlike Perl, AppleScript makes a distinction between booleans and | ||
| 181 | numbers -- you can't intermix them. So if an AppleScript method wants | ||
| 182 | a boolean as a parameter, you I<must> use the AppleScript constants | ||
| 183 | B<true> or B<false>. You can do this by enclosing the string with | ||
| 184 | angle-brackets (C<< <true> >>) or passing a reference to a string | ||
| 185 | containing the constant (C<\'true'>). | ||
| 186 | |||
| 187 | |||
| 188 | =head1 HOW IT WORKS | ||
| 189 | |||
| 190 | Contrary to what it might seem, this module knows nothing of Apple | ||
| 191 | Events, and only knows a sprinkling of AppleScript syntax. | ||
| 192 | |||
| 193 | Instead, it actually employs a variety of magic dust to accomplish its | ||
| 194 | tasks: | ||
| 195 | |||
| 196 | =over 4 | ||
| 197 | |||
| 198 | =item * | ||
| 199 | |||
| 200 | The Mac::AppleScript::Glue module translates Perl-style object/method | ||
| 201 | calls to actual AppleScript. | ||
| 202 | |||
| 203 | =item * | ||
| 204 | |||
| 205 | The resulting AppleScript is executed by the Mac::AppleScript(3pm) | ||
| 206 | module (by Dan Sugalski); any results are returned as text. | ||
| 207 | |||
| 208 | =item * | ||
| 209 | |||
| 210 | The AppleScript-format result data is translated into into Perl data | ||
| 211 | structures as appropriate. | ||
| 212 | |||
| 213 | =item * | ||
| 214 | |||
| 215 | Perl's C<$AUTOLOAD> feature (L<perlobj>) is used to translate | ||
| 216 | statements like C<< $finder->insertion_location >> to AppleScript. | ||
| 217 | Method calls that aren't defined in the module itself and don't refer | ||
| 218 | to a part of the object's data structure are delegated to a translater | ||
| 219 | function that tries to write the method as if it was AppleScript. | ||
| 220 | |||
| 221 | =item * | ||
| 222 | |||
| 223 | AppleScript's concept of the "object reference" is essential to the | ||
| 224 | idea of having Perl objects for things other than applications. | ||
| 225 | |||
| 226 | =item * | ||
| 227 | |||
| 228 | The AppleScript interpreter seems somewhat lenient on the exact syntax | ||
| 229 | of the language. This makes it possible to write AppleScript | ||
| 230 | statements that work even though they look weird. | ||
| 231 | |||
| 232 | =back | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 237 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 238 | |||
| 239 | require 5.6.0; | ||
| 240 | |||
| 241 | ###################################################################### | ||
| 242 | |||
| 243 | 3 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 244 | |||
| 245 | our ($VERSION, $AUTOLOAD, @EXPORT, @EXPORT_OK); | ||
| 246 | |||
| 247 | 3 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 248 | 3 | @EXPORT = qw(); | |
| 249 | |||
| 250 | 3 | @EXPORT_OK = qw( | |
| 251 | %Debug | ||
| 252 | @DebugAll | ||
| 253 | dump | ||
| 254 | dump_pretty | ||
| 255 | is_number | ||
| 256 | to_string | ||
| 257 | from_string | ||
| 258 | ); | ||
| 259 | } | ||
| 260 | |||
| 261 | our (%Debug, @DebugAll); | ||
| 262 | |||
| 263 | %Debug = (); | ||
| 264 | |||
| 265 | # | ||
| 266 | # NOTE: remember to update the "Debugging" section below if these are | ||
| 267 | # added or changed | ||
| 268 | # | ||
| 269 | |||
| 270 | @DebugAll = qw( | ||
| 271 | INIT | ||
| 272 | AUTOLOAD | ||
| 273 | SCRIPT | ||
| 274 | RESULT | ||
| 275 | EXEC | ||
| 276 | PARSE | ||
| 277 | ); | ||
| 278 | |||
| 279 | ###################################################################### | ||
| 280 | |||
| 281 | 3 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 282 | 3 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 283 | $Data::Dumper::Indent = | ||
| 284 | $Data::Dumper::Useqq = 1; | ||
| 285 | 3 | ▶ | use IO::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 286 | 3 | ▶ | use Text::ParseWords qw(); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 287 | 0 | ▶ | use Mac::AppleScript 0.03; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 288 | |||
| 289 | use Mac::AppleScript::Glue::Application; | ||
| 290 | use Mac::AppleScript::Glue::Object; | ||
| 291 | |||
| 292 | ###################################################################### | ||
| 293 | ###################################################################### | ||
| 294 | # beginning of methods | ||
| 295 | |||
| 296 | =head1 METHODS | ||
| 297 | |||
| 298 | There aren't any useful public methods in Mac::Application::Glue | ||
| 299 | itself. Instead, see L<Mac::AppleScript::Glue::Application> and | ||
| 300 | L<Mac::AppleScript::Glue::Object>. | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | ###################################################################### | ||
| 305 | |||
| 306 | # | ||
| 307 | # Constructor for object. Once initialized, each key/value pair of | ||
| 308 | # the argument list is treated as a separate method call, where the | ||
| 309 | # method corresponds to the key. | ||
| 310 | # | ||
| 311 | |||
| 312 | sub new { | ||
| 313 | my ($type, @args) = @_; | ||
| 314 | |||
| 315 | my $self = bless {}, $type; | ||
| 316 | |||
| 317 | $self->_init(\@args) | ||
| 318 | or return undef; | ||
| 319 | |||
| 320 | my %args = @args; | ||
| 321 | |||
| 322 | while (my ($method, $val) = each %args) { | ||
| 323 | warn "init: calling method \"$method\" with $val\n" | ||
| 324 | if $Debug{INIT}; | ||
| 325 | |||
| 326 | $self->$method($val); | ||
| 327 | } | ||
| 328 | |||
| 329 | $self->dump('initialized') | ||
| 330 | if $Debug{INIT}; | ||
| 331 | |||
| 332 | return $self; | ||
| 333 | } | ||
| 334 | |||
| 335 | ###################################################################### | ||
| 336 | |||
| 337 | # | ||
| 338 | # Default method for initializing a new object. Does nothing except | ||
| 339 | # return itself. | ||
| 340 | # | ||
| 341 | |||
| 342 | sub _init { | ||
| 343 | my ($self, $args) = @_; | ||
| 344 | |||
| 345 | return $self; | ||
| 346 | } | ||
| 347 | |||
| 348 | ###################################################################### | ||
| 349 | |||
| 350 | # | ||
| 351 | # An AUTOLOADer that handles all function/method calls not otherwise | ||
| 352 | # defined. It works by looking in the hashref $self to see if there's | ||
| 353 | # a key that starts with an underscore that corresponds to the | ||
| 354 | # attempted method (eg, "_foo" for a call of C<< $self->obj >>). | ||
| 355 | # | ||
| 356 | # Handles a simple "set" semantic with one argument. | ||
| 357 | # | ||
| 358 | # Calls the _unknown_method method for attempted methods that don't | ||
| 359 | # correspond to the $self's data structure. | ||
| 360 | # | ||
| 361 | |||
| 362 | sub AUTOLOAD { | ||
| 363 | my ($self, @args) = @_; | ||
| 364 | |||
| 365 | my $type = ref $self; | ||
| 366 | |||
| 367 | $AUTOLOAD =~ s/^.*:://; | ||
| 368 | |||
| 369 | my $method = $AUTOLOAD; | ||
| 370 | |||
| 371 | return if $method eq 'DESTROY'; | ||
| 372 | |||
| 373 | if ($Debug{AUTOLOAD}) { | ||
| 374 | warn "\n[" . ref(${self}) . "::AUTOLOAD->$method]\n", | ||
| 375 | Data::Dumper->Dump( | ||
| 376 | [$self, \@args, join(':', (caller(0))[1..2])], | ||
| 377 | [qw(self args caller)] | ||
| 378 | ); | ||
| 379 | } | ||
| 380 | |||
| 381 | if (exists $self->{"_$method"}) { | ||
| 382 | warn "[AUTOLOAD: calling local method \"$method\"]\n" | ||
| 383 | if $Debug{AUTOLOAD}; | ||
| 384 | |||
| 385 | if (@args) { | ||
| 386 | $self->{"_$method"} = $args[0]; | ||
| 387 | } | ||
| 388 | |||
| 389 | return $self->{"_$method"}; | ||
| 390 | } | ||
| 391 | |||
| 392 | warn "[AUTOLOAD: handling unknown method \"$method\"]\n" | ||
| 393 | if $Debug{AUTOLOAD}; | ||
| 394 | |||
| 395 | return $self->_unknown_method($method, @args); | ||
| 396 | } | ||
| 397 | |||
| 398 | ###################################################################### | ||
| 399 | |||
| 400 | # | ||
| 401 | # A default handler for AUTOLOAD'ed function calls. | ||
| 402 | # | ||
| 403 | |||
| 404 | sub _unknown_method { | ||
| 405 | my ($self, $method, @args) = @_; | ||
| 406 | |||
| 407 | confess "no method called \"$method\" in object $self"; | ||
| 408 | } | ||
| 409 | |||
| 410 | # end of methods | ||
| 411 | ###################################################################### | ||
| 412 | |||
| 413 | ###################################################################### | ||
| 414 | # beginning of functions | ||
| 415 | |||
| 416 | |||
| 417 | =head1 FUNCTIONS | ||
| 418 | |||
| 419 | Note that no functions are exported by default. You can use them by | ||
| 420 | specifying the full package name: | ||
| 421 | |||
| 422 | Mac::AppleScript::Glue::run('something'); | ||
| 423 | |||
| 424 | or by specifying them on the C<use> statement at the top of your | ||
| 425 | program: | ||
| 426 | |||
| 427 | use Mac::AppleScript::Glue qw(run); | ||
| 428 | |||
| 429 | run('something'); | ||
| 430 | |||
| 431 | =over 4 | ||
| 432 | |||
| 433 | =cut | ||
| 434 | |||
| 435 | ###################################################################### | ||
| 436 | |||
| 437 | =item run([$app, ] @script) | ||
| 438 | |||
| 439 | Runs an AppleScript whose lines are in C<@script>. If C<$app> is | ||
| 440 | specified, it should be a previously created | ||
| 441 | Mac::AppleScript::Application(3pm) object to which any object | ||
| 442 | references will "belong to." | ||
| 443 | |||
| 444 | =cut | ||
| 445 | |||
| 446 | sub run { | ||
| 447 | my $app; | ||
| 448 | |||
| 449 | if (@_ && ref $_[0]) { | ||
| 450 | $app = shift; | ||
| 451 | } | ||
| 452 | |||
| 453 | my $script = join("\n", @_); | ||
| 454 | |||
| 455 | if ($Debug{SCRIPT}) { | ||
| 456 | warn "\n-- script --\n", $script, "\n"; | ||
| 457 | } | ||
| 458 | |||
| 459 | my $result_str = Mac::AppleScript::RunAppleScript($script); | ||
| 460 | |||
| 461 | unless (defined $result_str) { | ||
| 462 | chomp $@; | ||
| 463 | |||
| 464 | if ($Debug{SCRIPT}) { | ||
| 465 | warn "-- error --\n", | ||
| 466 | "$@\n", | ||
| 467 | "-- done\n"; | ||
| 468 | } | ||
| 469 | |||
| 470 | die "Mac::AppleScript returned error ($@)\n"; | ||
| 471 | } | ||
| 472 | |||
| 473 | # | ||
| 474 | # work around Mac::AppleScript returning garbage in cases where it | ||
| 475 | # should return emptiness | ||
| 476 | # | ||
| 477 | |||
| 478 | $result_str =~ s/^\001.*//; | ||
| 479 | |||
| 480 | if ($Debug{SCRIPT}) { | ||
| 481 | warn "-- result --\n", | ||
| 482 | "$result_str\n", | ||
| 483 | "-- done --\n"; | ||
| 484 | } | ||
| 485 | |||
| 486 | my $result = from_string($app, $result_str); | ||
| 487 | |||
| 488 | if ($Debug{RESULT}) { | ||
| 489 | dump_pretty($result, 'result'); | ||
| 490 | } | ||
| 491 | |||
| 492 | return $result; | ||
| 493 | } | ||
| 494 | |||
| 495 | ###################################################################### | ||
| 496 | |||
| 497 | =item from_string([$app,] $str) | ||
| 498 | |||
| 499 | Parses a string containing an AppleScript result, and returns the Perl | ||
| 500 | data structures corresponding to that result. If C<$app> is specifed | ||
| 501 | as a Mac::AppleScript::Glue::Application(3pm) object, any object | ||
| 502 | references will be "owned" by that application. | ||
| 503 | |||
| 504 | =cut | ||
| 505 | |||
| 506 | sub from_string { | ||
| 507 | my $app; | ||
| 508 | my $str; | ||
| 509 | |||
| 510 | if (@_ == 2) { | ||
| 511 | ($app, $str) = @_; | ||
| 512 | } else { | ||
| 513 | ($str) = @_; | ||
| 514 | } | ||
| 515 | |||
| 516 | return undef unless $str; | ||
| 517 | |||
| 518 | chomp $str; | ||
| 519 | |||
| 520 | my @tokens = grep($_, | ||
| 521 | map { | ||
| 522 | # remove leading/trailing space | ||
| 523 | |||
| 524 | if ($_) { | ||
| 525 | s/^\s+//; | ||
| 526 | s/\s+$//; | ||
| 527 | } | ||
| 528 | |||
| 529 | $_; | ||
| 530 | |||
| 531 | } Text::ParseWords::parse_line( | ||
| 532 | '[,{}:]', | ||
| 533 | 'delimiters', | ||
| 534 | $str | ||
| 535 | ) | ||
| 536 | ); | ||
| 537 | |||
| 538 | warn Data::Dumper->Dump([\@tokens], [qw(tokens)]) | ||
| 539 | if $Debug{PARSE}; | ||
| 540 | |||
| 541 | my $result = _parse_word(\@tokens, $app); | ||
| 542 | |||
| 543 | warn Data::Dumper->Dump([$result], [qw(result)]) | ||
| 544 | if $Debug{PARSE}; | ||
| 545 | |||
| 546 | return $result; | ||
| 547 | } | ||
| 548 | |||
| 549 | ###################################################################### | ||
| 550 | |||
| 551 | # | ||
| 552 | # internal function to parse a AppleScript list or record | ||
| 553 | # | ||
| 554 | |||
| 555 | sub _parse_list { | ||
| 556 | my ($tokens, $app) = @_; | ||
| 557 | |||
| 558 | my @list; | ||
| 559 | my $is_hash; | ||
| 560 | |||
| 561 | while (@$tokens) { | ||
| 562 | my $token = shift @$tokens; | ||
| 563 | |||
| 564 | # if the token after the next one is a colon, then this is | ||
| 565 | # a record, not a list, and this token is the key | ||
| 566 | |||
| 567 | if ($tokens->[0] && $tokens->[0] eq ':') { | ||
| 568 | $token =~ s/ /_/g; | ||
| 569 | push @list, $token; | ||
| 570 | shift @$tokens; | ||
| 571 | $is_hash = 1; | ||
| 572 | next; | ||
| 573 | } | ||
| 574 | |||
| 575 | # right-brace: list or record terminator | ||
| 576 | if ($token eq '}') { | ||
| 577 | last; | ||
| 578 | |||
| 579 | # comma: list or record separator | ||
| 580 | } elsif ($token eq ',') { | ||
| 581 | if ($is_hash && @list % 2 != 0) { | ||
| 582 | push @list, undef; | ||
| 583 | } | ||
| 584 | |||
| 585 | # ignore | ||
| 586 | |||
| 587 | # something else | ||
| 588 | } else { | ||
| 589 | unshift @$tokens, $token; | ||
| 590 | push @list, _parse_word($tokens, $app); | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | if ($is_hash) { | ||
| 595 | return { @list }; | ||
| 596 | } else { | ||
| 597 | return \@list; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | ###################################################################### | ||
| 602 | |||
| 603 | # | ||
| 604 | # internal function parse an AppleScript word (which could be the | ||
| 605 | # start of a list or record; see _parse_list above) | ||
| 606 | # | ||
| 607 | |||
| 608 | sub _parse_word { | ||
| 609 | my ($tokens, $app) = @_; | ||
| 610 | |||
| 611 | my $token = shift @$tokens; | ||
| 612 | |||
| 613 | # left-brace? -- it's a start of list or record | ||
| 614 | if ($token eq '{') { | ||
| 615 | return _parse_list($tokens, $app); | ||
| 616 | |||
| 617 | # number? -- leave as is | ||
| 618 | } elsif (is_number($token)) { | ||
| 619 | return $token; | ||
| 620 | |||
| 621 | # quoted-string? -- remove quotes | ||
| 622 | } elsif ($token =~ s/^"(.*?)"$/$1/) { | ||
| 623 | return $token; | ||
| 624 | |||
| 625 | # otherwise it's a reference of some sort | ||
| 626 | } else { | ||
| 627 | if ($app) { | ||
| 628 | my $appref = $app->ref; | ||
| 629 | |||
| 630 | $token =~ s/ of $appref$//; | ||
| 631 | } | ||
| 632 | |||
| 633 | return new Mac::AppleScript::Glue::Object( | ||
| 634 | app => $app, | ||
| 635 | ref => $token, | ||
| 636 | ); | ||
| 637 | } | ||
| 638 | } | ||
| 639 | |||
| 640 | ###################################################################### | ||
| 641 | |||
| 642 | =item to_string($value) | ||
| 643 | |||
| 644 | Converts a Perl data structure into an AppleScript string. It will | ||
| 645 | correctly interpret Mac::AppleScript::Glue::Object(3pm) objects. | ||
| 646 | |||
| 647 | =cut | ||
| 648 | |||
| 649 | sub to_string { | ||
| 650 | my ($value) = @_; | ||
| 651 | |||
| 652 | # | ||
| 653 | # arrays are converted to AS lists (recursively) | ||
| 654 | # | ||
| 655 | |||
| 656 | if (ref($value) eq 'ARRAY') { | ||
| 657 | return '{' | ||
| 658 | . join(', ', | ||
| 659 | map { | ||
| 660 | to_string($_) | ||
| 661 | } @{$value}) | ||
| 662 | . '}'; | ||
| 663 | |||
| 664 | # | ||
| 665 | # hashes are converted to AS records (recursively) | ||
| 666 | # | ||
| 667 | |||
| 668 | } elsif (ref($value) eq 'HASH') { | ||
| 669 | my @list; | ||
| 670 | |||
| 671 | for my $key (keys %{$value}) { | ||
| 672 | my $val = $value->{$key}; | ||
| 673 | |||
| 674 | $key =~ s/_/ /g; | ||
| 675 | |||
| 676 | push @list, "$key:" . to_string($val) | ||
| 677 | } | ||
| 678 | |||
| 679 | return '{' . join(', ', @list) . '}'; | ||
| 680 | |||
| 681 | # | ||
| 682 | # scalar-refs are let through verbatim | ||
| 683 | # | ||
| 684 | |||
| 685 | } elsif (ref($value) eq 'SCALAR') { | ||
| 686 | return $$value; | ||
| 687 | |||
| 688 | # | ||
| 689 | # object references are let through verbatim | ||
| 690 | # | ||
| 691 | |||
| 692 | } elsif (ref $value && $value->isa('Mac::AppleScript::Glue::Object')) { | ||
| 693 | return $value->ref; | ||
| 694 | |||
| 695 | # | ||
| 696 | # otherwise it's something we don't know how to handle | ||
| 697 | # | ||
| 698 | |||
| 699 | } elsif (ref $value) { | ||
| 700 | confess "bad reference found in data"; | ||
| 701 | |||
| 702 | # | ||
| 703 | # numbers are let through as is | ||
| 704 | # | ||
| 705 | |||
| 706 | } elsif (is_number($value)) { | ||
| 707 | return $value; | ||
| 708 | |||
| 709 | # | ||
| 710 | # strings enclosed in <> are treated like object-references | ||
| 711 | # | ||
| 712 | |||
| 713 | } elsif ($value =~ /^<(.*)>$/) { | ||
| 714 | return $1; | ||
| 715 | |||
| 716 | # | ||
| 717 | # anything else is a string, and is quoted | ||
| 718 | # | ||
| 719 | |||
| 720 | } else { | ||
| 721 | $value =~ s/^\\</</g; # allow "\<..." for quoting brackets | ||
| 722 | $value =~ s/\\/\\\\/g; # quote backslashes | ||
| 723 | $value =~ s/"/\\"/g; # quote double-quotes | ||
| 724 | |||
| 725 | return "\"$value\""; # enclosee in double-quotes | ||
| 726 | } | ||
| 727 | } | ||
| 728 | |||
| 729 | ###################################################################### | ||
| 730 | |||
| 731 | =item dump($obj [, $label]) | ||
| 732 | |||
| 733 | Provides a simple dumping facility for any sort of data. All this | ||
| 734 | does is call Data::Dumper(3pm)'s C<Dump()> method. | ||
| 735 | |||
| 736 | =cut | ||
| 737 | |||
| 738 | sub dump { | ||
| 739 | my ($obj, $label) = @_; | ||
| 740 | |||
| 741 | warn Data::Dumper->Dump([$obj], [$label || 'obj']); | ||
| 742 | } | ||
| 743 | |||
| 744 | ###################################################################### | ||
| 745 | |||
| 746 | =item dump_pretty($object, $label [, $fh]) | ||
| 747 | |||
| 748 | Provides a nicely-formatted view of any object. The object can be as | ||
| 749 | simple as a regular scalar, or a deeply-nested tree of references. If | ||
| 750 | the object is a Mac::AppleScript::Glue::Object, angle-brackets (E<lt>, | ||
| 751 | E<gt>) are placed around its value. | ||
| 752 | |||
| 753 | If a string is supplied as C<$label>, the output will be labeled suchly. | ||
| 754 | |||
| 755 | Output is to B<STDERR> by default; you can provide an alternate | ||
| 756 | filehandle in C<$fh> if you like. | ||
| 757 | |||
| 758 | =cut | ||
| 759 | |||
| 760 | sub dump_pretty { | ||
| 761 | my ($obj, $label, $fh, $level) = @_; | ||
| 762 | |||
| 763 | $fh ||= \*STDERR; | ||
| 764 | $level ||= 0; | ||
| 765 | |||
| 766 | my $indent = "\t" x $level; | ||
| 767 | |||
| 768 | $fh->print($indent); | ||
| 769 | |||
| 770 | if ($label) { | ||
| 771 | $fh->print("$label: "); | ||
| 772 | } | ||
| 773 | |||
| 774 | if (!defined $obj) { | ||
| 775 | $fh->print("undef\n"); | ||
| 776 | |||
| 777 | } elsif (ref $obj) { | ||
| 778 | if (ref($obj) eq 'ARRAY') { | ||
| 779 | $fh->print("[\n"); | ||
| 780 | |||
| 781 | for (@{$obj}) { | ||
| 782 | dump_pretty($_, undef, $fh, $level + 1); | ||
| 783 | } | ||
| 784 | |||
| 785 | $fh->print($indent, "]\n"); | ||
| 786 | |||
| 787 | } elsif (ref($obj) eq 'HASH') { | ||
| 788 | $fh->print("{\n"); | ||
| 789 | |||
| 790 | for (sort keys %{$obj}) { | ||
| 791 | dump_pretty($obj->{$_}, $_, $fh, $level + 1); | ||
| 792 | } | ||
| 793 | |||
| 794 | $fh->print($indent, "}\n"); | ||
| 795 | |||
| 796 | } elsif ($obj->isa('Mac::AppleScript::Glue::Object')) { | ||
| 797 | $fh->print('<' . $obj->ref . ">\n"); | ||
| 798 | |||
| 799 | } else { | ||
| 800 | $fh->print("<$obj>\n"); | ||
| 801 | } | ||
| 802 | |||
| 803 | } elsif (is_number($obj)) { | ||
| 804 | $fh->print("$obj\n"); | ||
| 805 | |||
| 806 | } else { | ||
| 807 | $fh->print("\"$obj\"\n"); | ||
| 808 | } | ||
| 809 | } | ||
| 810 | |||
| 811 | ###################################################################### | ||
| 812 | |||
| 813 | =item is_number($str) | ||
| 814 | |||
| 815 | Returns true if the given string is really a number. | ||
| 816 | |||
| 817 | =cut | ||
| 818 | |||
| 819 | sub is_number { | ||
| 820 | my ($str) = @_; | ||
| 821 | |||
| 822 | # this line borrowed from the perl FAQs | ||
| 823 | |||
| 824 | $str =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/; | ||
| 825 | } | ||
| 826 | |||
| 827 | ###################################################################### | ||
| 828 | ###################################################################### | ||
| 829 | # end of functions | ||
| 830 | |||
| 831 | =back | ||
| 832 | |||
| 833 | =head1 DEBUGGING | ||
| 834 | |||
| 835 | Various amounts of debugging can be enabled by manipulating the | ||
| 836 | C<%Mac::AppleScript::Glue::Debug> hash. Debugging usually involves | ||
| 837 | printing messages to the B<STDERR> file handle. | ||
| 838 | |||
| 839 | To turn on a certain type of debugging, specify the key that names the | ||
| 840 | debug option, and a value of non-zero. For example, the following | ||
| 841 | enables debugging of generated AppleScripts: | ||
| 842 | |||
| 843 | $Mac::AppleScript::Glue::Debug{SCRIPT} = 1; | ||
| 844 | |||
| 845 | You can get a list of all the debugging keywords by examining | ||
| 846 | C<@Mac::AppleScript::Glue::DebugAll>. | ||
| 847 | |||
| 848 | |||
| 849 | =head2 Debugging keywords | ||
| 850 | |||
| 851 | =over 4 | ||
| 852 | |||
| 853 | =item SCRIPT | ||
| 854 | |||
| 855 | Show each generated AppleScript before it's sent off to the script | ||
| 856 | interpreter, as well as the AppleScript-formatted result string. This | ||
| 857 | is useful when writing programs using Mac::AppleScript::Glue, as | ||
| 858 | looking at the generated AppleScript is often the best way to figure | ||
| 859 | out why a statement is failing. | ||
| 860 | |||
| 861 | =item RESULT | ||
| 862 | |||
| 863 | Show the parsed return value from the AppleScript result. This is the | ||
| 864 | data you will be working with when you examine a return value from a | ||
| 865 | statement. | ||
| 866 | |||
| 867 | =item PARSE | ||
| 868 | |||
| 869 | Show the process of parsing the AppleScript result. You probably | ||
| 870 | don't want to be setting this. | ||
| 871 | |||
| 872 | =item INIT | ||
| 873 | |||
| 874 | Show the values of Mac::AppleScript::Glue objects after all | ||
| 875 | initialization has been completed. You probably don't want to be | ||
| 876 | setting this. | ||
| 877 | |||
| 878 | =item AUTOLOAD | ||
| 879 | |||
| 880 | Show attempted calls to non-existent functions and methods. You | ||
| 881 | probably don't want to be setting this. | ||
| 882 | |||
| 883 | =back | ||
| 884 | |||
| 885 | |||
| 886 | =head1 HINTS | ||
| 887 | |||
| 888 | Unfortunately this package doesn't mean that you don't have to know | ||
| 889 | AppleScript, or the class/event hierarchy of the operating system. | ||
| 890 | Both of those can be quite inscrutable. | ||
| 891 | |||
| 892 | I recommend having the Script Editor program open while writing Perl | ||
| 893 | code. Use the dictionary browser (File menu > Open Dictionary) to | ||
| 894 | browse the dictionaries for the applications you're trying to control. | ||
| 895 | If you're having trouble getting the right Perl code written, try | ||
| 896 | writing it in AppleScript first, then translate to Perl, then let this | ||
| 897 | module translate it back to AppleScript. ;) | ||
| 898 | |||
| 899 | If you're trying to navigate through inscrutable AppleScript results, | ||
| 900 | try using the C<dump_pretty()> function (see above). | ||
| 901 | |||
| 902 | Finally, turn on the B<SCRIPT> and B<RESULT> debugging keywords for | ||
| 903 | the most useful yet not-too-overwhelming debug output. | ||
| 904 | |||
| 905 | |||
| 906 | =head1 BUGS AND ISSUES | ||
| 907 | |||
| 908 | =over 4 | ||
| 909 | |||
| 910 | =item * | ||
| 911 | |||
| 912 | It's fairly slow. This is mostly because a compound statement (C<< | ||
| 913 | $finder->insertion_location->name >>) requires several separate | ||
| 914 | AppleScript executions, generally one per element besides the | ||
| 915 | application object. It's also slow because under the hood, the Perl | ||
| 916 | calls are translated to AppleScript, then compiled, and finally | ||
| 917 | executed. | ||
| 918 | |||
| 919 | =item * | ||
| 920 | |||
| 921 | Error-handling is nearly non-existant. If the resulting AppleScript | ||
| 922 | is bad, or the target applications don't understand the resulting | ||
| 923 | AppleScript, this module will force a C<die()>, and you will see | ||
| 924 | errors on B<STDERR>. If you want to trap this, use C<eval>. | ||
| 925 | |||
| 926 | =item * | ||
| 927 | |||
| 928 | AppleScript generation is not quite right. However, it works most of | ||
| 929 | the time. | ||
| 930 | |||
| 931 | =back | ||
| 932 | |||
| 933 | =head1 BUG REPORTS, CONTRIBUTIONS AND SUPPORT | ||
| 934 | |||
| 935 | Please use GitHub for all communications regarding this module: | ||
| 936 | L<GitHub|https://github.com/sdondley/Mac-AppleScript-Glue> | ||
| 937 | |||
| 938 | =head1 SEE ALSO | ||
| 939 | |||
| 940 | L<Mac::AppleScript> | ||
| 941 | |||
| 942 | L<Mac::AppleScript::Glue::Application> | ||
| 943 | |||
| 944 | L<Mac::AppleScript::Glue::Object> | ||
| 945 | |||
| 946 | the application dictionaries, accessible through the B<Script Editor> | ||
| 947 | application (in the C</Applications/AppleScript> folder) | ||
| 948 | |||
| 949 | =head1 AUTHOR | ||
| 950 | |||
| 951 | Original author: John Labovitz | ||
| 952 | Maintainer: Steve Dondley | ||
| 953 | |||
| 954 | =head1 ACKNOWLEDGEMENTS FROM ORIGINAL AUTHOR | ||
| 955 | |||
| 956 | Thanks to David Bonn for the use of his mountain retreat, where most | ||
| 957 | of this module was written over three days of peace, quiet, and light. | ||
| 958 | |||
| 959 | =head1 COPYRIGHT | ||
| 960 | |||
| 961 | Copyright (c) 2019 John Labovitz. All rights reserved. This program is | ||
| 962 | free software; you can redistribute it and/or modify it under the same | ||
| 963 | terms as Perl itself. | ||
| 964 | |||
| 965 | =cut | ||
| 966 | |||
| 967 | 1; | ||