Revision: 22256
          http://sourceforge.net/p/bibdesk/svn/22256
Author:   hofman
Date:     2018-05-25 20:00:44 +0000 (Fri, 25 May 2018)
Log Message:
-----------
Allow auto filing files from local file field (Local-Url)

Modified Paths:
--------------
    trunk/bibdesk/BDSKAutofileCommand.m
    trunk/bibdesk/BDSKFiler.h
    trunk/bibdesk/BDSKFiler.m
    trunk/bibdesk/BDSKFilerErrorController.m
    trunk/bibdesk/BibItem.m

Modified: trunk/bibdesk/BDSKAutofileCommand.m
===================================================================
--- trunk/bibdesk/BDSKAutofileCommand.m 2018-05-25 06:30:45 UTC (rev 22255)
+++ trunk/bibdesk/BDSKAutofileCommand.m 2018-05-25 20:00:44 UTC (rev 22256)
@@ -54,10 +54,11 @@
        NSDictionary *params = [self evaluatedArguments];
        NSNumber *indexNumber = [params objectForKey:@"index"];
     NSString *location = [params objectForKey:@"to"];
+    NSString *field = [params objectForKey:@"for"] ?: BDSKLocalFileString;
     NSNumber *checkNumber = [params objectForKey:@"check"];
        BOOL check = checkNumber ? [checkNumber boolValue] : YES;
-    BDSKFilerOptions mask = 0;
     NSUInteger i = indexNumber ? [indexNumber unsignedIntegerValue] - 1 : 0;
