Hello-

I've been trying to create/use search groups with AppleScript and have  
run into several bugs and/or unexpected behavior. The modified files,  
current as of revision 13371, are here:

http://zinc.ucsf.edu/~colin/BibDeskAppleScriptPatch

The diffs as well as per-file commentary are below.

Cheers.

-Colin

********************************************************************************
When search groups are created via AppleScript, the no-argument init  
method is
called. Because the searchIndexes initializer lived in the more  
specialized init
methods, it never got called leading to thrown exceptions as soon as a  
search
was initiated. This fixes that problem.

Also, in version 1.3.17, if search groups were deleted while a search  
was in
progress, the server would have an EXEC_BAD_ACCESS trying to add the
publications to the already deallocated publications array. Changing  
the order
in the dealloc method appears to fix this problem in my testing. I'm  
not sure
that this is 100%, but I haven't been able to get it to fail since I  
made the
switch. There is a slight UI hiccup where the group selection changes  
as the
deallocation occurs. There must be some notification getting fired but  
I haven't
tracked that down. Anyway, not crashing is a good step in the right  
direction.
********************************************************************************

Index: BDSKSearchGroup.m
===================================================================
--- BDSKSearchGroup.m   (revision 13371)
+++ BDSKSearchGroup.m   (working copy)
@@ -55,6 +55,13 @@

  @implementation BDSKSearchGroup

+- (id)init;
+{
+    return [self initWithType:BDSKSearchGroupEntrez
+                   serverInfo:[BDSKServerInfo  
defaultServerInfoWithType:BDSKSearchGroupEntrez]
+                   searchTerm:nil];
+}
+
  - (id)initWithType:(NSString *)typeName name:(NSString *)aName;
  {
      return [self initWithType:typeName serverInfo:[NSDictionary  
dictionaryWithObject:aName forKey:@"database"] searchTerm:nil];
@@ -198,11 +205,12 @@

  - (void)dealloc
  {
+    // first terminate/release the server so it doesn't try to access  
objects that will be released
+    [server terminate];
+    [server release];
      [[NSNotificationCenter defaultCenter] removeObserver:self];
      [publications makeObjectsPerformSelector:@selector(setOwner:)  
withObject:nil];
      [publications release];
-    [server terminate];
-    [server release];
      [type release];
      [searchTerm release];
      [searchIndexes release];

********************************************************************************
AppleScript commands that changed search group settings previously  
would cause
the undo manager to think that the document changed, erroneously  
prompting the
user to save the document when search groups are only temporary.

The other change made was to automatically trigger a search if an  
AppleScript
command changes either the serverInfo or searchTerm (and the other  
piece of data
is already set).

Finally, there was a bug that made it impossible to change the server  
type through
AppleScript. Also fixed.
********************************************************************************

Index: BDSKGroup+Scripting.m
===================================================================
--- BDSKGroup+Scripting.m       (revision 13371)
+++ BDSKGroup+Scripting.m       (working copy)
@@ -425,7 +425,12 @@
  }

  - (void)setScriptingSearchTerm:(NSString *)newSerachTerm {
+    [[self undoManager] disableUndoRegistration];
      [self setSearchTerm:newSerachTerm];
+    [[self undoManager] enableUndoRegistration];
+    // execute the search if the server info is available
+    if ([self scriptingServerInfo])
+        [self search];
  }

  - (NSDictionary *)scriptingServerInfo {
@@ -481,7 +486,7 @@
      NSString *host = [info valueForKey:@"host"];
      NSString *port = [info valueForKey:@"port"];

-    if ([[serverInfo type] isEqualToString:type]) {
+    if ([[serverInfo type] isEqualToString:serverType]) {
          serverInfo = [[self serverInfo] mutableCopy];

          NSString *value;
@@ -538,8 +543,12 @@
      }

      if (isValid) {
+        [[self undoManager] disableUndoRegistration];
          [self setServerInfo:serverInfo];
-        [[self undoManager]  
setActionName:NSLocalizedString(@"AppleScript",@"Undo action name for  
AppleScript")];
+        [[self undoManager] enableUndoRegistration];
+        // execute the search if the search term is set
+        if ([self searchTerm] && [[self searchTerm] length] > 0)
+            [self search];
      } else {
          NSScriptCommand *cmd = [NSScriptCommand currentCommand];
          [cmd  
setScriptErrorNumber:NSReceiversCantHandleCommandScriptError];

********************************************************************************
The make new AppleScript command also triggers the undo mananger.  
Fixing that as
well.
********************************************************************************

Index: BibDocument+Scripting.m
===================================================================
--- BibDocument+Scripting.m     (revision 13371)
+++ BibDocument+Scripting.m     (working copy)
@@ -463,8 +463,9 @@
          [cmd  
setScriptErrorNumber:NSReceiversCantHandleCommandScriptError];
          [cmd setScriptErrorString:NSLocalizedString(@"Cannot add  
group.",@"Error description")];
      } else {
+        [[self undoManager] disableUndoRegistration];
          [groups addSearchGroup:group];
-        [[self undoManager]  
setActionName:NSLocalizedString(@"AppleSearch",@"Undo action name for  
AppleSearch")];
+        [[self undoManager] enableUndoRegistration];
      }
  }

@@ -473,7 +474,9 @@
  }

  - (void)removeObjectFromSearchGroupsAtIndex:(unsigned int)idx {
+    [[self undoManager] disableUndoRegistration];
        [groups removeSearchGroup:[[groups searchGroups] objectAtIndex:idx]];
+    [[self undoManager] enableUndoRegistration];
        [[self undoManager]  
setActionName:NSLocalizedString(@"AppleSearch",@"Undo action name for  
AppleSearch")];
  }

********************************************************************************
The import script hook wasn't getting called when clicking the  
"import" button
in search group table lines. This enables that.
********************************************************************************

Index: BibDocument_Groups.m
===================================================================
--- BibDocument_Groups.m        (revision 13371)
+++ BibDocument_Groups.m        (working copy)
@@ -82,6 +82,7 @@
  #import "BDSKSearchButtonController.h"
  #import "BDSKSharingClient.h"
  #import "WebURLsWithTitles.h"
+#import "BDSKScriptHookManager.h"


  @implementation BibDocument (Groups)
@@ -1483,6 +1484,8 @@
      [self generateCiteKeysForPublications:generatePubs];

      [groups setLastImportedPublications:newPubs];
+
+    [[BDSKScriptHookManager sharedManager]  
runScriptHookWithName:BDSKImportPublicationsScriptHookName  
forPublications:newPubs document:self];
        
        [[self undoManager] setActionName:NSLocalizedString(@"Merge  
External Publications", @"Undo action name")];

@@ -1842,6 +1845,8 @@
      [self generateCiteKeysForPublications:generatePubs];

      [groups setLastImportedPublications:newPubs];
+
+    [[BDSKScriptHookManager sharedManager]  
runScriptHookWithName:BDSKImportPublicationsScriptHookName  
forPublications:newPubs document:self];

        [[self undoManager] setActionName:NSLocalizedString(@"Import  
Publication", @"Undo action name")];
  }


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Bibdesk-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-develop

Reply via email to