header

Torsten Curdt’s weblog

Current page from Safari

Opening a web page in an external browser is easy in Cocoa.


 NSString *url = @"https://vafer.org/blog";
 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];

If you want to get the URL of the current web page this is a little more complicated. In fact you need to talk to the browser. Safari (like many other applications) can be accessed easily with AppleScript.


- (NSString*) safariURL
{
    NSDictionary *dict;
    NSAppleEventDescriptor *result;

    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
        @"\ntell application \"Safari\"\n\tget URL of document 1\nend tell\n"];

    result = [script executeAndReturnError:&dict];

    [script release];

    if ((result != nil) && ([result descriptorType] != kAENullEvent)) {
        return [result stringValue];
    }

    return nil;
}

It’s also pretty easy to add AppleScript support to you own Cocoa application. I thought I came across a screencast covering this topic but can’t find the link anymore. But check out the following resources if you want to get deeper into this: [1] [2] [3] [4]

Requests per Second from Log

While this is not nearly as fancy as Theo’s realtime version with DTrace this little script extracts your requests per second (and minutes) from Apache log files. Useful if you didn’t have monitoring set up already and want to know about the amount of request you were getting – in retrospect. As you most likely will have a log file it’s easy to extract the data from there. I’ve just use the log file parser from my other post and added some grouping and counting.


parser = LogParser.new

current_key = 0
count = 0

while STDIN.gets
  line = $_

  parsed_data = parser.parse_line(line)  

  parsed_data[:datetime] =~ %r{(\d{2})/(\w{3})/(\d{4}):(\d{2}):(\d{2})}
  day, month, year, hour, minute = $1, $2, $3, $4, $5

  key = Time.mktime(year, month, day, hour, minute, 0).to_i

  if key != current_key

    if count > 0
        # timestamp, request/minute, requests/second
        printf "%d %d %d\n", key, count, count/60
    end

    count = 0
    current_key = key
  end

  count = count + 1
end

Now just pipe in the log data and you get a nice aggregation that can easily be injected into a round robin database for example.

$ cat access.log | ./request.rb

HttpServletRequest Examples

What was the semantic of the servlet request parameter again? I can’t remember how often I wrote code that produced something along the lines of the following.

The direct servlet mapping:

GET http://localhost:8080/statement
contextPath:
pathInfo:null
pathTranslated:null
requestUri:/statement
servletPath:/statement
queryString:null

With trailing slash:

GET http://localhost:8080/statement/
contextPath:
pathInfo:/
pathTranslated:/path/to/webapp
requestUri:/statement/
servletPath:/statement
queryString:null

With path component:

GET http://localhost:8080/statement/test
contextPath:
pathInfo:/test
pathTranslated:/path/to/webapp/test
requestUri:/statement/test
servletPath:/statement
queryString:null

With query string:

GET http://localhost:8080/statement/test?as=true
contextPath:
pathInfo:/test
pathTranslated:/path/to/webapp/test
requestUri:/statement/test
servletPath:/statement
queryString:as=true

Just for the records.

ApacheCon US 2008 recap

steamboatThis year’s ApacheCon US was quite amazing – in several ways.

From the city point of view I am quite thankful that it gave me the opportunity to experience New Orleans – a little bit of the South. I wish I had seen more. Surprisingly the French Quarter just didn’t feel like the US at all. Overall I think it was also the best food I ever had during any of the ApacheCons. (Only counting dinners of course.) It was also very cool to experience the presidential elections – and how relieved people were about the results. Over the past years the socializing part of ApacheCon has become much more important to me. But this year I also didn’t want to miss some of the talks. Especially Theo Schlossnagle’s talk about DTrace was just awesome. (I did know DTrace is cool – but this session blew me away). The video is supposed to be available soon. The after hour activities were a bit less for me this time. I had quite some problems with jetlag – more than ever before. What a bad timing! At least I wasn’t alone with this. My presentation was at the very last slot and still some people showed up. Even more than expected. (The slides are available now on slideshare.)

Unfortunately I didn’t take as much pictures as hoped. But the few are available in my ApacheCon US set on flickr.

User Idle Detection

This question came up a couple of times recently. I came across this a while ago.

There is a function called CGSSecondsSinceLastInputEvent that is not defined in any headers. So while this is a private API it seems to work fine on Tiger and Leopard. Just declare the prototype and you can easily find out when the user last provided input. That is moved the mouse or pressed a key.

double CGSSecondsSinceLastInputEvent(long evType);
double idleTime = CGSSecondsSinceLastInputEvent(-1);


Of course there is no guaranty this will work in future versions of OSX as well. So if you know an official way of doing that – please let me know.

Update: Apparently there is also a way of finding out through the public API. Thanks to Michael for posting this on cocoa-dev.