Revision: 24219
          http://sourceforge.net/p/bibdesk/svn/24219
Author:   hofman
Date:     2019-09-17 16:16:58 +0000 (Tue, 17 Sep 2019)
Log Message:
-----------
run synchronous script hooks usimng NSAppleScript, as NSUserAppleScriptTask 
blocks when we try to run it synchronously

Modified Paths:
--------------
    trunk/bibdesk/BDSKAppleScript.h
    trunk/bibdesk/BDSKAppleScript.m
    trunk/bibdesk/BDSKScriptHookManager.m

Modified: trunk/bibdesk/BDSKAppleScript.h
===================================================================
--- trunk/bibdesk/BDSKAppleScript.h     2019-09-17 09:33:11 UTC (rev 24218)
+++ trunk/bibdesk/BDSKAppleScript.h     2019-09-17 16:16:58 UTC (rev 24219)
@@ -41,10 +41,12 @@
 
 @interface BDSKAppleScript : NSObject {
     id appleScript;
+    BOOL synchronous;
 }
 
 + (dispatch_queue_t)appleScriptQueue;
 
+- (id)initWithURL:(NSURL *)url synchronous:(BOOL)sync error:(NSError **)error;
 - (id)initWithURL:(NSURL *)url error:(NSError **)error;
 - (id)initWithSource:(NSString *)source;
 

Modified: trunk/bibdesk/BDSKAppleScript.m
===================================================================
--- trunk/bibdesk/BDSKAppleScript.m     2019-09-17 09:33:11 UTC (rev 24218)
+++ trunk/bibdesk/BDSKAppleScript.m     2019-09-17 16:16:58 UTC (rev 24219)
@@ -57,7 +57,7 @@
 
 + (void)initialize {
     BDSKINITIALIZE;
-    NSUserAppleScriptTaskClass = NSClassFromString(@"NSUserAppleScriptTask");
+    //NSUserAppleScriptTaskClass = NSClassFromString(@"NSUserAppleScriptTask");
 }
 
 + (dispatch_queue_t)appleScriptQueue {
@@ -79,10 +79,10 @@
     return [NSError errorWithDomain:@"NSAppleScriptErrorDomain" code:code 
userInfo:userInfo];
 }
 
