Steve Brown
Steve Brown

Reputation: 1416

How can I access the Apple Events "openFile" event in C++?

I'm interested in writing a small utility in C++ for Mac OS X to read, parse, save (over)write a file. I don't need any GUI, menus, or windows.

  1. What type of project template do I need to start with in XCode?
  2. How can I access the file that is passed in? (It's passed with Apple Events openFile, right?)

I've done a little C++ but nothing on Mac. Links appreciated, code samples appreciated more.

Upvotes: 5

Views: 1354

Answers (2)

Darren
Darren

Reputation: 25619

How do you intend to pass files to your application?

If it's via the command line then you would use the Command Line Tool template and access the command line parameters just as you would on any POSIX platform (argc and argv).

If you want to pass files to your application using Finder, say, by dropping files onto the application icon, then you would use the Cocoa Application template.

The Info.plist file contains your application configuration and supported document types, similar to the registry on Windows.

  1. You configure Info.plist via the "Info" tab of your Project Settings (It's the top-most file in the file navigator in XCode). Click the "Add" button in the lower right, then select "Add Document Type" to add a document type that your application will accept. To accept all documents, set the document name to All and set the extension to *. More info is here.

  2. Add a LSUIElement key in your Info.plist, and set its value to YES to indicate that your application has no UI. This key is also displayed as "Application is agent" in XCode. More info on LSUIElement is here.

  3. In your MainMenu.xib, you can delete the Window and Font Manager objects that are there by default, since you won't be needing them.

  4. Rename the AppDelegate.m file to AppDelegate.mm, so that it's compiled as Objective-C++. This will allow you to use C++ code in that file.

  5. In the applicationDidFinishLaunching: delegate method, add [NSApp terminate:nil]; so that your app quits immediately when it's done its work.

Add the following method to AppDelegate.mm:

- (BOOL)application:(NSApplication*)app openFile:(NSString *)filename
{
    NSLog(@"Opening file %@", filename);

    char* cFilename = [filename UTF8String];    

    // Your C++ code goes here

    return YES;
}

That's it. The rest is your C++ code. You can add any C++ code to AppDelegate.mm that you want. E.g.:

#include <string>
#include <iostream>
#include "MyCppFileProcessor.h"

- (BOOL)application:(NSApplication*)app openFile:(NSString *)filename
{
    std::string cFilename([filename UTF8String]);
    std::cout << "Processing file: " << cFilename << std::endl;

    MyCppFileProcessor fileProcessor;
    fileProcessor.processFile(cFilename);

    return YES;
}

This code will run whenever you drop a document onto your Application's icon in Finder.

Upvotes: 1

Fernando Valente
Fernando Valente

Reputation: 1096

1)Use the Command Line Tool template. There are several options for this template. You may select C++ from the menu.

2)As far as I know IOstream will work just fine. Also, there's an argument parameter on your main() function, you may get the file name from these args.

http://www.cplusplus.com/reference/iostream/

Upvotes: 1

Related Questions