The xed
tool works fine:
xed --line 100 /Users/Anne/Desktop/Test/TestAppDelegate.m
Error
for example: the specifier requested a third, but there are only 2
The error above simply indicates that the requested string is out of range.
Decision
Check if line number really exists before xed is executed.
Quickly written example
// 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]; }
Note
Remember to count the number of lines correctly: Counting lines of text
source share