我正在尝试找出在 xcode 中的字符串周围自动添加 NSLocalizedString() 的最佳方法。
理想情况下,我希望有一种方法可以将光标定位在 @"foo"
内,按下按键绑定(bind),然后它会变成 NSLocalizedString(@"foo",无)
。
我查看了用户脚本的文档,但没有找到获取当前光标位置的明显方法。
我错过了什么,还是有其他方法可以达到相同的结果?
谢谢!
最佳答案
您可以使用%%%{PBXSelectionStart}%%%
来自苹果文档:
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.
拖延给你带来了这个脚本。
它有效并且做了它应该做的事情。但这是非常基本的,并且存在错误,这可能不是最好的方法。
不要在要替换的字符串中使用 @ 和 "。如果我是你,我无论如何也不会使用它。^^
脚本输入是选择
,输出是替换文档内容
#!/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}}"
关于objective-c - 如何编写 xcode 用户脚本以使用 NSLocalizedString(<string>, nil) 包围光标所在的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3916092/