Welcome to Part 2 of How to write apps for macOS/OSX in Perl!
See Part 1 to get started with the built-in
macOS devtool Automator. This edition demonstrates how to:
- Process choices with Perl from an Applescript dialog to:
- Display output from Perl to an Applescript dialog.
- Execute Perl in Terminal to display its output.
- Send output from Perl to an application (TextExit).
Applescript is to the operating system what Javascript
is the the web browser. It can do many things and what it can't do can
always be handled by shell commands and especially Perl! When the code
below is saved by Automator as something like PerlVersionTool.app you
will have a 1.3MB portable binary application! Double click and ENJOY!
- Other techniques covered here include:
- Visiting websites (Perlmonks of course!)
- Displaying notifications
- Application control
- Applescript subroutines
- Abusing buttons to widen dialogs
- How to rule your world with Perl!
Source:
(* Demonstration macOS/OSX app in AppleScript and Perl *)
(* Posted to perlmonks.org by Anonymous Monk 6/14/2018 *)
(* Node: How to write apps for macOS/OSX in Perl! Part 2 *)
set TITLE to "Perl Version Tool"
set PROMPT to "Make a selection"
set _1 to "Perl version, patchlevel and license"
set _2 to "Perl configuration summary"
set _3 to "Perl command line help"
set _4 to "Visit Perlmonks.org!"
repeat
set what to choose from list {_1, _2, _3, _4} with title TITLE wit
+h prompt PROMPT OK button name {"View"} cancel button name {"Exit"} d
+efault items _1
set what to what as string
if what is _1 then
set CMD to "perl -v" # ONE LINERS OR PROGRAMS OF ANY SIZE!
else if what is _2 then
set CMD to "perl -V"
else if what is _3 then
set CMD to "perl -h"
else if what is _4 then
display notification "Opening The Monastery Gates!"
set CMD to "open https://perlmonks.org"
else if what is "false" then
return # EXIT
end if
if what is _2 then
# SEND PERL CODE TO TERMINAL AND EXECUTE
doShell(CMD)
else if what is _3 then
# CAPTURE PERL STDOUT
set CMD to do shell script CMD
# SEND PERL STDOUT TO TEXTEDIT
textEdit(CMD)
else
# CAPTURE PERL STDOUT
set RES to do shell script CMD
# MAKE DIALOG WIDE
set SPC to "
+ "
# PRINT PERL STDOUT TO APPLESCRIPT ALERT
display alert TITLE message RES buttons {SPC & "Cool" & SPC} d
+efault button 1
end if
end repeat
# APPLESCRIPT SUBS:
on doShell(CMD)
try
tell application "Terminal"
activate
tell application "System Events" to keystroke "n" using {c
+ommand down}
end tell
tell application "System Events"
tell application process "Terminal"
set frontmost to true
keystroke CMD
keystroke return
end tell
end tell
on error oops
display alert oops as critical
end try
end doShell
on textEdit(CMD)
try
tell application "TextEdit"
activate
tell application "System Events" to keystroke "n" using {c
+ommand down}
end tell
tell application "System Events"
tell application process "TextEdit"
set frontmost to true
keystroke CMD
end tell
end tell
on error oops
display alert oops as critical
end try
end textEdit
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|