Revision: 24312
          http://sourceforge.net/p/bibdesk/svn/24312
Author:   hofman
Date:     2019-10-20 13:44:06 +0000 (Sun, 20 Oct 2019)
Log Message:
-----------
Run will generate cite key script hook async, perform generation in completion 
handler

Modified Paths:
--------------
    trunk/bibdesk/BDSKEditor.m
    trunk/bibdesk/BDSKScriptHookManager.h
    trunk/bibdesk/BDSKScriptHookManager.m
    trunk/bibdesk/BibDocument_Actions.m

Modified: trunk/bibdesk/BDSKEditor.m
===================================================================
--- trunk/bibdesk/BDSKEditor.m  2019-10-19 21:33:46 UTC (rev 24311)
+++ trunk/bibdesk/BDSKEditor.m  2019-10-20 13:44:06 UTC (rev 24312)
@@ -908,35 +908,38 @@
     // could use [[alert window] orderOut:nil] here, but we're using the 
didDismissSelector instead
     BDSKPRECONDITION([self commitEditing]);
        
-       NSString *oldKey = [publication citeKey];
-       NSString *newKey = [publication suggestedCiteKey];
+       NSArray *oldValues = [NSArray arrayWithObject:[publication citeKey]];
+       NSArray *newValues = [NSArray arrayWithObject:[publication 
suggestedCiteKey]];
+    NSArray *pubs = [NSArray arrayWithObject:publication];
        
        [[BDSKScriptHookManager sharedManager] 
runScriptHookWithName:BDSKWillGenerateCiteKeyScriptHookName 
-        forPublications:[NSArray arrayWithObject:publication] document:[self 
document] 
-        field:BDSKCiteKeyString oldValues:[NSArray arrayWithObject:oldKey] 
newValues:[NSArray arrayWithObject:newKey]];
-       
-       // get them again, as the script hook might have changed some values
-       oldKey = [publication citeKey];
-       newKey = [publication suggestedCiteKey];
-    
-    NSString *crossref = [publication valueOfField:BDSKCrossrefString 
inherit:NO];
-    if (crossref != nil && [crossref isCaseInsensitiveEqual:newKey]) {
-        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
-        [alert setMessageText:NSLocalizedString(@"Could not generate cite 
key", @"Message in alert dialog when failing to generate cite key")];
-        [alert setInformativeText:[NSString 
stringWithFormat:NSLocalizedString(@"The cite key for \"%@\" could not be 
generated because the generated key would be the same as the crossref key.", 
@"Informative text in alert dialog"), oldKey]];
-        [alert beginSheetModalForWindow:[self window] completionHandler:NULL];
-        return;
-    }
-       [publication setCiteKey:newKey];
-       
-       [[self undoManager] setActionName:NSLocalizedString(@"Generate Cite 
Key", @"Undo action name")];
-       [tabView selectFirstTabViewItem:self];
-       
-       [[BDSKScriptHookManager sharedManager] 
runScriptHookWithName:BDSKDidGenerateCiteKeyScriptHookName 
-        forPublications:[NSArray arrayWithObject:publication] document:[self 
document] 
-               field:BDSKCiteKeyString oldValues:[NSArray 
arrayWithObject:oldKey] newValues:[NSArray arrayWithObject:newKey]];
+        forPublications:pubs document:[self document]
+        field:BDSKCiteKeyString oldValues:oldValues newValues:newValues
+        completionHandler:^{
+        
+        // get them again, as the script hook might have changed some values
+        NSString *oldKey = [publication citeKey];
+        NSString *newKey = [publication suggestedCiteKey];
+        
+        NSString *crossref = [publication valueOfField:BDSKCrossrefString 
inherit:NO];
+        if (crossref != nil && [crossref isCaseInsensitiveEqual:newKey]) {
+            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+            [alert setMessageText:NSLocalizedString(@"Could not generate cite 
key", @"Message in alert dialog when failing to generate cite key")];
+            [alert setInformativeText:[NSString 
stringWithFormat:NSLocalizedString(@"The cite key for \"%@\" could not be 
generated because the generated key would be the same as the crossref key.", 
@"Informative text in alert dialog"), oldKey]];
+            [alert beginSheetModalForWindow:[self window] 
completionHandler:NULL];
+            return;
+        }
+        [publication setCiteKey:newKey];
+        
+        [[self undoManager] setActionName:NSLocalizedString(@"Generate Cite 
Key", @"Undo action name")];
+        [tabView selectFirstTabViewItem:self];
+        
+        [[BDSKScriptHookManager sharedManager] 
runScriptHookWithName:BDSKDidGenerateCiteKeyScriptHookName
+            forPublications:pubs document:[self document]
+            field:BDSKCiteKeyString oldValues:[NSArray arrayWithObject:oldKey] 
newValues:[NSArray arrayWithObject:newKey]];
+    }];
 }
-
+     
 - (IBAction)generateCiteKey:(id)sender{
     
     /*

Modified: trunk/bibdesk/BDSKScriptHookManager.h
===================================================================
--- trunk/bibdesk/BDSKScriptHookManager.h       2019-10-19 21:33:46 UTC (rev 
24311)
+++ trunk/bibdesk/BDSKScriptHookManager.h       2019-10-20 13:44:06 UTC (rev 
24312)
@@ -67,5 +67,6 @@
 
 - (void)runScriptHookWithName:(NSString *)name forPublications:(NSArray 
*)items document:(BibDocument *)document;
 - (void)runScriptHookWithName:(NSString *)name forPublications:(NSArray 
*)items document:(BibDocument *)document field:(NSString *)field 
oldValues:(NSArray *)oldValues newValues:(NSArray *)newValues;
+- (void)runScriptHookWithName:(NSString *)name forPublications:(NSArray 
*)items document:(BibDocument *)document field:(NSString *)field 
oldValues:(NSArray *)oldValues newValues:(NSArray *)newValues 
completionHandler:(void (^)(void))handler;
 
 @end

Modified: trunk/bibdesk/BDSKScriptHookManager.m
===================================================================
--- trunk/bibdesk/BDSKScriptHookManager.m       2019-10-19 21:33:46 UTC (rev 
24311)
+++ trunk/bibdesk/BDSKScriptHookManager.m       2019-10-20 13:44:06 UTC (rev 
24312)
@@ -122,7 +122,7 @@
         NSLog(@"No script file found for script hook %@.", name);
                return nil;
        } else {
-        BOOL sync = [name isEqualToString:BDSKWillAutoFileScriptHookName] || 
[name isEqualToString:BDSKWillGenerateCiteKeyScriptHookName];
+        BOOL sync = [name isEqualToString:BDSKWillAutoFileScriptHookName];
                NSError *error = nil;
         script = [[BDSKAppleScript alloc] initWithURL:[NSURL 
fileURLWithPath:path isDirectory:NO] synchronous:sync error:&error];
                if (script == nil) {
@@ -143,6 +143,10 @@
 }
 
 - (void)runScriptHookWithName:(NSString *)name forPublications:(NSArray 
*)items document:(BibDocument *)document field:(NSString *)field 
oldValues:(NSArray *)oldValues newValues:(NSArray *)newValues {
+    [self runScriptHookWithName:name forPublications:items document:document 
field:field oldValues:oldValues newValues:newValues completionHandler:NULL];
+}
+
+- (void)runScriptHookWithName:(NSString *)name forPublications:(NSArray 
*)items document:(BibDocument *)document field:(NSString *)field 
oldValues:(NSArray *)oldValues newValues:(NSArray *)newValues 
completionHandler:(void (^)(void))handler {
        BDSKScriptHook *scriptHook = [self newScriptHookWithName:name];
        
     if (scriptHook) {
@@ -164,8 +168,11 @@
             dispatch_async(dispatch_get_main_queue(), ^{
                 // cleanup
                 [scriptHooks removeObjectForKey:[scriptHook uniqueID]];
+                if (handler) handler();
             });
         }];
+    } else if (handler) {
+        handler();
     }
 }
 

Modified: trunk/bibdesk/BibDocument_Actions.m
===================================================================
--- trunk/bibdesk/BibDocument_Actions.m 2019-10-19 21:33:46 UTC (rev 24311)
+++ trunk/bibdesk/BibDocument_Actions.m 2019-10-20 13:44:06 UTC (rev 24312)
@@ -1324,44 +1324,44 @@
         return;
     }
     
-    NSArray *arrayOfPubs = [pubs copy]; // copy in case we were passed a 
mutable aray and the order changes
+    NSArray *arrayOfPubs = [[pubs copy] autorelease]; // copy in case we were 
passed a mutable aray and the order changes
     NSArray *arrayOfOldValues = [arrayOfPubs valueForKey:@"citeKey"];
     NSArray *arrayOfNewValues = [arrayOfPubs valueForKey:@"suggestedCiteKey"];
     
     [[BDSKScriptHookManager sharedManager] 
runScriptHookWithName:BDSKWillGenerateCiteKeyScriptHookName 
-        forPublications:arrayOfPubs document:self 
-        field:BDSKCiteKeyString oldValues:arrayOfOldValues 
newValues:arrayOfNewValues];
-    
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    
-    for (BibItem *aPub in arrayOfPubs){
-        NSString *newKey = [aPub suggestedCiteKey];
-        NSString *crossref = [aPub valueOfField:BDSKCrossrefString inherit:NO];
-        if (crossref != nil && [crossref isCaseInsensitiveEqual:newKey]) {
-            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
-            [alert setMessageText:NSLocalizedString(@"Could not generate cite 
key", @"Message in alert dialog when failing to generate cite key")];
-            [alert setInformativeText:[NSString 
stringWithFormat:NSLocalizedString(@"The cite key for \"%@\" could not be 
generated because the generated key would be the same as the crossref key.", 
@"Informative text in alert dialog"), [aPub citeKey]]];
-            [alert beginSheetModalForWindow:documentWindow 
completionHandler:NULL];
-            continue;
+        forPublications:arrayOfPubs document:self
+        field:BDSKCiteKeyString oldValues:arrayOfOldValues 
newValues:arrayOfNewValues
+        completionHandler:^{
+        
+        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+        
+        for (BibItem *aPub in arrayOfPubs){
+            NSString *newKey = [aPub suggestedCiteKey];
+            NSString *crossref = [aPub valueOfField:BDSKCrossrefString 
inherit:NO];
+            if (crossref != nil && [crossref isCaseInsensitiveEqual:newKey]) {
+                NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+                [alert setMessageText:NSLocalizedString(@"Could not generate 
cite key", @"Message in alert dialog when failing to generate cite key")];
+                [alert setInformativeText:[NSString 
stringWithFormat:NSLocalizedString(@"The cite key for \"%@\" could not be 
generated because the generated key would be the same as the crossref key.", 
@"Informative text in alert dialog"), [aPub citeKey]]];
+                [alert beginSheetModalForWindow:documentWindow 
completionHandler:NULL];
+                continue;
+            }
+            [aPub setCiteKey:newKey];
+            
+            [pool release];
+            pool = [[NSAutoreleasePool alloc] init];
         }
-        [aPub setCiteKey:newKey];
         
+        // should be safe to release here since arrays were created outside 
the scope of this local pool
         [pool release];
-        pool = [[NSAutoreleasePool alloc] init];
-    }
-    
-    // should be safe to release here since arrays were created outside the 
scope of this local pool
-    [pool release];
-    
-    [[self undoManager] setActionName:([arrayOfPubs count] > 1 ? 
NSLocalizedString(@"Generate Cite Keys", @"Undo action name") : 
NSLocalizedString(@"Generate Cite Key", @"Undo action name"))];
-    
-    arrayOfNewValues = [arrayOfPubs valueForKey:@"citeKey"];
-    
-    [[BDSKScriptHookManager sharedManager] 
runScriptHookWithName:BDSKDidGenerateCiteKeyScriptHookName 
-        forPublications:arrayOfPubs document:self 
-        field:BDSKCiteKeyString oldValues:arrayOfOldValues 
newValues:arrayOfNewValues];
-    
-    [arrayOfPubs release];
+        
+        [[self undoManager] setActionName:([arrayOfPubs count] > 1 ? 
NSLocalizedString(@"Generate Cite Keys", @"Undo action name") : 
NSLocalizedString(@"Generate Cite Key", @"Undo action name"))];
+        
+        NSArray *newValues = [arrayOfPubs valueForKey:@"citeKey"];
+        
+        [[BDSKScriptHookManager sharedManager] 
runScriptHookWithName:BDSKDidGenerateCiteKeyScriptHookName
+            forPublications:arrayOfPubs document:self
+            field:BDSKCiteKeyString oldValues:arrayOfOldValues 
newValues:newValues];
+    }];
 }    
 
 - (IBAction)generateCiteKey:(id)sender

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to