On Feb 29, 2012, at 2:02 PM, Jens Alfke wrote:
On Feb 29, 2012, at 8:33 AM, Matt Neuburg wrote: (I fear that when Jens Alfke left Apple, he took with him the last Apple-employed brain that actually understood Apple events and the OSA.)
I do have a brain, but it never fully grokked the full complexity of the OSA or especially the Text event suite. To me AppleScript has always been more of a read-only language.
Seriously, AppleScript doesn’t seem like the right language for writing scripts to manipulate a source-code editor. Ruby or even sed would be a lot better. (Didn’t Xcode 3 let you write scripts in any language that you could invoke from a menu?)
I tried to post this yesterday but ran afoul of the message size limit with an ill-timed screenshot. The gist is that I've found a solution to my original problem via the unholy trinity of Automator, Services and Perl. The catch is that I can only find a way to write scripts that manipulate the currently-selected text in Xcode 4.
To wit, I created a new Service in Automator and added a "Run Shell Script" component that used Perl, received the selected text from Xcode and checked "Output replaces selected text"
What follows is my Perl script, which takes the selected text, brackets it in an #if / #else / #endif group and adds my initials and date. I'm able to install this service and assign it to a keystroke in Xcode 4. It seems to run instantly, so that's nice. It's enough to get me moving forward for now. Not sure what I'm going to do about scripts that need to act on things other than the active selection.
---
my $outputString = "";
my @selection = <STDIN>; # read the selection from standard input
if (!@selection) { return; }; # no chars in selection, nothing to do
$dateString = `date +%d-%b-%Y`; $outputString = "#if TARGET_OS_MAC // LBO "; $outputString .= $dateString;
# Mac copy of selected text foreach my $line (@selection) { $outputString .= $line; }
# Remove extraneous EOL chomp($outputString);
$outputString .= "#else\r";
# PC copy of selected text foreach my $line (@selection) { $outputString .= $line; }
# Remove extraneous EOL chomp($outputString);
$outputString .= "#endif\r";
print $outputString;
---
|