From 5799c8687f888dc7d62ab8115cd402d6eca4eb3c Mon Sep 17 00:00:00 2001 From: daschae Date: Mon, 8 Nov 2004 03:32:12 +0000 Subject: [PATCH] [RFE 1062042] Initial import providing the "post current Safari document" feature. --- Application/AppController.h | 5 ++ Application/AppController.m | 64 ++++++++++++++++++-- Delicious Client.xcode/project.pbxproj | 83 ++++++++++++++++++++++---- Utilities/NSAppleScript+HandlerCalls.h | 24 ++++++++ Utilities/NSAppleScript+HandlerCalls.m | 51 ++++++++++++++++ defines.h | 7 ++- 6 files changed, 217 insertions(+), 17 deletions(-) create mode 100644 Utilities/NSAppleScript+HandlerCalls.h create mode 100644 Utilities/NSAppleScript+HandlerCalls.m diff --git a/Application/AppController.h b/Application/AppController.h index 929224c..a45860c 100644 --- a/Application/AppController.h +++ b/Application/AppController.h @@ -13,6 +13,7 @@ #import "DCAPIParser.h" #import "DCAPITagFormatter.h" #import "NSString+SFHFUtils.h" +#import "NSAppleScript+HandlerCalls.h" #import "SFHFKeychainUtils.h" #import "SFHFTableView.h" #import "SFHFiTunesTableHeaderCell.h" @@ -58,6 +59,8 @@ BOOL useExtendedSearch; + NSAppleScript *safariScript; + #ifdef AWOOSTER_CHANGES FullTextIndex *textIndex; BOOL useFullTextSearch; @@ -107,8 +110,10 @@ - (IBAction) showPostingInterface: (id) sender; - (IBAction) closePostingInterface: (id) sender; - (IBAction) postNewLink: (id) sender; +- (IBAction) postCurrentSafariURL: (id) sender; - (IBAction) editSelectedLinks: (id) sender; - (IBAction) deleteSelectedLinks: (id) sender; +- (void) handleScriptError: (NSDictionary *) errorInfo; - (IBAction) setSearchTypeToBasic: (id) sender; - (IBAction) setSearchTypeToExtended: (id) sender; #ifdef AWOOSTER_CHANGES diff --git a/Application/AppController.m b/Application/AppController.m index 15999c3..4692f1d 100644 --- a/Application/AppController.m +++ b/Application/AppController.m @@ -61,7 +61,17 @@ - (void) awakeFromNib { - (void) applicationDidFinishLaunching: (NSNotification *) aNotification { /* Support for NetNewsWire External Weblog Editor Interface */ [[NSAppleEventManager sharedAppleEventManager] setEventHandler: self andSelector: @selector(postNewNNWLink:withReplyEvent:) forEventClass: DCNNWPostAppleEventClass andEventID: DCNNWPostAppleEventID]; - + + NSString *safariScriptPath = [[NSBundle mainBundle] pathForResource: kDCSafariScriptLibrary ofType: kDCScriptType]; + NSURL *safariScriptURL = [NSURL fileURLWithPath: safariScriptPath]; + NSDictionary *errorInfo = nil; + + safariScript = [[NSAppleScript alloc] initWithContentsOfURL: safariScriptURL error: &errorInfo]; + + if (!safariScript || errorInfo) { + [self handleScriptError: errorInfo]; + } + [postList setDoubleAction: @selector(openSelected:)]; [self setPosts: [NSArray array]]; @@ -81,7 +91,7 @@ - (void) setupTaglist { [tagList initializeColumnsUsingHeaderCellClass: [SFHFMetalTableHeaderCell class] formatterClass: [DCAPITagFormatter class]]; - [tagList registerForDraggedTypes: [NSArray arrayWithObject: DCAPIPostPboardType]]; + [tagList registerForDraggedTypes: [NSArray arrayWithObject: kDCAPIPostPboardType]]; SFHFMetalTableHeaderCell *cornerCell = [[SFHFMetalTableHeaderCell alloc] initTextCell: @" "]; SFHFCornerView *cornerControl = [[SFHFCornerView alloc] init]; @@ -638,12 +648,11 @@ - (void) tableView: (NSTableView *) view setObjectValue: (id) object forTableCol - (BOOL) tableView: (NSTableView *) tableView writeRows: (NSArray *) rows toPasteboard: (NSPasteboard *) pboard { if (tableView == postList) { - [pboard declareTypes: [NSArray arrayWithObject: DCAPIPostPboardType] owner: self]; + [pboard declareTypes: [NSArray arrayWithObject: kDCAPIPostPboardType] owner: self]; NSNumber *currentPostIndex = [rows objectAtIndex: 0]; DCAPIPost *currentPost = [[self filteredPosts] objectAtIndex: [currentPostIndex unsignedIntValue]]; - - [pboard setData: [NSKeyedArchiver archivedDataWithRootObject: currentPost] forType: DCAPIPostPboardType]; + [pboard setData: [NSKeyedArchiver archivedDataWithRootObject: currentPost] forType: kDCAPIPostPboardType]; return YES; } @@ -662,7 +671,7 @@ - (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id ) info row: (int) row dropOperation: (NSTableViewDropOperation) operation { #warning modify if tableView:validateDrop:proposedRow:proposedOperation returns for more than just tag assignment NSPasteboard *pboard = [info draggingPasteboard]; - NSData *data = [pboard dataForType: DCAPIPostPboardType]; + NSData *data = [pboard dataForType: kDCAPIPostPboardType]; DCAPIPost *post = [NSKeyedUnarchiver unarchiveObjectWithData: data]; NSString *postTags = [post tagsAsString]; @@ -998,6 +1007,40 @@ - (IBAction) postNewLink: (id) sender { [self refresh: self]; } +- (IBAction) postCurrentSafariURL: (id) sender { + NSDictionary *errorInfo = nil; + NSAppleEventDescriptor *arguments = [[NSAppleEventDescriptor alloc] initListDescriptor]; + + NSAppleEventDescriptor *result = [safariScript callHandler: kDCSafariGetCurrentURL withArguments: arguments errorInfo: &errorInfo]; + + NSString *scriptResult = [result stringValue]; + + /* Check for errors in running the handler */ + if (errorInfo) { + [self handleScriptError: errorInfo]; + } + /* Check the handler's return value */ + else if ([scriptResult isEqualToString: kScriptError]) { + NSRunAlertPanel(NSLocalizedString(@"Script Failure", @"Title on script failure window."), [NSString stringWithFormat: @"%@ %d", NSLocalizedString(@"The script failed:", @"Message on script failure window."), scriptResult], NSLocalizedString(@"OK", @""), nil, nil); + } + + NSString *URLString = [[scriptResult componentsSeparatedByString: @"***"] objectAtIndex: 1]; + + if (URLString) { + [currentPostProperties setObject: URLString forKey: @"url"]; + } + + NSString *description = [[scriptResult componentsSeparatedByString: @"***"] objectAtIndex: 0]; + + if (description) { + [currentPostProperties setObject: description forKey: @"description"]; + } + + [self showPostingInterface: self]; + + [arguments release]; +} + - (IBAction) editSelectedLinks: (id) sender { int selectedRow = [postList selectedRow]; @@ -1048,6 +1091,14 @@ - (IBAction) deleteSelectedLinks: (id) sender { } } +- (void) handleScriptError: (NSDictionary *) errorInfo { +#warning if safariScript = nil disable menu item + NSString *errorMessage = [errorInfo objectForKey: NSAppleScriptErrorBriefMessage]; + NSNumber *errorNumber = [errorInfo objectForKey: NSAppleScriptErrorNumber]; + + NSRunAlertPanel(NSLocalizedString(@"Script Error", @"Title on script error window."), [NSString stringWithFormat: @"%@: %@", NSLocalizedString(@"The script produced an error", @"Message on script error window."), errorNumber, errorMessage], NSLocalizedString(@"OK", @""), nil, nil); +} + - (void) windowWillClose: (NSNotification *) aNotification { NSWindow *theWindow = [aNotification object]; @@ -1097,6 +1148,7 @@ - (void) dealloc { #ifdef AWOOSTER_CHANGES [textIndex release]; #endif + [safariScript release]; [super dealloc]; } diff --git a/Delicious Client.xcode/project.pbxproj b/Delicious Client.xcode/project.pbxproj index 1ff2340..cc23898 100644 --- a/Delicious Client.xcode/project.pbxproj +++ b/Delicious Client.xcode/project.pbxproj @@ -55,8 +55,7 @@ sourceTree = ""; }; 1058C7A1FEA54F0111CA2CBB = { - fallbackIsa = PBXFileReference; - isa = PBXFrameworkReference; + isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; @@ -123,6 +122,7 @@ 6BD8D4C407205239008CA80A, 6BD8D4B607205221008CA80A, 49FE4954073A0868004027E5, + 3B3F0225073EF17A003AB1C7, 6BD8D4A707205205008CA80A, 29B97315FDCFA39411CA2CEA, 29B97317FDCFA39411CA2CEA, @@ -199,8 +199,7 @@ sourceTree = ""; }; 29B97324FDCFA39411CA2CEA = { - fallbackIsa = PBXFileReference; - isa = PBXFrameworkReference; + isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; @@ -208,8 +207,7 @@ sourceTree = ""; }; 29B97325FDCFA39411CA2CEA = { - fallbackIsa = PBXFileReference; - isa = PBXFrameworkReference; + isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; @@ -239,6 +237,70 @@ //322 //323 //324 +//3B0 +//3B1 +//3B2 +//3B3 +//3B4 + 3B3F0225073EF17A003AB1C7 = { + children = ( + 3B3F0228073EF1A3003AB1C7, + ); + isa = PBXGroup; + name = Scripts; + refType = 4; + sourceTree = ""; + }; + 3B3F0228073EF1A3003AB1C7 = { + fileEncoding = 30; + isa = PBXFileReference; + lastKnownFileType = sourcecode.scpt; + name = safari_script.scpt; + path = Scripts/safari_script.scpt; + refType = 4; + sourceTree = ""; + }; + 3B3F0229073EF1A3003AB1C7 = { + fileRef = 3B3F0228073EF1A3003AB1C7; + isa = PBXBuildFile; + settings = { + }; + }; + 3B3F0249073EF405003AB1C7 = { + fileEncoding = 30; + isa = PBXFileReference; + lastKnownFileType = sourcecode.c.h; + name = "NSAppleScript+HandlerCalls.h"; + path = "Utilities/NSAppleScript+HandlerCalls.h"; + refType = 4; + sourceTree = ""; + }; + 3B3F024A073EF405003AB1C7 = { + fileEncoding = 30; + isa = PBXFileReference; + lastKnownFileType = sourcecode.c.objc; + name = "NSAppleScript+HandlerCalls.m"; + path = "Utilities/NSAppleScript+HandlerCalls.m"; + refType = 4; + sourceTree = ""; + }; + 3B3F024B073EF405003AB1C7 = { + fileRef = 3B3F0249073EF405003AB1C7; + isa = PBXBuildFile; + settings = { + }; + }; + 3B3F024C073EF405003AB1C7 = { + fileRef = 3B3F024A073EF405003AB1C7; + isa = PBXBuildFile; + settings = { + }; + }; +//3B0 +//3B1 +//3B2 +//3B3 +//3B4 //490 //491 //492 @@ -293,8 +355,6 @@ //4A3 //4A4 4A9504CCFFE6A4B311CA0CBA = { - buildRules = ( - ); buildSettings = { COPY_PHASE_STRIP = NO; DEBUGGING_SYMBOLS = YES; @@ -310,8 +370,6 @@ name = Development; }; 4A9504CDFFE6A4B311CA0CBA = { - buildRules = ( - ); buildSettings = { COPYING_PRESERVES_HFS_DATA = NO; COPY_PHASE_STRIP = YES; @@ -802,6 +860,8 @@ }; 6BD8D4A707205205008CA80A = { children = ( + 3B3F0249073EF405003AB1C7, + 3B3F024A073EF405003AB1C7, 6BA936C20655EEC400AE7890, 6BA936C30655EEC400AE7890, 6B212599071D935F0044339D, @@ -1014,6 +1074,7 @@ 6B34D116072A2CBA00D08DB1, 6B34D18F072A2E7400D08DB1, 49FE4957073A0868004027E5, + 3B3F024B073EF405003AB1C7, ); isa = PBXHeadersBuildPhase; runOnlyForDeploymentPostprocessing = 0; @@ -1031,6 +1092,7 @@ 8D11072B0486CEB800E47090, 6BA939AE065692DC00AE7890, 6B0D0363072EC7B6009A7725, + 3B3F0229073EF1A3003AB1C7, ); isa = PBXResourcesBuildPhase; runOnlyForDeploymentPostprocessing = 0; @@ -1068,6 +1130,7 @@ 6B34D117072A2CBA00D08DB1, 6B34D190072A2E7400D08DB1, 49FE4958073A0868004027E5, + 3B3F024C073EF405003AB1C7, ); isa = PBXSourcesBuildPhase; runOnlyForDeploymentPostprocessing = 0; diff --git a/Utilities/NSAppleScript+HandlerCalls.h b/Utilities/NSAppleScript+HandlerCalls.h new file mode 100644 index 0000000..2c2803f --- /dev/null +++ b/Utilities/NSAppleScript+HandlerCalls.h @@ -0,0 +1,24 @@ +/* + +NSAppleScript+HandlerCalls.h +ASHandlerTest +by Buzz Andersen + +More information at: http://www.scifihifi.com/weblog/mac/Cocoa-AppleEvent-Handlers.html + +This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit + +http://creativecommons.org/licenses/by/1.0/ + +or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, +California 94305, USA. + +*/ + +#import + +@interface NSAppleScript (HandlerCalls) + +- (NSAppleEventDescriptor *) callHandler: (NSString *) handler withArguments: (NSAppleEventDescriptor *) arguments errorInfo: (NSDictionary **) errorInfo; + +@end diff --git a/Utilities/NSAppleScript+HandlerCalls.m b/Utilities/NSAppleScript+HandlerCalls.m new file mode 100644 index 0000000..f7e820c --- /dev/null +++ b/Utilities/NSAppleScript+HandlerCalls.m @@ -0,0 +1,51 @@ +/* + +NSAppleScript+HandlerCalls.m +ASHandlerTest +by Buzz Andersen + +More information at: http://www.scifihifi.com/weblog/mac/Cocoa-AppleEvent-Handlers.html + +This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit + +http://creativecommons.org/licenses/by/1.0/ + +or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, +California 94305, USA. + +*/ + +#import "NSAppleScript+HandlerCalls.h" + +@implementation NSAppleScript (HandlerCalls) + +- (NSAppleEventDescriptor *) callHandler: (NSString *) handler withArguments: (NSAppleEventDescriptor *) arguments errorInfo: (NSDictionary **) errorInfo { + NSAppleEventDescriptor* event; + NSAppleEventDescriptor* targetAddress; + NSAppleEventDescriptor* subroutineDescriptor; + NSAppleEventDescriptor* result; + + /* This will be a self-targeted AppleEvent, so we need to identify ourselves using our process id */ + int pid = [[NSProcessInfo processInfo] processIdentifier]; + targetAddress = [[NSAppleEventDescriptor alloc] initWithDescriptorType: typeKernelProcessID bytes: &pid length: sizeof(pid)]; + + /* Set up our root AppleEvent descriptor: a subroutine call (psbr) */ + event = [[NSAppleEventDescriptor alloc] initWithEventClass: 'ascr' eventID: 'psbr' targetDescriptor: targetAddress returnID: kAutoGenerateReturnID transactionID: kAnyTransactionID]; + + /* Set up an AppleEvent descriptor containing the subroutine (handler) name */ + subroutineDescriptor = [NSAppleEventDescriptor descriptorWithString: handler]; + [event setParamDescriptor: subroutineDescriptor forKeyword: 'snam']; + + /* Add the provided arguments to the handler call */ + [event setParamDescriptor: arguments forKeyword: keyDirectObject]; + + /* Execute the handler */ + result = [self executeAppleEvent: event error: errorInfo]; + + [targetAddress release]; + [event release]; + + return result; +} + +@end diff --git a/defines.h b/defines.h index 358a530..32a1e79 100644 --- a/defines.h +++ b/defines.h @@ -25,7 +25,12 @@ #define kEXTENDED_COLUMN @"extended" #define kURL_COLUMN @"url" -#define DCAPIPostPboardType @"kDCAPIPostPboardType" +#define kDCAPIPostPboardType @"DCAPIPostPboardType" + +#define kDCSafariScriptLibrary @"safari_script" +#define kDCScriptType @"scpt" +#define kDCSafariGetCurrentURL @"fetch_safari_url" +#define kScriptError @"ERROR" #define AWOOSTER_CHANGES 1 #define AWOOSTER_DEBUG 0