CUFP
usemodperl
This perl code compiles and runs an apple mac app
that decompiles and prints its own applescript
source code:
<code>
perl -Mautodie -we '$app="ApplePerlQuine\@perlmonks.org.app";die"not apple mac"unless${^O}eq"darwin";open$f,"|-","osacompile -o $app";print$f qq~set myPATH to path to me as string\nset myPATH to myPATH & "Contents:Resources:Scripts:main.scpt"\nset myPATH to do shell script"echo " & myPATH & " | tr : / | sed -E \x27s/Macintosh HD//\x27"\ndisplay dialog (do shell script ("osadecompile " & myPATH)) with title "$app" buttons {"Use Perl!"} default button 1\n~;close$f;system("open $app")'
</code>
<readmore>
<br>
<b>Easier to read:</b>
<pre>
perl -Mautodie -we '$app="ApplePerlQuine\@perlmonks.org.app";
die"not apple mac"unless${^O}eq"darwin";
open$f,"|-","osacompile -o $app";
print$f qq~
set myPATH to path to me as string\n
set myPATH to myPATH & "Contents:Resources:Scripts:main.scpt"\n
set myPATH to do shell script"echo " & myPATH & " | tr : / | sed -E \x27s/Macintosh HD//\x27"\n
display dialog (do shell script ("osadecompile " & myPATH)) with title "$app" buttons {"Use Perl!"} default button 1\n
~;
close$f;
system("open $app")'
</pre>
<b>Easier to understand:</b>
<br>
<br>
1. Perl setup:
<ul><pre>
perl -Mautodie -we '$app = "ApplePerlQuine\@perlmonks.org.app";
die "not apple mac" unless ${^O} eq "darwin";
</pre></ul>
2. Open pipe to applescript compiler and print:
<ul><pre>
open $f, "|-", "osacompile -o $app"; print$f qq~
</pre></ul>
3. Set applescript variable to the app path:
<ul><pre>
set myPATH to path to me as string\n
</pre></ul>
4. Append path of compiled source (inside the app):
<ul><pre>
set myPATH to myPATH & "Contents:Resources:Scripts:main.scpt"\n
</pre></ul>
5. Use shell to manipulate the path:
<ul><pre>
set myPATH to do shell script"
</pre></ul>
6. Change applescript variable with tr and sed:
<ul><pre>
echo " & myPATH & " | tr : / | sed -E \x27s/Macintosh HD//\x27"\n
</pre></ul>
7. Decompile app and display source code:
<ul><pre>
display dialog (do shell script ("osadecompile " & myPATH)) with title "$app" buttons {"Use Perl!"} default button 1\n
</pre></ul>
8. Back to perl, end print, close pipe, open app:
<ul><pre>
~; close$f; system("open $app")'
</pre></ul>
Summary: lines 1-2 and 8 use perl to write the app with applescript in lines 3-7.
<br><br>
<b>As a script:</b>
<p>
<code>
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
die "not apple mac" unless ${^O} eq "darwin";
my $app = "ApplePerlQuine\@perlmonks.org.app";
open my $f, "|-", "osacompile -o $app";
print $f qq~set myPATH to path to me as string
set myPATH to myPATH & "Contents:Resources:Scripts:main.scpt"
set myPATH to do shell script "echo " & myPATH & " | tr : / | sed -E \x27s/Macintosh HD//\x27"
display dialog (do shell script ("osadecompile " & myPATH)) with title "$app" buttons {"Use Perl!"} default button 1\n~;
close $f;
system("open $app");
</code>
<p><hr><p>
This one prints itself entirely, including the perl:
<code>
perl -Mautodie -we '$app=qq(ApplePerlQuine\@perlmonks.org.app);die"not apple mac"unless${^O}eq"darwin";open$f,qq(|-),qq(osacompile -o $app);print$f qq(set myPATH to path to me as string\nset myPATH to myPATH & "Contents:Resources:Scripts:main.scpt"\nset myPATH to do shell script"echo " & myPATH & " | tr : / | sed -E \x27s/Macintosh HD//\x27"\ndisplay dialog (\"perl -Mautodie -we \x27\x24app=qq(ApplePerlQuine\134\134\100perlmonks.org.app);\nopen\x24f,qq(|-),qq(osacompile -o \x24app);\nprint\x24f qq\(\" & do shell script ("osadecompile " & myPATH)) & "\);close\x24;system\(\x27\x27open \x24app\x27\x27\)" with title "$app" buttons {"Use Perl!"} default button 1\n\(* This is an ASCII representation of the Perl source used to make this app. It will not cut and paste because of hex and octal tricks. See perlmonks.org/?node=1216984 for details. *\));close$f;system("open $app")'
</code>
<div class="pmsig"><div class="pmsig-1216830">
<sub><b>STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!</b>—[https://www.cpan.org/|CPAN]</sub>
</div></div>