Revision: 22836 http://sourceforge.net/p/bibdesk/svn/22836 Author: hofman Date: 2018-10-14 17:09:16 +0000 (Sun, 14 Oct 2018) Log Message: ----------- let generic group respond to export command
Modified Paths: -------------- trunk/bibdesk/BDSKGroup+Scripting.h trunk/bibdesk/BDSKGroup+Scripting.m trunk/bibdesk/Scripting/BibDesk.sdef Modified: trunk/bibdesk/BDSKGroup+Scripting.h =================================================================== --- trunk/bibdesk/BDSKGroup+Scripting.h 2018-10-14 16:40:21 UTC (rev 22835) +++ trunk/bibdesk/BDSKGroup+Scripting.h 2018-10-14 17:09:16 UTC (rev 22836) @@ -69,6 +69,8 @@ - (BOOL)isExternal; +- (id)handleExportScriptCommand:(NSScriptCommand *)command; + @end #pragma mark - @@ -127,9 +129,6 @@ #pragma mark - @interface BDSKExternalGroup (Scripting) - -- (id)handleExportScriptCommand:(NSScriptCommand *)command; - @end #pragma mark - Modified: trunk/bibdesk/BDSKGroup+Scripting.m =================================================================== --- trunk/bibdesk/BDSKGroup+Scripting.m 2018-10-14 16:40:21 UTC (rev 22835) +++ trunk/bibdesk/BDSKGroup+Scripting.m 2018-10-14 17:09:16 UTC (rev 22836) @@ -134,6 +134,125 @@ return ([self groupType] & BDSKExternalGroupType) != 0; } +- (id)handleExportScriptCommand:(NSScriptCommand *)command { + NSDictionary *params = [command evaluatedArguments]; + + // the 'to' parameters gives the file to save to, either as a path or a url (it seems) + id fileObj = [params objectForKey:@"to"]; + NSURL *fileURL = nil; + + // make sure we get the right thing + if ([fileObj isKindOfClass:[NSString class]]) { + fileURL = [NSURL fileURLWithPath:(NSString *)fileObj]; + if (fileURL == nil) + return nil; + } else if ([fileObj isKindOfClass:[NSURL class]]) { + fileURL = (NSURL *)fileObj; + } else if (fileObj && ([fileObj isKindOfClass:[NSPropertySpecifier class]] == NO || [[fileObj key] isEqualToString:@"clipboard"] == NO)) { + [command setScriptErrorNumber:NSArgumentsWrongScriptError]; + return nil; + } + + // the 'using' parameters gives the template name to use + id templateStyle = [params objectForKey:@"using"]; + id templateString = [params objectForKey:@"usingText"]; + id templateAttrString = [params objectForKey:@"usingRichText"]; + BDSKTemplate *template = nil; + // make sure we get something + if (templateStyle == nil && templateString == nil && templateAttrString == nil) { + [command setScriptErrorNumber:NSRequiredArgumentsMissingScriptError]; + [command setScriptErrorString:@"One of the 'using' argument needs to be provided."]; + return nil; + } + // make sure we get the right thing + if ([templateStyle isKindOfClass:[BDSKTemplate class]] ) { + template = templateStyle; + } else if ([templateStyle isKindOfClass:[NSString class]] ) { + template = [BDSKTemplate templateForStyle:templateStyle]; + } else if ([templateStyle isKindOfClass:[NSURL class]] ) { + template = [BDSKTemplate templateWithName:@"" mainPageURL:templateStyle fileType:[templateStyle pathExtension] ?: [fileURL pathExtension] ?: @"txt"]; + } else if ([templateString isKindOfClass:[NSString class]] ) { + template = [BDSKTemplate templateWithString:templateString fileType:[fileURL pathExtension] ?: @"txt"]; + } else if ([templateAttrString isKindOfClass:[NSAttributedString class]] ) { + template = [BDSKTemplate templateWithAttributedString:templateAttrString fileType:[fileURL pathExtension] ?: @"rtf"]; + } + if (template == nil) { + [command setScriptErrorNumber:NSArgumentsWrongScriptError]; + return nil; + } + + id<BDSKOwner> owner = [self isExternal] ? (id<BDSKOwner>)self : [self document]; + + // the 'for' parameter can select the items to template + NSArray *obj = [params objectForKey:@"for"]; + NSArray *items = nil; + if (obj) { + items = [obj isKindOfClass:[NSArray class]] ? obj : [NSArray arrayWithObject:obj]; + if ([items count] && NSNotFound != [items indexOfObjectPassingTest:^BOOL(id anObj, NSUInteger idx, BOOL *stop){ return NO == [anObj isKindOfClass:[BibItem class]] || [anObj owner] != owner; }]) { + // wrong kind of argument + [command setScriptErrorNumber:NSArgumentsWrongScriptError]; + [command setScriptErrorString:@"The 'in' option needs to be a publication or a list of publications."]; + return nil; + } + } else { + items = [self scriptingPublications]; + } + + // the 'in' parameter can select the items context to template + obj = [params objectForKey:@"in"]; + NSArray *itemsContext = nil; + if (obj) { + items = [obj isKindOfClass:[NSArray class]] ? obj : [NSArray arrayWithObject:obj]; + if ([items count] && NSNotFound != [obj indexOfObjectPassingTest:^BOOL(id anObj, NSUInteger idx, BOOL *stop){ return NO == [anObj isKindOfClass:[BibItem class]] || [anObj owner] != owner; }]) { + // wrong kind of argument + [command setScriptErrorNumber:NSArgumentsWrongScriptError]; + [command setScriptErrorString:@"The 'for' option needs to be a list of publications."]; + return nil; + } + } + + NSString *string = nil; + NSAttributedString *attrString = nil; + NSDictionary *docAttributes = nil; + BDSKTemplateFormat format = [template templateFormat]; + + if ((format & BDSKTemplateFormatRichText)) + attrString = [BDSKTemplateObjectProxy attributedStringByParsingTemplate:template withObject:owner publications:items publicationsContext:itemsContext documentAttributes:&docAttributes]; + else + string = [BDSKTemplateObjectProxy stringByParsingTemplate:template withObject:owner publications:items publicationsContext:itemsContext]; + + if (string == nil && attrString == nil) { + [command setScriptErrorNumber:NSInternalScriptError]; + [command setScriptErrorString:@"Could not parse template."]; + return nil; + } + + if (fileURL) { + NSData *fileData = nil; + if ((format & BDSKTemplateFormatRTFD)) { + NSFileWrapper *fileWrapper = [attrString RTFDFileWrapperFromRange:NSMakeRange(0,[attrString length]) documentAttributes:docAttributes]; + [fileWrapper writeToFile:[fileURL path] atomically:YES updateFilenames:NO]; + } else if ((format & BDSKTemplateFormatRichText)) { + NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:docAttributes]; + [mutableAttributes setObject:[template documentType] forKey:NSDocumentTypeDocumentAttribute]; + fileData = [attrString dataFromRange:NSMakeRange(0,[attrString length]) documentAttributes:mutableAttributes error:NULL]; + [fileData writeToURL:fileURL atomically:YES]; + } else { + [string writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:NULL]; + } + + NSURL *destDirURL = [fileURL URLByDeletingLastPathComponent]; + for (NSURL *accessoryURL in [template accessoryFileURLs]) + [[NSFileManager defaultManager] copyItemAtURL:accessoryURL toURL:[destDirURL URLByAppendingPathComponent:[accessoryURL lastPathComponent]] error:NULL]; + } else if (fileObj) { + NSPasteboard *pboard = [NSPasteboard generalPasteboard]; + [pboard clearContents]; + [pboard writeObjects:[NSArray arrayWithObjects:(id)attrString ?: (id)string, nil]]; + } + + return string ? (id)string : (id)[attrString richTextSpecifier]; +} + @end #pragma mark - @@ -336,123 +455,6 @@ return identifierURL ? [[self publications] itemForIdentifierURL:identifierURL] : nil; } -- (id)handleExportScriptCommand:(NSScriptCommand *)command { - NSDictionary *params = [command evaluatedArguments]; - - // the 'to' parameters gives the file to save to, either as a path or a url (it seems) - id fileObj = [params objectForKey:@"to"]; - NSURL *fileURL = nil; - - // make sure we get the right thing - if ([fileObj isKindOfClass:[NSString class]]) { - fileURL = [NSURL fileURLWithPath:(NSString *)fileObj]; - if (fileURL == nil) - return nil; - } else if ([fileObj isKindOfClass:[NSURL class]]) { - fileURL = (NSURL *)fileObj; - } else if (fileObj && ([fileObj isKindOfClass:[NSPropertySpecifier class]] == NO || [[fileObj key] isEqualToString:@"clipboard"] == NO)) { - [command setScriptErrorNumber:NSArgumentsWrongScriptError]; - return nil; - } - - // the 'using' parameters gives the template name to use - id templateStyle = [params objectForKey:@"using"]; - id templateString = [params objectForKey:@"usingText"]; - id templateAttrString = [params objectForKey:@"usingRichText"]; - BDSKTemplate *template = nil; - // make sure we get something - if (templateStyle == nil && templateString == nil && templateAttrString == nil) { - [command setScriptErrorNumber:NSRequiredArgumentsMissingScriptError]; - [command setScriptErrorString:@"One of the 'using' argument needs to be provided."]; - return nil; - } - // make sure we get the right thing - if ([templateStyle isKindOfClass:[BDSKTemplate class]] ) { - template = templateStyle; - } else if ([templateStyle isKindOfClass:[NSString class]] ) { - template = [BDSKTemplate templateForStyle:templateStyle]; - } else if ([templateStyle isKindOfClass:[NSURL class]] ) { - template = [BDSKTemplate templateWithName:@"" mainPageURL:templateStyle fileType:[templateStyle pathExtension] ?: [fileURL pathExtension] ?: @"txt"]; - } else if ([templateString isKindOfClass:[NSString class]] ) { - template = [BDSKTemplate templateWithString:templateString fileType:[fileURL pathExtension] ?: @"txt"]; - } else if ([templateAttrString isKindOfClass:[NSAttributedString class]] ) { - template = [BDSKTemplate templateWithAttributedString:templateAttrString fileType:[fileURL pathExtension] ?: @"rtf"]; - } - if (template == nil) { - [command setScriptErrorNumber:NSArgumentsWrongScriptError]; - return nil; - } - - // the 'for' parameter can select the items to template - NSArray *obj = [params objectForKey:@"for"]; - NSArray *items = nil; - if (obj) { - items = [obj isKindOfClass:[NSArray class]] ? obj : [NSArray arrayWithObject:obj]; - if ([items count] && NSNotFound != [items indexOfObjectPassingTest:^BOOL(id anObj, NSUInteger idx, BOOL *stop){ return NO == [anObj isKindOfClass:[BibItem class]] || [anObj owner] != self; }]) { - // wrong kind of argument - [command setScriptErrorNumber:NSArgumentsWrongScriptError]; - [command setScriptErrorString:@"The 'in' option needs to be a publication or a list of publications."]; - return nil; - } - } else { - items = [self publications]; - } - - // the 'in' parameter can select the items context to template - obj = [params objectForKey:@"in"]; - NSArray *itemsContext = nil; - if (obj) { - items = [obj isKindOfClass:[NSArray class]] ? obj : [NSArray arrayWithObject:obj]; - if ([items count] && NSNotFound != [obj indexOfObjectPassingTest:^BOOL(id anObj, NSUInteger idx, BOOL *stop){ return NO == [anObj isKindOfClass:[BibItem class]] || [anObj owner] != self; }]) { - // wrong kind of argument - [command setScriptErrorNumber:NSArgumentsWrongScriptError]; - [command setScriptErrorString:@"The 'for' option needs to be a list of publications."]; - return nil; - } - } - - NSString *string = nil; - NSAttributedString *attrString = nil; - NSDictionary *docAttributes = nil; - BDSKTemplateFormat format = [template templateFormat]; - - if ((format & BDSKTemplateFormatRichText)) - attrString = [BDSKTemplateObjectProxy attributedStringByParsingTemplate:template withObject:self publications:items publicationsContext:itemsContext documentAttributes:&docAttributes]; - else - string = [BDSKTemplateObjectProxy stringByParsingTemplate:template withObject:self publications:items publicationsContext:itemsContext]; - - if (string == nil && attrString == nil) { - [command setScriptErrorNumber:NSInternalScriptError]; - [command setScriptErrorString:@"Could not parse template."]; - return nil; - } - - if (fileURL) { - NSData *fileData = nil; - if ((format & BDSKTemplateFormatRTFD)) { - NSFileWrapper *fileWrapper = [attrString RTFDFileWrapperFromRange:NSMakeRange(0,[attrString length]) documentAttributes:docAttributes]; - [fileWrapper writeToFile:[fileURL path] atomically:YES updateFilenames:NO]; - } else if ((format & BDSKTemplateFormatRichText)) { - NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:docAttributes]; - [mutableAttributes setObject:[template documentType] forKey:NSDocumentTypeDocumentAttribute]; - fileData = [attrString dataFromRange:NSMakeRange(0,[attrString length]) documentAttributes:mutableAttributes error:NULL]; - [fileData writeToURL:fileURL atomically:YES]; - } else { - [string writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:NULL]; - } - - NSURL *destDirURL = [fileURL URLByDeletingLastPathComponent]; - for (NSURL *accessoryURL in [template accessoryFileURLs]) - [[NSFileManager defaultManager] copyItemAtURL:accessoryURL toURL:[destDirURL URLByAppendingPathComponent:[accessoryURL lastPathComponent]] error:NULL]; - } else if (fileObj) { - NSPasteboard *pboard = [NSPasteboard generalPasteboard]; - [pboard clearContents]; - [pboard writeObjects:[NSArray arrayWithObjects:(id)attrString ?: (id)string, nil]]; - } - - return string ? (id)string : (id)[attrString richTextSpecifier]; -} - @end #pragma mark - Modified: trunk/bibdesk/Scripting/BibDesk.sdef =================================================================== --- trunk/bibdesk/Scripting/BibDesk.sdef 2018-10-14 16:40:21 UTC (rev 22835) +++ trunk/bibdesk/Scripting/BibDesk.sdef 2018-10-14 17:09:16 UTC (rev 22836) @@ -532,7 +532,7 @@ <command name="export" code="BDSKexpt" description="Exports publications in the document using a template to file."> <direct-parameter - description="The object responding to the command, the document or external group."> + description="The object responding to the command, the document or group."> <type type="document"/> <type type="group"/> </direct-parameter> @@ -1399,6 +1399,9 @@ <responds-to name="select"> <cocoa method=""/> </responds-to> + <responds-to name="export"> + <cocoa method="handleExportScriptCommand:"/> + </responds-to> </class> <class name="library group" plural="library groups" code="Libr" inherits="group" @@ -1449,9 +1452,6 @@ description="The file for the external group"> <cocoa key="scriptingFileURL"/> </property> - <responds-to name="export"> - <cocoa method="handleExportScriptCommand:"/> - </responds-to> </class> <class name="script group" plural="script groups" code="ScGp" inherits="group" @@ -1465,9 +1465,6 @@ description="The script arguments for the group"> <cocoa key="scriptingScriptArguments"/> </property> - <responds-to name="export"> - <cocoa method="handleExportScriptCommand:"/> - </responds-to> </class> <class name="search group" plural="search groups" code="SrGp" inherits="group" @@ -1489,17 +1486,11 @@ description="The server name of the search group"> <cocoa key="scriptingServerName"/> </property> - <responds-to name="export"> - <cocoa method="handleExportScriptCommand:"/> - </responds-to> </class> <class name="shared group" plural="shared groups" code="ShGp" inherits="group" description="A group containing shared publications."> <cocoa class="BDSKSharedGroup"/> - <responds-to name="export"> - <cocoa method="handleExportScriptCommand:"/> - </responds-to> </class> <class name="web group" plural="web groups" code="WbGp" inherits="group" @@ -1509,9 +1500,6 @@ description="The URL for the web group"> <cocoa key="URLString"/> </property> - <responds-to name="export"> - <cocoa method="handleExportScriptCommand:"/> - </responds-to> </class> <class name="script hook" plural="script hooks" code="bshk" 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