Open files on drag and drop (creating a droplet application)

  • I am starting my journey into Objective-C/Cocoa development. I hope I
    might find sympathetic ears in this forum.

    One of my favorite ways of interacting with files is dragging &
    dropping items onto the application icon, and since that is how I
    write most of my applications (OOP AppleScript/ASS), I thought that
    would be a great way to start developing ObjC-Cocoa.

    By modifying the document types pList, I was able to make the Cocoa
    Application highlight and launch when defined files are dragged over
    it. Great start!

    Then, using a class provided by Wilfredo Sánchez Vega's demo program
    "DropScript", I was able to run the program successfully by using the
    File --> Open command through a Cocoa connection. This is
    encouraging. I can locate a file in the browser, select it and a
    shell script will gzip the file! Neat!

    (please see the code... http://www.wsanchez.net/papers/DropScript/)

    The problem is that I cannot make the app function with the drag &
    drop feature! My understanding is that the following line of code is
    supposed to feed the array of paths into the class, but apparently
    nothing seems to happen.

      - (BOOL) application:(NSApplication *) anApplication openFiles:
    (NSArray *) filenames {

    [self runScriptWithFiles: [NSArray arrayWithObject: filenames]];

    return YES;

    }

    I have also used this code with a single file drop as well, but with
    no luck.

    - (BOOL) application:(NSApplication *) anApplication openFile:
    (NSString *) aFileName {

    // [self runScriptWithFiles: [NSArray arrayWithObject: filenames]];

        [self runScriptWithFiles: [NSArray arrayWithObject: aFileName]];

    return YES;

    }

    Since drag & drop is such a basic feature of MacOS programs, I
    thought you guys could steer me in the right direction. Seems I'm at
    a stopping point far too early in the learning process!

    Thanks for your help!

    -- Kevin Muldoon
  • Am 01.09.2006 um 20:21 schrieb Kevin Muldoon:
    > - (BOOL) application:(NSApplication *) anApplication openFiles:
    > (NSArray *) filenames {
    >
    > [self runScriptWithFiles: [NSArray arrayWithObject: filenames]];
    >
    > return YES;
    >
    > }

      filenames is already an array. You don't need to wrap it in an
    array again. Also, if you provide no openFiles method, it will
    repeatedly call the openFile variant, so provide either, not both.

    > I have also used this code with a single file drop as well, but
    > with no luck.
    >
    > - (BOOL) application:(NSApplication *) anApplication openFile:
    > (NSString *) aFileName {
    >
    > // [self runScriptWithFiles: [NSArray arrayWithObject: filenames]];
    >
    > [self runScriptWithFiles: [NSArray arrayWithObject: aFileName]];
    >
    > return YES;
    >
    > }

    Check with the debugger that these methods are actually being called.
    Just set a breakpoint by clicking in the gutter to the left of your
    runScriptWithFiles line, and then run your app in the debugger
    ("Build and Debug"). If your breakpoints aren't triggered, you're
    either running a release ("deployment") build (which *never* breaks
    on breakpoints), or you didn't set up the object that has these
    handlers as the application delegate. NSApplication will *only* send
    this message to the application delegate. Not to any other objects.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
previous month september 2006 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
Go to today