该xed
工具似乎工作正常:
xed --line 100 /Users/Anne/Desktop/Test/TestAppDelegate.m
错误
例如:说明符要求第 3 个,但只有 2 个
上面的错误只是表明请求的行超出了范围。
解决方案
在执行之前检查行号是否实际存在xed
。
快速编写的示例
// Define file and line number
NSString *filePath = @"/Users/Anne/Desktop/Test/TestAppDelegate.m";
int theLine = 100;
// Count lines in file
NSString *fileContent = [[NSString alloc] initWithContentsOfFile: filePath];
unsigned numberOfLines, index, stringLength = [fileContent length];
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
index = NSMaxRange([fileContent lineRangeForRange:NSMakeRange(index, 0)]);
// The requested line does not exist
if (theLine > numberOfLines) {
NSLog(@"Error: The requested line is out of range.");
// The requested line exists
} else {
// Run xed through AppleScript or NSTask
NSString *theSource = [NSString stringWithFormat: @"do shell script \"xed --line %d \" & quoted form of \"%@\"", theLine, filePath];
NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:theSource];
[theScript executeAndReturnError:nil];
}
笔记
确保正确计算行数:
Counting Lines of Text