Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to create GUI for a perl script on MAC?/Apple Script and Perl?

by hary536 (Novice)
on Jan 18, 2013 at 01:25 UTC ( #1013934=perlquestion: print w/ replies, xml ) Need Help??
hary536 has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have a perl script which accepts command line arguments like folder locations and some other yes/no switches. It is meant for MAC users.

I want to create some kind of simple GUI so that you can enter those command line arguments in the GUI instead of running the perl script from command line.

I came across Platypus, but seems, I can't pass multiple arguments using platypus. I don't know Objective-C or Cocoa. I know a bit of AppleScript.

I am wondering, is there a way to get user input through AppleScript and then pass that input as arguments to my perl script? I know, you can get user inputs through Apple Script, but don't know how to pass them as arguments to my perl script.

Any help is appreciated. Thanks.

Example of how my script accepts command-line arguments:

MyScript.pl -s PathToSoruceFolder -t PathtoDestinationFolder -c PathtoSettingsFolder -b Filenamestartswith -1 "YYYY/MM/DD HH:MM"(process files created after this time) -R 1 -W 1 -S 0 -E 1 Where 0 & 1 are switches for different things in my script.

Comment on How to create GUI for a perl script on MAC?/Apple Script and Perl?
Re: How to create GUI for a perl script on MAC?/Apple Script and Perl?
by Anonymous Monk on Jan 18, 2013 at 01:48 UTC
Re: How to create GUI for a perl script on MAC?/Apple Script and Perl?
by tobyink (Abbot) on Jan 18, 2013 at 07:25 UTC

    If you can persuade the Wx module to install on your system (WxWidgets does work on Mac, so it should theoretically, and according to the CPAN testers service there are successful install reports from Mac OS X), then you could investigate my Ask module. It's as simple as:

    use Ask qw( file_selection info ); my $file = file_selection("Please choose a file!"); info("You chose: $file");

    Then Ask does the rest. If the user has run your script through the Mac terminal, Ask should detect this, and to input and output via the terminal. If they've launched your script by, say, double clicking it in Finder, it will notice there's no terminal and interact with the user via dialog boxes.

    I do keep meaning to write a blogs.perl.org post about Ask.

    Update: blog post written... Ask not what your user can do for you...

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      I don't know how often i failed to install Wx on different platforms...

      Last try:

      It's slightly annoying...

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Re: How to create GUI for a perl script on MAC?/Apple Script and Perl?
by karlgoethebier (Curate) on Jan 18, 2013 at 14:42 UTC

    Like this:

    AppleScript

    set theWindowTitle to "PerlMonks" set theOption to text returned of (display dialog "Enter a Source fold +er:" & return default answer "" with title theWindowTitle) set thePerlScript to quoted form of (POSIX path of ((choose file with +prompt "Select a script:") as Unicode text)) set theResult to do shell script thePerlScript & " -S " & theOption display dialog "Result: " & theResult with title theWindowTitle

    Perl

    #!/usr/bin/perl use strict; use warnings; use Getopt::Long; Getopt::Long::Configure("ignore_case"); my %options; GetOptions(\%options, "SourceFolder=s"); print $options{SourceFolder};

    Update: see also Getopt::Long

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Thanks Karl, I didn't notice your post until later today. I spent quite a bit of time on friday and saturday researching for my problem solution. Came across this Apple tech note, which helped some. http://developer.apple.com/library/mac/#technotes/tn2065/_index.html Then I wrote following code to see if it will work:
      set cfgfolder to choose folder with prompt "Choose the Configs folder: +" default location alias "MacHD:Folder1:Folder2:Configs:" set cfgposix to POSIX path of cfgfolder set sourcefolder to choose folder with prompt "Choose the source fold +er:" default location alias "MacHD:Folder3:Folder4" set sourceposix to POSIX path of sourcefolder set targetfolder to choose folder with prompt "Choose the Target folde +r location:" default location alias "MacHD:TargetFolder" set targetposix to POSIX path of targetfolder do shell script "/usr/bin/perl /PathToPerlScript &> '/Logs/Reprocesslo +g.txt'" & " -t " & targetposix & " -s " & sourceposix & "-c" & cfgpos +ix set nextdate to do shell script "date" & " +%y%m%d_%H%M%S" # some more code will go here to open the log file in console, so that + one can see the log updates in real time.

      Now, I can pass the arguments to my script, but it doesn't run in background. Hence, the next AppleScript statement doesn't execute until the perl script finishes execution.

      Since, then I have tried several quote combinations to make it work and I thought, I succeeded yesterday night but today morning, couldn't figure out what I did that made it work.

      Now, after several trials, I only face two outcomes:

      Either perl script runs in background(but doesn't accept the arguments passed to it) or it accepts the arguments passed to it, but doesn't run in background.

      Have hit a roadblock.

      Also, there is one more challenge with this method and that is I can run multiple instances of this script and each instance will create it's own log file. I want to be able to kill/stop any instance out of the several running ones. Challenge is how to do to it via Applescript so that user can select the exact instance that they want to stop.

      Any help/advice from anyone is appreciated. Thanks. PS: My perl script is not using Getopt:Long as of now, but I will look into it.

        This snippet reads a option, does the backgrounding and reads the pid of the current perlscript for killing it...

        set theWindowTitle to "PerlMonks" set theOption to text returned of (display dialog "Enter a sourceFolde +r:" & return default answer "" with title theWindowTitle) set thePerlscript to quoted form of (POSIX path of ((choose file with +prompt "Select a script:") as Unicode text)) set cmd to thePerlscript & " -S " & theOption & " &> /dev/null" & " & + echo $!" display dialog "Command: " & cmd with title theWindowTitle do shell script cmd set pid to the result display dialog "PID: " & pid with title theWindowTitle display dialog "Kill the proccess (" & pid & ")" buttons {"OK"} with t +itle theWindowTitle do shell script ("kill " & pid)

        I hope this helps a little bit to work it out...

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013934]
Front-paged by Lotus1
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-09-18 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (127 votes), past polls