Skip to content

Instantly share code, notes, and snippets.

@2shortplanks
Created August 20, 2012 20:54
Show Gist options
  • Save 2shortplanks/3407819 to your computer and use it in GitHub Desktop.
Save 2shortplanks/3407819 to your computer and use it in GitHub Desktop.
Last command run in current terminal, applescript in Perl
use Mac::AppleScript qw(RunAppleScript);
########################################################################
# work out the line on the command line
########################################################################
my $text = RunAppleScript(<<'END');
tell application "Terminal"
tell front window
history of selected tab as text
end
end
END
# strip off any applescript string wrapper
$text =~ s/\A"//x;
$text =~ s/"\z//x;
$text =~ s/\\"/"/gx;
$text =~ s/\\\\/\\/gx;
# doesn't get much more inefficent than this!
my @lines = split /\n/, $text;
my $capture = "";
do {
$capture = pop(@lines) . "\n" . $capture;
} while ($lines[-1] =~ /\\\z/);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment