Re: How to do some AppleScript inside a Perl program to label duplicates
Re: How to do some AppleScript inside a Perl program to label duplicates
- Subject: Re: How to do some AppleScript inside a Perl program to label duplicates
- From: "Mark J. Reed" <email@hidden>
- Date: Mon, 19 Sep 2005 13:37:45 -0400
On 9/19/05, Matt Neuburg <email@hidden> wrote:
> Interesting. My book discusses use of a here-doc but then runs osascript
> thru backticks, not with this "print" device. Should I be documenting this?
> Why is it better? m.
For one thing, backticks are an expression, which means they have a
value: the output of the command. Whenever you're not doing anything
with that value, backticks are a tad misleading, and stylistically
inappropriate.
More significantly, backticks always involve the user's shell in
between Perl and the command being executed, just like "do shell
script". That comes with a whole set of concerns from syntatic (extra
levels of interpretation that require extra levels of quoting) through
security issues.
Still, we are talking about different things. The code in your book
assigns the AppleScript text to a Perl variable using a here-doc and
then passes that text as an argument to the -e option of osascript.
Besides the backtick issues I mentioned above, here's an inherent
limit on the size of the code which can be passed this way, and you
run into serious problems if you have any apostrophes in it., but
that's not what I was objecting to.
While it wasn't stated explicitly in the thread, what I inferred was
that Pete was doing something like this in his Perl code:
`osascript <<DONE
tell app "Finder"
activate
display dialog "Hello, world!"
end
DONE`
That's not a Perl here-document, that's a shell here-document; the
entire thing is fed to and run by the shell.
Your basic options for running a shell command from Perl are these:
$output = `command`; # run command via the shell, capture its output
and store in var $output
system("command"); # run command (possibly via the shell); let its
output go wherever it
# normally does
Whenever your program is not doing anything with the ouptut of a
command, and you want it to go to wherever Perl's output is going, use
system() instead of `....`. The "possibly via the shell" is because
Perl looks at the string to see if it includes anything that requires
the shell, and if not, bypasses it and executes the target program
directly.
If you need to communicate with the command beyond just capturing its
output all at once, you can open a pipe to it with open():
open(FILEHANDLE, "|command");
# run command (possibly via the shell); anything printed to
FILEHANDLE becomes input to
# command.
open(FILEHANDLE,"command|");
# run command (possibly via the shell); its output may be obtained by
reading from FILEHANDLE.
My example used the first open() form above. By opening a pipe to
osascript and printing the AppleScript to it, rather than passing it
via -e, I avoided any restrictions on the size or content of the
AppleScript code.
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden