Tuesday, July 14, 2020

#1 2002-03-26 06:16:39 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

call applescript from perl?

is it possible to call applescript from perl? if so, can someone point me to an example?


One mans trash is another mans treasure

Offline

 

#2 2002-12-07 06:11:15 am

altenburg
Member
From:: Netherlands
Registered: 2002-12-07
Posts: 6

Re: call applescript from perl?

Hi,

I rummaged thru some a Perl list I'm subscribed to.

Possible keywords for Google
doapplescript
mac::glue

I hope the snippets below suffice to get you started. Sorry for not having a clearcut answer, as I don't do Perl since I finished the introductory tutorials on it (see www.macinstruct.com in the multimedia section).

Bye for now,

Bert


Here is a script snippet:
MacPerl::DoAppleScript('tell application "Finder"  blah blah');

Here's another (with mac::glue)
Or to avoid DoAppleScript:

    use Mac::Glue;
    my $finder = new Mac::Glue 'Finder';

    # optional :-)
    my $app_path = $Mac::MoreFiles::Application{$app_sign};

    my $file = $finder->obj(file => $file_path);
    my $app  = $finder->obj(file => $app_path);
    $finder->open($file, using => $app);




if the file type/creator are set correctly, this works with explicit paths:

MacPerl::DoAppleScript(<<END_SCRIPT);
tell application "Finder"
    open file "Documents:RSC:Perl:basic:AppleScript:hello world"
end tell
END_SCRIPT




I have found there are things I can do in Applescript that I can't do in
MacPerl and more so the opposite way around.  What is nice is that using
AppleScript from MacPerl is mostly a pretty good solution when you need to
do Applescrit things.  One way to glue them together is to use the Script
editor to record the actions you wish to base some operation on and then use
that as a here document in MacPerl, sending the here document to AppleScript
via

&MacPerl'DoAppleScript("${script}");

Note that the return value of the script is the return value of the
applescript call, so you can use this in constructing further calls.

I have found that calling Applescript from MacPerl often leads to the
application not being found.  In Applescript this only occurs the first time
a script is run (as I recall) but it always occurs when calling from
MacPerl.  The work around for this that I have used is the following.

$script = "tell application "Finder" n";
$script .= "  open application file id "SIT!"n";
$script .= "  set appName to name of resultn";
$script .= "end telln";
my($tval) = &MacPerl'DoAppleScript($script);

You need to know the 4 letter code for your application for this.

with this response:

At 16:02 -0800 2002.03.11, Daschbach, John L wrote:
&MacPerl'DoAppleScript("${script}");

A much better way to do this is:

    MacPerl::DoAppleScript($script);

' instead of :: as package separator was deprecated a long time ago.  You
don't need to put a variable in quotes, and when you do, you don't need to
put the variable name in braces, unless it is to differentiate it from
something else.  The "&" is also unnecessary.

No big deal, but I figured I'd mention those while I was here.  smile


I have found that calling Applescript from MacPerl often leads to the
application not being found.  In Applescript this only occurs the first time
a script is run (as I recall) but it always occurs when calling from
MacPerl.  The work around for this that I have used is the following.

$script = "tell application "Finder" n";
$script .= "  open application file id "SIT!"n";
$script .= "  set appName to name of resultn";
$script .= "end telln";

A faster way to do this is simply:

    use Mac::Apps::Launch;
    LaunchApps('SIT!');


It depends on what you want to do. AppleScript is extremely slow as a
generic programming language, but OTOH, in order to control external
apps, calling an Applescript embedded in a Perl script takes a very long
time to compile -- which will have to happen every single time. The
alternative is raw AppleEvents, but it's very hard to write stuff in,
compared to plain AppleScript. Check out the modules Mac::AppleEvents,
Mac::AppleEvents::Simple, Mac::Glue. (They appear to come with the new
MacPerl 5.6.)

you can take AppleScript and compile it with Mac::OSA::Simple
(or Mac::OSA, of course).  You can compile it to memory or to disk, and
then execute it later.  It can be far more efficient than DoAppleScript,
obviously.

    use Mac::OSA::Simple;
    my $script = load_osa_script($path, 1); # boolean for loading from file
    $script->execute;

One thing that would be nice would be to make execute() accept parameters
somehow, so that you may pass in variables to a script.  Mac::OSA and
::Simple are not used much, so not much work is done on them, but if
someone wants these things, they can help me work on that.


Filed under: Finder

Offline

 

#3 2002-12-07 12:11:13 pm

Rob
Member
From:: Ohio, USA
Registered: 2002-11-20
Posts: 1886

Re: call applescript from perl?

is it possible to call applescript from perl? if so, can someone point me to an example?


I have absolutely no experience with Perl but here are a couple of links that I've bookmarked. I can't say for sure but hopefully they are relevant.

MacDevCenter: Controlling iTunes with Perl
Apple: Web Services with AppleScript and Perl

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)