• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Xcode 4 editor scripting - help!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Xcode 4 editor scripting - help!


  • Subject: Re: Xcode 4 editor scripting - help!
  • From: Brad Oliver <email@hidden>
  • Date: Wed, 29 Feb 2012 14:50:18 -0700

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;

---

--
Brad Oliver
email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: Xcode 4 editor scripting - help! (From: Matt Neuburg <email@hidden>)
 >Re: Xcode 4 editor scripting - help! (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Another feature request - anyone already put it in?
  • Next by Date: Re: static libraries - how is their "Location" in the File Inspector determined
  • Previous by thread: Re: Xcode 4 editor scripting - help!
  • Next by thread: Re: Xcode 4 editor scripting - help!
  • Index(es):
    • Date
    • Thread