-- (id)initWithURL:(NSURL *)url error:(NSError **)error {
+- (id)initWithURL:(NSURL *)url synchronous:(BOOL)sync error:(NSError **)error {
     self = [super init];
     if (self) {
-        if (NSUserAppleScriptTaskClass) {
+        if (sync == NO && NSUserAppleScriptTaskClass) {
             appleScript = [[NSUserAppleScriptTaskClass alloc] initWithURL:url 
error:error];
         } else {
             NSDictionary *errorDict = nil;
@@ -90,6 +90,7 @@
             if (appleScript == nil && error != NULL)
                 *error = errorFromDictionary(errorDict);
         }
+        synchronous = sync;
         if (appleScript == nil) {
             [self release];
             self = nil;
@@ -98,10 +99,16 @@
     return self;
 }
 
+- (id)initWithURL:(NSURL *)url error:(NSError **)error {
+    self = [self initWithURL:url synchronous:NO error:error];
+    return self;
+}
+
 - (id)initWithSource:(NSString *)source {
     self = [super init];
     if (self) {
         appleScript = [[NSAppleScript alloc] initWithSource:source];
+        synchronous = NO;
         if (appleScript == nil) {
             [self release];
             self = nil;
@@ -118,6 +125,28 @@
 - (void)executeWithAppleEvent:(NSAppleEventDescriptor *)event 
completionHandler:(void (^)(NSAppleEventDescriptor *result, NSError 
*error))handler {
     if ([appleScript 
respondsToSelector:@selector(executeWithAppleEvent:completionHandler:)]) {
         [appleScript executeWithAppleEvent:event completionHandler:handler];
+    } else if (synchronous) {
+        if (event && [appleScript 
respondsToSelector:@selector(executeAppleEvent:error:)]) {
+            dispatch_sync([[self class] appleScriptQueue], ^{
+                NSError *error = nil;
+                NSDictionary *errorDict = nil;
+                NSAppleEventDescriptor *result = [appleScript 
executeAppleEvent:event error:&errorDict];
+                if (result == nil)
+                    error = errorFromDictionary(errorDict);
+                if (handler)
+                    handler(result, error);
+            });
+        } else if (event == nil && [appleScript 
respondsToSelector:@selector(executeAndReturnError:)]) {
+            dispatch_sync([[self class] appleScriptQueue], ^{
+                NSError *error = nil;
+                NSDictionary *errorDict = nil;
+                NSAppleEventDescriptor *result = [appleScript 
executeAndReturnError:&errorDict];
+                if (result == nil)
+                    error = errorFromDictionary(errorDict);
+                if (handler)
+                    handler(result, error);
+            });
+        }
     } else if (event && [appleScript 
respondsToSelector:@selector(executeAppleEvent:error:)]) {
         dispatch_async([[self class] appleScriptQueue], ^{
             NSError *error = nil;

Modified: trunk/bibdesk/BDSKScriptHookManager.m
===================================================================
--- trunk/bibdesk/BDSKScriptHookManager.m       2019-09-17 09:33:11 UTC (rev 
24218)
+++ trunk/bibdesk/BDSKScriptHookManager.m       2019-09-17 16:16:58 UTC (rev 
24219)
@@ -100,11 +100,6 @@
        return self;
 }
 
-- (BOOL)canRunScriptHookAsync:(BDSKScriptHook *)scriptHook {
-    return [[scriptHook name] isEqualToString:BDSKWillAutoFileScriptHookName] 
== NO &&
-           [[scriptHook name] 
isEqualToString:BDSKWillGenerateCiteKeyScriptHookName] == NO;
-}
-
 - (BDSKScriptHook *)scriptHookWithUniqueID:(NSString *)uniqueID {
        return [scriptHooks objectForKey:uniqueID];
 }
@@ -127,8 +122,9 @@
         NSLog(@"No script file found for script hook %@.", name);
                return nil;
        } else {
+        BOOL sync = [name isEqualToString:BDSKWillAutoFileScriptHookName] || 
[name isEqualToString:BDSKWillGenerateCiteKeyScriptHookName];
                NSError *error = nil;
-        script = [[BDSKAppleScript alloc] initWithURL:[NSURL 
fileURLWithPath:path isDirectory:NO] error:&error];
+        script = [[BDSKAppleScript alloc] initWithURL:[NSURL 
fileURLWithPath:path isDirectory:NO] synchronous:sync error:&error];
                if (script == nil) {
                        NSLog(@"Error creating AppleScript: %@", error);
                        return nil;
@@ -161,8 +157,6 @@
         NSAppleEventDescriptor *appleEvent = [NSAppleEventDescriptor 
appleEventWithEventClass:kBDSKBibdeskSuite eventID:kBDSKPerformBibdeskAction 
labelsAndParameters:keyDirectObject, items, kBDSKPrepositionForScriptHook, 
scriptHook, 0];
         BDSKAppleScript *script = [scriptHook script];
         
-        __block BOOL scriptHookFinished = NO;
-        
         [scriptHook autorelease];
         [script executeWithAppleEvent:appleEvent 
completionHandler:^(NSAppleEventDescriptor *result, NSError *error){
             if (error)
@@ -170,14 +164,8 @@
             dispatch_async(dispatch_get_main_queue(), ^{
                 // cleanup
                 [scriptHooks removeObjectForKey:[scriptHook uniqueID]];
-                scriptHookFinished = YES;
             });
         }];
-        
-        if ([self canRunScriptHookAsync:scriptHook] == NO) {
-            NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
-            while (scriptHookFinished == NO && [runLoop 
runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
-        }
     }
 }
 

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



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to