Re: Example: Can an AppleScript accept arguments
Re: Example: Can an AppleScript accept arguments
- Subject: Re: Example: Can an AppleScript accept arguments
- From: Rolf Ordahl <email@hidden>
- Date: Tue, 2 Jul 2002 15:33:39 -0500
It sounds like you are struggling with something I have been struggling
with as well. I am working on an AppleScript application with a friend
who has been learning PERL. Keep in mind we are both newbies, but maybe
this will help.
We wanted to pass text strings to a perl script so the perl script could
act on the text and return it to AS for display. I posted a request for
help on this list. Alex Robinson sent back a very helpful reply:
==================
You can invoke the perl scripts using "do shell script" either directly
do shell script "perl -e 'print q[hello world];'"
or (probably more usefully to you) by invoking scripts
do shell script "PATH_TO_SCRIPT"
where PATH_TO_SCRIPT is the full path to the script you want to invoke.
You can pass arguments to the script by placing them afterwards as normal
and access them using the usual ARGV variables that your friend should be
familiar with
do shell script "PATH_TO_SCRIPT arg1 arg2 arg3"
To get stuff back from the script, whatever you print to STDOUT will be
returned to AppleScript
You might also want to check out CamelBones which allows you to write
Cocoa
apps in Perl (although the documentation is sadly lagging behind)
http://camelbones.sourceforge.net/
========================
While perl will return text as a result through STDOUT, my understanding
is there is no ability in AS to use STDIN yet. So we decided to use a
delimited list of text strings in one argument. Through a lot of
experimentation and research, we came up with the below test AS and perl
scripts which we believe are achieving our goal. The key on the perl
side was Getopt::Long, because that was the only way we have found so
far to get the perl script to treat the text as text and not a file name.
AppleScript:
do shell script "perl /PATH_TO_PERL_SCRIPT/PERL_SCRIPT --str \"anything
here\""
set x to result as text
display dialog x
Perl Script:
#!/usr/bin/perl -w
use Getopt::Long;
&GetOptions("str=s" => \$result);
print "$result\n";
We plan to send delimited text strings as a list in one argument and
parse/coerce them into individual variables by item number. Item number
one = true/false, item #2 = file path, item #3 = text, etc.
I hope this is helpful. We certainly would appreciate any better
way/suggestions.
Good luck,
Rolf
On Monday, July 1, 2002, at 11:11 AM, bryan wrote:
The way to send arguments to applescripts, is to sent them events after
you activate them.
I am not familiar with the commands to do this from the event side
<<event ?????>> though;
sorry.
From another applescript you tell the target to "handler_name
(argument)" as follows:
--script_1
activate--not necessary if script_2 is running
tell script_2 to do_dialog("hi")
--end script_1
--script_2
on o_dialog (arg1)
set argument to arg1
if argument = "hi" then
display dialog "Hello there!"
else if argument = "bye" then
display dialog "See you later..."
else
display dialog "I don't understand " & argument
end if
end o_dialog
--end script_2
I know there is an event which you can generate from a command line,
but I do not know
what it is. I am sure someone on this list knows what it is though.
yoiu have to be sure the
application process (the script) is running, set the target to the
script, and send the
event to
the target.
Anybody know what the events are?
(Sorry in advance for the multipost)
Bryan Kaufman
"Spunk S. Spunk III" wrote:
It looks like Applescript only accepts filenames/paths/folders as
arguments.
Here's an example of what I would like to see (although it doesn't
work!):
on run (arg1)
set argument to arg1
if argument = "hi" then
display dialog "Hello there!"
else if argument = "bye" then
display dialog "See you later..."
else
display dialog "I don't understand " & argument
end if
end run
This returns an error stating you can't set <<script>> to a string (in
this
case the variable "argument"). I want to know if there is a way for the
argument to be handled as text in order to run tests on (if/then). This
could perhaps be accomplished with a "set argument to arg1 as text" or
maybe
"as argument" or something.
This functionality may in fact exist... Hence my question. If not, why
not
add it. Again, this could only be used from the command line or another
script but I think it would find many uses.
Anyone?
Spunk
_______________________________________________
applescript-studio mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-studio
Do not post admin requests to the list. They will be ignored.
[demime 0.98b removed an attachment of type text/x-vcard which had a
name of bryan.vcf]
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.