Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

ApplescriptObjc Detect Title in HTML Code

I am working on an HTML editor, and it's going very well! My next hurdle in the project is detecting the title, if present, in the code. I can get the input HTML code as text. I would really appreciate it if someone could help me come up with a way to extract the title from the code. For example, <title>Hello World</title> might be present, and I want to be able to extract 'Hello World'. Thanks for any help!

MacBook Pro (Retina, 13-inch, Mid 2014), OS X El Capitan (10.11.3)

Posted on Feb 10, 2016 12:10 PM

Reply
4 replies

Feb 11, 2016 8:02 AM in response to VikingOSX

I have adapted code found on stackoverflow that uses NSScanner, and not NSAppleScript:

User uploaded file


In cv.html, <title>HTML5 Canvas • Text on an Arc</title>:

Result: 2016-02-11 10:52:06.024 scanit[1155:1046188] HTML5 Canvas • Text on an Arc

#import <Foundation/Foundation.h>



// http://stackoverflow.com/questions/28204380/objective-c-using-nsscanner-to-obtai n-body-from-html



int main(int argc, const char * argv[]) {



@autoreleasepool {



NSString *startTag = @"<title>";

NSString *endTag = @"</title>";



NSString *path = @"~/Desktop/cv.html";

NSString *stdPath = [path stringByStandardizingPath];

NSString *htmlContent = [NSString stringWithContentsOfFile:stdPath

encoding:NSUTF8StringEncoding

error:NULL];



// NSScanner* newScanner = [NSScanner scannerWithString:@"<title>HTML5 Canvas • Text on an Arc</title>"];

NSScanner* newScanner = [NSScanner scannerWithString:htmlContent];

NSString *titleText;

while (![newScanner isAtEnd]) {

[newScanner scanUpToString:startTag intoString:NULL];

// step over initial title tag

[newScanner scanString:startTag intoString:NULL];

[newScanner scanUpToString:endTag intoString:&titleText];

}

NSLog(@"%@", titleText);

}

return 0;

}

ApplescriptObjc Detect Title in HTML Code

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.