http://qs321.pair.com?node_id=1216610

macOS/OSX comes with tools that make it super easy to write native GUI applications with Applescript and Perl! This example uses the cool and free Robtex API to validate Autonomous System Numbers for networks in the global BGP table. Applescript provides plenty of ways to collect and display data, handle errors, and can launch terminals and text editors or any app and automate the entire operating system GUI while Perl does pretty much anything else you can imagine.

Start : Applications -> Automator
Select: File -> New
Select: Application

We're going to create an application but Automator can also encapsulate Perl into a Service, Image Capture Plugin, Dictation Command, Folder Action, Calendar Alarm, Print Plugin or Workflow.

Now that Automator is open click the Library icon or select View -> Show Library.

Select: Actions -> Utilities -> Run AppleScript (double click it)

Replace the default code with this:

(* Demonstration MacOS/OSX app in AppleScript and Perl *) (* Posted at perlmonks.org by Anonymous Monk 6/13/2018 *) (* Node: How to write apps for macOS/OSX in Perl! *) repeat repeat try set ASN to text returned of (display dialog "Autonomous Sy +stem Number: (Example: 714 is Apple Inc. 666 does not exist. Blank to exit.)" with +title "Perl ASN Check" default answer "" buttons {"Check"} default bu +tton 1) set ASN to ASN as number # require a number exit repeat # continue if ASN is numeric on error # not a number? display alert "Please enter an Autonomous System Number!" +as critical end try end repeat if ASN is equal to 0 then return # exit if blank # ALL MACS HAVE PERL BABY! set RES to do shell script " perl -MHTTP::Tiny -e ' my $r = HTTP::Tiny->new->get(q~https://freeapi.robtex.com/ +asquery/" & ASN & "~); if (length $r->{content}) { $r->{content} =~ /[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+/ ? print q~ASN Exists!~ : print q~ASN Not Found!~; } else { print q~Download failed!~ } ' " display alert RES end repeat

Save the application and double click its icon in finder. BEHOLD! Perl apps for macOS/OSX!!!

(Tips: In the Perl code avoid single quotes and be prepared to do some extra backslashing.)

Replies are listed 'Best First'.
Re: How to write apps for macOS/OSX in Perl!
by Anonymous Monk on Jun 15, 2018 at 19:56 UTC
    Automator! It's simultaneously awesome and horrible. It has many powers and quirks. Here are some notes on functionality and issues to watch out for:

    If you can't see code: Close the Library.

    If you can't see Library items: Click "hide description" icon bottom left (or resize its frame).

    The Hammer Icon checks syntax and indents your code (if syntax passed). It also does odd things sometimes like rename all instances of a particular uppercase variable to lowercase (but only some variables).

    Debug Applescript with try blocks that propogate the error:

    try something on error oops # <- oops is my pet name for the error msg :-) display alert oops as critical # let's see wtf display alert MyData # examine some data structure #return # maybe exit too, while debugging end try
    Debug Perl the usual ways but, I have to mention something WEIRD about shell. It seems like an Automator bug but who knows. Sometimes the code inside a do shell script stops working for no apparent reason. Nothing is wrong with the previously working code, it was not changed, and it does not work. The solution to this crazy problem is equally crazy: erase the problem code and rewrite the do shell script part by actually typing, then paste your Perl back into place.

    Automator has a spectacular built-in Time Machine (that every program should have)! You can restore any version of the file. It's simply magnificent.

    Be very careful about using the Play button to test scripts. If you have an infinite loop it can get stranded. You may be able to break out with dot (Command .) or you may have to kill Automator. Luckily the revision system means you'll lose never lose data if it crashes or gets killed. It usually works fine but when Automator crashes on me it's always when going back through multiple levels of undo (Command Z). That crash can be ignored but you have to restart the program to fix undo.

    Background apps made with Automator get a cool free animated gear icon in the system menubar! Click the gear to exit or, in some cases, monitor its progress. Love your Gear!

Re: How to write apps for macOS/OSX in Perl! Part 1: Perl ASN Check (Revised)
by Anonymous Monk on Jun 16, 2018 at 16:33 UTC
    (* Demonstration MacOS/OSX app in AppleScript and Perl *) (* Posted to perlmonks.org by Anonymous Monk 6/16/2018 *) (* Node: How to write apps for macOS/OSX in Perl! Part 1: Perl ASN Che +ck (Revised) *) (* Changes: Refactored hard-coded variables and improved comments. *) set TITLE to "Perl ASN Check" set PROMPT to "Autonomous System Number: (Example: 714 is Apple Inc. 666 does not exist. Blank to exit.)" set ASQUERY to "https://freeapi.robtex.com/asquery/" # EVENT LOOP repeat # CONFIG LOOP repeat try # LET USER SET A VARIABLE set ASN to text returned of (display dialog PROMPT with ti +tle TITLE default answer "" buttons {"Check"} default button 1) # VERIFY NUMERIC INPUT, OR ELSE: ERROR set ASN to ASN as number # require a number exit repeat # continue if ASN is numeric on error # not a number? # CRITICAL ALERTS HAVE A SCARY ICON display alert "Please enter an Autonomous System Number!" +as critical end try # END CONFIG end repeat if ASN is equal to 0 then return # exit if blank # ALL MACS HAVE PERL BABY! # SEND PERL STDOUT TO APPLESCRIPT VARIABLE set RES to do shell script " perl -MHTTP::Tiny -e ' my $r = HTTP::Tiny->new->get(qq~" & ASQUERY & ASN & "~); if (length $r->{content}) { $r->{content} =~ /[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+/ ? print q~ASN (" & ASN & ") Exists!~ : print q~ASN (" +& ASN & ") Not Found!~; } else { print q~Download failed! (" & ASQUERY & ")~ } ' " # PRINT PERL STDOUT TO DESKTOP ALERT VIA APPLESCRIPT VAR display alert RES end repeat
Re: How to write apps for macOS/OSX in Perl!
by Anonymous Monk on Jun 17, 2018 at 16:04 UTC