+    BOOL isFiles = [field isEqualToString:BDSKLocalFileString];
     
        if (pub == nil) {
                [self 
setScriptErrorNumber:NSRequiredArgumentsMissingScriptError]; 
@@ -72,11 +73,13 @@
     
     NSArray *localFiles = [pub localFiles];
        
-    if (i >= [localFiles count]) {
-               [self setScriptErrorNumber:NSArgumentsWrongScriptError]; 
-               return nil;
-       } else if ([localFiles count] == 0) {
-        return nil;
+    if (isFiles) {
+        if (i >= [localFiles count]) {
+            [self setScriptErrorNumber:NSArgumentsWrongScriptError];
+            return nil;
+        } else if ([localFiles count] == 0) {
+            return nil;
+        }
     }
     
     if (location) {
@@ -85,16 +88,24 @@
             return nil;
         }
         if ([location isAbsolutePath] == NO) {
+            if ([[BDSKFiler sharedFiler] checkPapersFolder] == NO) {
+                [self 
setScriptErrorNumber:NSReceiversCantHandleCommandScriptError];
+                return nil;
+            }
             NSString *papersFolderPath = [BDSKFormatParser 
folderPathForFilingPapersFromDocumentAtPath:[[doc fileURL] path]];
             location = [papersFolderPath 
stringByAppendingPathComponent:location]; 
         }
-        NSArray *paperInfos = [NSArray arrayWithObject:[NSDictionary 
dictionaryWithObjectsAndKeys:[localFiles objectAtIndex:i], BDSKFilerFileKey, 
pub, BDSKFilerPublicationKey, location, BDSKFilerNewPathKey, nil]];
-        if ([[BDSKFiler sharedFiler] movePapers:paperInfos 
forField:BDSKLocalFileString fromDocument:doc options:mask])
+        BDSKLinkedFile *file = isFiles ? [localFiles objectAtIndex:i] : nil;
+        NSArray *paperInfos = [NSArray arrayWithObject:[NSDictionary 
dictionaryWithObjectsAndKeys:pub, BDSKFilerPublicationKey, location, 
BDSKFilerNewPathKey, file, BDSKFilerFileKey, nil]];
+        if ([[BDSKFiler sharedFiler] movePapers:paperInfos forField:field 
fromDocument:doc options:BDSKAutoFileOptionInitial])
             [[pub undoManager] 
setActionName:NSLocalizedString(@"AppleScript",@"Undo action name for 
AppleScript")];
-    } else {
-        NSArray *files = indexNumber ? [NSArray arrayWithObject:[localFiles 
objectAtIndex:i]] : [pub localFiles];
+    } else if (isFiles) {
+        NSArray *files = indexNumber ? [NSArray arrayWithObject:[localFiles 
objectAtIndex:i]] : localFiles;
         if ([[BDSKFiler sharedFiler] autoFileLinkedFiles:files 
fromDocument:doc check:check])
             [[pub undoManager] 
setActionName:NSLocalizedString(@"AppleScript",@"Undo action name for 
AppleScript")];
+    } else {
+        if ([[BDSKFiler sharedFiler] autoFileItems:[NSArray 
arrayWithObjects:pub, nil] forField:field fromDocument:doc check:check])
+            [[pub undoManager] 
setActionName:NSLocalizedString(@"AppleScript",@"Undo action name for 
AppleScript")];
     }
     
     return nil;

Modified: trunk/bibdesk/BDSKFiler.h
===================================================================
--- trunk/bibdesk/BDSKFiler.h   2018-05-25 06:30:45 UTC (rev 22255)
+++ trunk/bibdesk/BDSKFiler.h   2018-05-25 20:00:44 UTC (rev 22256)
@@ -61,8 +61,9 @@
 
 typedef NS_OPTIONS(NSUInteger, BDSKFilerOptions) {
     BDSKAutoFileOptionInitial = 1 << 0,
-    BDSKAutoFileOptionCheckComplete = 1 << 1,
-    BDSKAutoFileOptionForce = 1 << 2
+    BDSKAutoFileOptionGenerate = 1 << 1,
+    BDSKAutoFileOptionCheckComplete = 1 << 2,
+    BDSKAutoFileOptionForce = 1 << 3
 };
 
 @interface BDSKFiler : NSWindowController {
@@ -71,6 +72,8 @@
 
 + (BDSKFiler *)sharedFiler;
 
+- (BOOL)checkPapersFolder;
+
 /*!
        @method         autoFileLinkedFiles:fromDocument:doc:check:
        @abstract       Main auto-file routine to file papers in the Papers 
folder according to a generated location.
@@ -83,6 +86,18 @@
 - (BOOL)autoFileLinkedFiles:(NSArray *)papers fromDocument:(BibDocument *)doc 
check:(BOOL)check;
 
 /*!
+ @method        autoFileItems:forField:fromDocument:doc:check:
+ @abstract    Main auto-file routine to file papers for a field in the Papers 
folder according to a generated location.
+ @param        items An array of publications for which files should be 
auto-filed.
+ @param        field The field whose values should be auto-filed
+ @param        doc The parent document of the papers.
+ @param        check Boolean determines whether to move only entries with all 
necessary fields set.
+ @discussion    This is the main method that should be used to autofile papers.
+ It calls the necessary methods to do the move and generates the new locations 
for the papers.
+ */
+- (BOOL)autoFileItems:(NSArray *)items forField:(NSString *)field 
fromDocument:(BibDocument *)doc check:(BOOL)check;
+
+/*!
        @method         movePapers:forField:fromDocument:options:
        @abstract       Tries to move list of papers from a document.
        @param          paperInfos A list of info dictionaries containing a 
BibItem, a BDSKLinkedFile and for non-initial autofiles a target path.

Modified: trunk/bibdesk/BDSKFiler.m
===================================================================
--- trunk/bibdesk/BDSKFiler.m   2018-05-25 06:30:45 UTC (rev 22255)
+++ trunk/bibdesk/BDSKFiler.m   2018-05-25 20:00:44 UTC (rev 22256)
@@ -109,10 +109,10 @@
 }
 
 - (BOOL)autoFileLinkedFiles:(NSArray *)papers fromDocument:(BibDocument *)doc 
check:(BOOL)check{
-       if ([self checkPapersFolder] == NO)
+    if ([self checkPapersFolder] == NO)
         return NO;
     
-    BDSKFilerOptions mask = BDSKAutoFileOptionInitial;
+    BDSKFilerOptions mask = BDSKAutoFileOptionInitial | 
BDSKAutoFileOptionGenerate;
     if (check) mask |= BDSKAutoFileOptionCheckComplete;
     
     NSMutableArray *paperInfos = [NSMutableArray arrayWithCapacity:[papers 
count]];
@@ -119,9 +119,23 @@
     for (BDSKLinkedFile *file in papers)
         [paperInfos addObject:[NSDictionary dictionaryWithObjectsAndKeys:file, 
BDSKFilerFileKey, [file delegate], BDSKFilerPublicationKey, nil]];
     
-       return [self movePapers:paperInfos forField:BDSKLocalFileString 
fromDocument:doc options:mask];
+    return [self movePapers:paperInfos forField:BDSKLocalFileString 
fromDocument:doc options:mask];
 }
 
+- (BOOL)autoFileItems:(NSArray *)items forField:(NSString *)field 
fromDocument:(BibDocument *)doc check:(BOOL)check{
+    if ([self checkPapersFolder] == NO)
+        return NO;
+    
+    BDSKFilerOptions mask = BDSKAutoFileOptionInitial | 
BDSKAutoFileOptionGenerate;
+    if (check) mask |= BDSKAutoFileOptionCheckComplete;
+    
+    NSMutableArray *paperInfos = [NSMutableArray arrayWithCapacity:[items 
count]];
+    for (BibItem *item in items)
+        [paperInfos addObject:[NSDictionary dictionaryWithObjectsAndKeys:item, 
BDSKFilerPublicationKey, nil]];
+    
+    return [self movePapers:paperInfos forField:field fromDocument:doc 
options:mask];
+}
+
 - (BOOL)movePapers:(NSArray *)paperInfos forField:(NSString *)field 
fromDocument:(BibDocument *)doc options:(BDSKFilerOptions)mask{
        NSFileManager *fm = [NSFileManager defaultManager];
     NSInteger numberOfPapers = [paperInfos count];
@@ -135,14 +149,16 @@
        NSError *error = nil;
     
     BOOL initial = (mask & BDSKAutoFileOptionInitial);
+    BOOL generate = (mask & BDSKAutoFileOptionGenerate);
     BOOL force = (mask & BDSKAutoFileOptionForce);
     BOOL check = (initial) && (force == NO) && (mask & 
BDSKAutoFileOptionCheckComplete);
+    BOOL isLinkedFiles = [field isEqualToString:BDSKLocalFileString];
     
        if (numberOfPapers == 0)
                return NO;
        
-       if (initial && [field isEqualToString:BDSKLocalFileString] == NO)
-        [NSException raise:BDSKUnimplementedException format:@"%@ is only 
implemented for local files for initial moves.",NSStringFromSelector(_cmd)];
+       //if (initial && [field isEqualToString:BDSKLocalFileString] == NO)
+    //    [NSException raise:BDSKUnimplementedException format:@"%@ is only 
implemented for local files for initial moves.",NSStringFromSelector(_cmd)];
        
        if (numberOfPapers > 1) {
         [self window];
@@ -155,12 +171,17 @@
                
         file = [paperInfo valueForKey:BDSKFilerFileKey];
         pub = [paperInfo valueForKey:BDSKFilerPublicationKey];
-        oldPath = [file path];
-               if (initial) // autofile action: an array of BDSKLinkedFiles
-                       newPath = [[pub suggestedURLForLinkedFile:file] path];
-               else // an explicit move, possibly from undo: a list of info 
dictionaries
-                       newPath = [paperInfo valueForKey:BDSKFilerNewPathKey];
-               
+        if (generate) // initial auto file
+            newPath = [[pub suggestedURLForLinkedFile:file] path];
+        else // new path provided, e.g. undo
+            newPath = [paperInfo valueForKey:BDSKFilerNewPathKey];
+        if (isLinkedFiles)
+            oldPath = [file path];
+        else if (initial)
+            oldPath = [[pub localFileURLForField:field] path];
+        else
+            oldPath = [paperInfo valueForKey:BDSKFilerOldPathKey];
+        
                if (numberOfPapers > 1) {
                        [progressIndicator incrementBy:1.0];
                        [progressIndicator displayIfNeeded];
@@ -167,7 +188,8 @@
                }
                        
                if ([NSString isEmptyString:oldPath]) {
-            [pub removeFileToBeFiled:file];
+            if (isLinkedFiles)
+                [pub removeFileToBeFiled:file];
                        continue;
         }
         
@@ -187,7 +209,8 @@
             
         } else if ([NSString isEmptyString:newPath] || [oldPath 
isEqualToString:newPath]) {
             
-            [pub removeFileToBeFiled:file];
+            if (isLinkedFiles)
+                [pub removeFileToBeFiled:file];
             
         } else {
             
@@ -206,12 +229,25 @@
                 
             } else {
                 
-                [file updateWithPath:newPath];
-                // make sure the UI is updated
-                [pub noteFilesChanged:YES];
+                if (isLinkedFiles) {
+                    [file updateWithPath:newPath];
+                    // make sure the UI is updated
+                    [pub noteFilesChanged:YES];
+                } else if (initial) {
+                    // if this is not an initial move, undo should take care 
of this
+                    NSString *value = [pub valueOfField:field inherit:NO];
+                    if ([value hasCaseInsensitivePrefix:@"file://"])
+                        value = [[NSURL fileURLWithPath:newPath] 
absoluteString];
+                    else if ([value isAbsolutePath] == NO && [doc fileURL])
+                        value = [value relativePathFromPath:[[[doc fileURL] 
URLByDeletingLastPathComponent] path]];
+                    else
+                        value = newPath;
+                    [pub setField:field toValue:value];
+                }
                 
                 // switch them as this is used in undo
                 [info setValue:oldPath forKey:BDSKFilerNewPathKey];
+                [info setValue:newPath forKey:BDSKFilerOldPathKey];
                 [fileInfoDicts addObject:info];
                 
                 [[BDSKScriptHookManager sharedManager] 
runScriptHookWithName:BDSKDidAutoFileScriptHookName 

Modified: trunk/bibdesk/BDSKFilerErrorController.m
===================================================================
--- trunk/bibdesk/BDSKFilerErrorController.m    2018-05-25 06:30:45 UTC (rev 
22255)
+++ trunk/bibdesk/BDSKFilerErrorController.m    2018-05-25 20:00:44 UTC (rev 
22256)
@@ -87,7 +87,7 @@
 - (void)windowDidLoad {
     [self setWindowFrameAutosaveNameOrCascade:@"AutoFileWindow"];
     
-    if (options & BDSKAutoFileOptionInitial)
+    if (options & BDSKAutoFileOptionGenerate)
         [infoTextField setStringValue:NSLocalizedString(@"There were problems 
moving the following files to the location generated using the format string. 
You can retry to move items selected in the first column.",@"description 
string")];
     else
         [infoTextField setStringValue:NSLocalizedString(@"There were problems 
moving the following files to the target location. You can retry to move items 
selected in the first column.",@"description string")];
@@ -117,7 +117,7 @@
     }
     
     NSString *field = [[fieldName retain] autorelease];
-    BDSKFilerOptions mask = (options & BDSKAutoFileOptionInitial);
+    BDSKFilerOptions mask = (options & (BDSKAutoFileOptionInitial | 
BDSKAutoFileOptionGenerate));
     mask |= ([forceCheckButton state]) ? BDSKAutoFileOptionForce : (options & 
BDSKAutoFileOptionCheckComplete);
     
     [self close];
@@ -132,7 +132,7 @@
         [string appendStrings:NSLocalizedString(@"Publication key: ", @"Label 
for autofile dump"),
                               [[info objectForKey:BDSKFilerPublicationKey] 
citeKey], @"\n", 
                               NSLocalizedString(@"Original path: ", @"Label 
for autofile dump"),
-                              [[[info objectForKey:BDSKFilerFileKey] URL] 
path], @"\n", 
+                              [info objectForKey:BDSKFilerOldPathKey], @"\n",
                               NSLocalizedString(@"New path: ", @"Label for 
autofile dump"),
                               [info objectForKey:BDSKFilerNewPathKey], @"\n", 
                               NSLocalizedString(@"Status: ",@"Label for 
autofile dump"),
@@ -182,7 +182,7 @@
         case 0:
             if(statusFlag & BDSKSourceFileDoesNotExistErrorMask)
                 return;
-            path = [[[dict objectForKey:BDSKFilerFileKey] URL] path];
+            path = [dict objectForKey:BDSKFilerOldPathKey];
             [[NSWorkspace sharedWorkspace]  selectFile:path 
inFileViewerRootedAtPath:@""];
             break;
         case 1:

Modified: trunk/bibdesk/BibItem.m
===================================================================
--- trunk/bibdesk/BibItem.m     2018-05-25 06:30:45 UTC (rev 22255)
+++ trunk/bibdesk/BibItem.m     2018-05-25 20:00:44 UTC (rev 22256)
@@ -2814,9 +2814,13 @@
 
 - (NSURL *)suggestedURLForLinkedFile:(BDSKLinkedFile *)file
 {
-       NSString *localFileFormat = [[NSUserDefaults standardUserDefaults] 
stringForKey:BDSKLocalFileFormatKey];
-       NSString *path = [BDSKFormatParser parseFormat:localFileFormat 
forLinkedFile:file ofItem:self];
-       return [NSURL fileURLWithPath:path];
+    NSString *path = nil;
+    NSString *localFileFormat = [[NSUserDefaults standardUserDefaults] 
stringForKey:BDSKLocalFileFormatKey];
+    if (file)
+        path = [BDSKFormatParser parseFormat:localFileFormat 
forLinkedFile:file ofItem:self];
+    else
+        path = [BDSKFormatParser parseFormat:localFileFormat 
forField:BDSKLocalUrlString ofItem:self suggestion:[[self 
localFileURLForField:BDSKLocalUrlString] path]];
+    return [NSURL fileURLWithPath:path];
 }
 
 - (BOOL)canSetURLForLinkedFile:(BDSKLinkedFile *)file
@@ -2833,8 +2837,10 @@
             if([self hasEmptyOrDefaultCiteKey])
                                return NO;
                } else if ([fieldName isEqualToString:BDSKLocalFileString]) {
-                       if ([file URL] == nil)
-                               return NO;
+            if (file && [file URL] == nil)
+                return NO;
+            else if (file == nil && [self 
localFileURLForField:BDSKLocalUrlString] == nil)
+                return NO;
                } else if ([fieldName isEqualToString:@"Document Filename"]) {
                        if ([NSString isEmptyString:[[owner fileURL] path]])
                                return NO;

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


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to