Tags

    Aperture MetaWeblog Plugin: Showing progress

    Comments

    T

    Changing the plug-in

    In order to show progress, we need to initialize the number of steps in the exportManagerShouldBeginExport method and update it during the exportManagerShouldWriteImageData method. Add the following code snippet to the exportManagerShouldBeginExport method.

    - (void)exportManagerShouldBeginExport
    {
     // create the blog instance
     _blog = [[MetaWeblog alloc] init];
     [_blog setUrl: [blogAccessPointTextField stringValue]];
     [_blog setBlogId: [blogIdTextField stringValue]];
     [_blog setUsername: [blogUserNameTextField stringValue]];
     [_blog setPassword: [blogPasswordTextField stringValue]];
      
     // save settings
     NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     NSString* pluginIdentifier = [[[NSBundle bundleForClass: [self class]] infoDictionary] objectForKey: @"CFBundleIdentifier"];
     NSMutableDictionary* settings = [NSMutableDictionary dictionaryWithDictionary: [defaults persistentDomainForName:pluginIdentifier]];
     [settings setValue: [_blog url] forKey: @"accessPoint"]; [settings setValue: [_blog blogId] forKey: @"blogId"]; [settings setValue: [_blog username] forKey: @"username"]; [settings setValue: [_blog password] forKey: @"password"]; [defaults setPersistentDomain:settings forName:pluginIdentifier]; 
     [defaults synchronize];
     [pool release];
     
     // initialize the progress
     unsigned long count = [_exportManager imageCount];
     [self lockProgress];
     exportProgress.totalValue = count * 3;
      exportProgress.currentValue = 0;
     exportProgress.message = @"Exporting...";
     [self unlockProgress]; 
     [_exportManager shouldBeginExport];
    }
    

    One thing to note is that we set the total number of steps (totalValue) to the number of images being exported times three. Why? Well, it takes a while for Aperture to scale and export the image, another for us to upload the image to the server, and can even take a few seconds to create a post. In order to make it easier to track progress we know there are three times the steps than there are images. Chances are that as the plug-in becomes more sophisticated, more steps will be added.

    The next step is to add a method to update progress. First, add the updateProgress method prototype to MetaWeblogPlugIn.h:

    #import <Cocoa/Cocoa.h>
    #import <Foundation/Foundation.h>
    #import "ApertureExportManager.h"
    #import "ApertureExportPlugIn.h"
    #import "MetaWeblog.h"
    
    @interface MetaWeblogPlugIn : NSObject <ApertureExportPlugIn>
    {
     id _apiManager; 
     NSObject<ApertureExportManager, PROAPIObject> *_exportManager; 
     NSLock *_progressLock;
     NSArray *_topLevelNibObjects;
     MetaWeblog* _blog;
     ApertureExportProgress exportProgress;
     IBOutlet NSView *settingsView;
     IBOutlet NSView *firstView;
     IBOutlet NSView *lastView;
     IBOutlet NSTextField *blogAccessPointTextField;
     IBOutlet NSTextField *blogUserNameTextField;
     IBOutlet NSSecureTextField *blogPasswordTextField;
     IBOutlet NSTextField *blogIdTextField;
    }
    
    - (void)updateProgress: (NSString*) msg;
    
    @end
    

    In MetaWeblogPlugIn.m, lets implement the method

    - (void)updateProgress: (NSString*) msg
    {
     [self lockProgress];
     [msg retain];
     exportProgress.currentValue += 1;
     exportProgress.message = msg;
     [self unlockProgress]; 
    }
    

    This method allows us to increment the current progress by one and set a message for that step. To use the method, lets update the exportManagerShouldWriteImageData method to call it whenever it is about to do something:

    - (BOOL)exportManagerShouldWriteImageData:(NSData *)imageData toRelativePath:(NSString *)path forImageAtIndex:(unsigned)index
    {
     NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
     
     NSDictionary* properties = [_exportManager propertiesForImageAtIndex: index];
     NSString* versionName = [properties objectForKey:@"kExportKeyVersionName"];
     NSString* name = [path lastPathComponent];
     [self updateProgress: [NSString stringWithFormat: @"Uploading %@", versionName]];
    
     NSString* url = [_blog newMediaObject: name andType: @"image/jpeg" andBits: imageData];
     if(url != nil)
     {
      NSLog(@"Uploaded image %@", url);
         
      Post* post = [[Post alloc] init];
      post.title = versionName;
      post.description = [NSString stringWithFormat: @"<img src="%@" />", url];
      post.dateCreated = [NSCalendarDate init];
      
      [self updateProgress: [NSString stringWithFormat: @"Creating post for %@", versionName]];
      NSString* postId = [_blog newPost: post];
      NSLog(@"Created post with id %@", postId);
      
      [post release];
     }
     else
     {
      NSLog(@"Failed to upload the image '%@'", path);
     }
     
     [self updateProgress: @"Exporting..."];
     [pool release];
     return FALSE; 
    }
    

    The above code is rather straight forward. We simply update progress before we upload the image to the server. Once that is done, we update the progress before we create a post and finally we set it to the default "Exporting..." for the next image.

    Testing

    To test, rebuild and redeploy the plug-in. Restart Aperture and export ten or more images. You should now be able to see progress as Aperture exports and then uploads the image, before creating a post for it

    Conclusion

    In this installment we simply provide the user with feedback in terms of the steps we are taking while exporting images to a blog using the MetaWeblog API.