Go to file and line from outside Xcode 4.2

For the NSLogger project, we would like to implement this function to immediately go to Xcode to the line in the file that issued the log entry. One would expect this to be easy with a command line tool like this:

xed --line 100 ~/work/xyz/MainWindowController.m 

But this leads to an unexpected error:

2011-10-31 17: 37: 36.159 xed [53507: 707] Error: Error Domain = NSOSStatusErrorDomain Code = -1728 "The operation could not be completed. (OSStatus error -1728.)" (For example: specifier requested for 3 , but there is only 2. Basically this indicates a run-time permission error.) UserInfo = 0x40043dc20 {ErrorNumber = -1728, ErrorOffendingObject =}

Another idea is to use AppleScript to tell Xcode to complete the necessary steps, but I could not find a working solution.

Therefore, any decision to achieve the desired effect would be greatly appreciated.

Link to NSLogger issue on GitHub: https://github.com/fpillet/NSLogger/issues/30

+4
source share
1 answer

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

0
source

All Articles