views:

31

answers:

1

Hi all,

I'm trying to figure out the best way to automatically add NSLocalizedString() around a string in xcode.

Ideally I'd like a way that I could position the cursor within @"foo", press a key binding, and it'd be turned into NSLocalizedString(@"foo", nil).

I've had a look at the documentation for user scripts and can't see an obvious way to get the current cursor position.

Did I miss something, or is there another way to achieve the same result?

Thanks!

+2  A: 

You can use %%%{PBXSelectionStart}%%%

From the apple documentation:

Getting Text from the Active Window
These variables are replaced by text in the active window:

  • %%%{PBXSelectedText}%%% is replaced by the selected text in the active text object.
  • %%%{PBXAllText}%%% is replaced by the entire text in the active text object.

Getting Information on the Contents of the Active Window
These variables are replaced by information on the text in the active window:

  • %%%{PBXTextLength}%%% is replaced by the number of characters in the active text object.
  • %%%{PBXSelectionStart}%%% is replaced by the index of the first character in the selection in the active text object.
  • %%%{PBXSelectionEnd}%%% is replaced by the index of the first character after the selection in the active text object.
  • %%%{PBXSelectionLength}%%% is replaced by the number of characters in the current selection in the active text object.

Procrastination brought you this script.
It works and does what it should. But it is very basic, and there are bugs, and this is probably not the best way to do it.
Don't use @ and " in the strings you want to replace. If I were you, I wouldn't use it anyway. ^^
Script Input is Selection, Output is Replace Document Contents

#!/bin/sh
if [ %%%{PBXSelectionLength}%%% -gt 0 ]
  then
    echo "This does not work if you select text. Put your cursor inside a String." >&2
    exit
fi
Source=`cat "%%%{PBXFilePath}%%%"`
SelectionStart="%%%{PBXSelectionStart}%%%"
SelectionEnd="%%%{PBXSelectionEnd}%%%"
BOOL=1
StringStart=$SelectionStart
StringStop=$SelectionEnd
while [ $BOOL -eq 1 ]
 do
  tmpText=`echo "${Source:${StringStart}:1}"`
  if [ "$tmpText" = "@" ]
   then BOOL=0
   else StringStart=$(($StringStart - 1))
  fi
done
BOOL=1
while [ $BOOL -eq 1 ]
 do 
  tmpText=`echo "${Source:${StringStop}:1}"`
  if [ "$tmpText" = "\"" ]
   then BOOL=0
  fi   
  StringStop=$(($StringStop + 1))
done

StringToReplace=`echo ${Source:${StringStart}:$(($StringStop - $StringStart))}`
ReplacementString="NSLocalizedString($StringToReplace,nil)"
echo -n "${Source:0:${StringStart}}"
echo -n "$ReplacementString"
echo -n "${Source:${StringStop}}"
fluchtpunkt
Thanks! I'd not appreciated that PBXSelectionStart had that meaning when I read the documentation. I wasn't expecting a whole script - works very nicely - the only downside is that (at least in xcode 3.2.4) it seems to result in the cursor being placed at the end of the document when you use it - any idea if there's a way to avoid that?
JosephH
if you don't select anything you have to replace the whole document, so the cursor will be at the end of the document. No idea how to change this. To be honest, these were my first steps with xcode user scripts ^^
fluchtpunkt
Ah right, I see - thanks! It's also my first time looking at the user scripts, I only discovered they existed this week :)
JosephH