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}}"