[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Scripting I/O



At 15.34 1998.02.23, Stephan Somogyi wrote:
>However, I'm curious whether there are more elaborate facilities for
>getting exchanging data between AppleScript and MacPerl. Eg, can I get
>the values of specific AppleScript variables into MacPerl (and
>vice-versa), and are there type-conversion facilities that would, for
>example, allow me to convert an AppleScript list into a Perl array?

Well, to clarify, you can only pass data, not variables.  As to type
conversion for scalars, I don't know much about AS, but Perl obviously
doesn't care.  As to arrays, you need to join and split yourself, unless
you send via Apple Events by setting up an event handler in one or the
other.

_Passing data._  We make a Perl variable, pass it to the AppleScript text
(note the double-quotes around "  EOS" which forces variable interpolation)
and then we return the value back.

  $myVal = "hello";
  print MacPerl::DoAppleScript(<<"  EOS");
  set myVal to "$myVal"
  return myVal
  EOS

Also, if calling from AppleScript: remember, the MacPerl is just a string.
I don't know how to do interpolation in a string in AS if it possible; so I
just concatenate.  MacPerl::Reply() send the data back to the calling
process.

  set myVal to "hello"
  tell application "MacPerl"
    return Do Script("
      $myVal = " & myVal & ";
      MacPerl::Reply($myVal);
    ")
  end tell

Arrays:

  tell application "MacPerl"
    set myResult to Do Script Â
    "MacPerl::Reply(join ('|', ('a'..'g')))"
  end tell

  set AppleScript's text item delimiters to "|"
  set myList to text items of myResult
  set AppleScript's text item delimiters to ""
  return myList

And:

  use Text::ParseWords;
  $script = <<'  EOS';
    set myList to {"a", "b", "c", "d", "e", "f", "g"}
    return myList
  EOS
  ($result = MacPerl::DoAppleScript($script))
    =~ s/^\{(.*)\}$/$1/;
  @array = quotewords(', ', 0, $result);
  print join(' ', @array);


Finally, setting up an event handler in MacPerl:

  #!perl -w
  use Mac::AppleEvents;
  my($ok);

  # set handler for app McPL and event id DATA
  $AppleEvent{'McPL', 'DATA'} = \&RecData;
  print "Starting ...\n";
  while(!$ok){sleep(1)}
  print "Done.\n";

  sub RecData {
    my($event, $reply, $desc, $n, $c) = @_;
    $desc = AEGetParamDesc($event, keyDirectObject());
    $c = AECountItems($desc);
    foreach (1..$c) {
      $n = AEGetNthDesc($desc, $_);
      print AEPrint($n), "\n";
      AEDisposeDesc($n);
    }
    foreach ('q'..'z') {
      AEPut($desc, ++$c, 'TEXT', $_)
    }
    AEPutParamDesc($reply, keyDirectObject(), $desc);
    print AEPrint($event), "\n";
    print AEPrint($reply), "\n";
    $ok = 1;
    0;
  }


The calling it via this AppleScript:

  tell application "MacPerl"
    set myList to {"a", "b", "c", "d", "e", "f", "g"}
    return Send Data myList
  end tell

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10  1FF7 7F13 8180 B6B6'])
#==               New Book:  MacPerl: Power and Ease               ==#
#==    Publishing Date: Early 1998. http://www.ptf.com/macperl/    ==#



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch