Skip to content

Commit

Permalink
[RFE 1062042] Initial import providing the "post current Safari docum…
Browse files Browse the repository at this point in the history
…ent" feature.
  • Loading branch information
daschae committed Nov 8, 2004
1 parent cf2cf53 commit 5799c86
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 17 deletions.
5 changes: 5 additions & 0 deletions Application/AppController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -58,6 +59,8 @@

BOOL useExtendedSearch;

NSAppleScript *safariScript;

#ifdef AWOOSTER_CHANGES
FullTextIndex *textIndex;
BOOL useFullTextSearch;
Expand Down Expand Up @@ -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
Expand Down
64 changes: 58 additions & 6 deletions Application/AppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand All @@ -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];
Expand Down Expand Up @@ -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;
}
Expand All @@ -662,7 +671,7 @@ - (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDr
- (BOOL) tableView: (NSTableView *) tableView acceptDrop: (id <NSDraggingInfo>) 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];
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -1097,6 +1148,7 @@ - (void) dealloc {
#ifdef AWOOSTER_CHANGES
[textIndex release];
#endif
[safariScript release];
[super dealloc];
}

Expand Down
83 changes: 73 additions & 10 deletions Delicious Client.xcode/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@
sourceTree = "<group>";
};
1058C7A1FEA54F0111CA2CBB = {
fallbackIsa = PBXFileReference;
isa = PBXFrameworkReference;
isa = PBXFileReference;
lastKnownFileType = wrapper.framework;
name = Cocoa.framework;
path = /System/Library/Frameworks/Cocoa.framework;
Expand Down Expand Up @@ -123,6 +122,7 @@
6BD8D4C407205239008CA80A,
6BD8D4B607205221008CA80A,
49FE4954073A0868004027E5,
3B3F0225073EF17A003AB1C7,
6BD8D4A707205205008CA80A,
29B97315FDCFA39411CA2CEA,
29B97317FDCFA39411CA2CEA,
Expand Down Expand Up @@ -199,17 +199,15 @@
sourceTree = "<group>";
};
29B97324FDCFA39411CA2CEA = {
fallbackIsa = PBXFileReference;
isa = PBXFrameworkReference;
isa = PBXFileReference;
lastKnownFileType = wrapper.framework;
name = AppKit.framework;
path = /System/Library/Frameworks/AppKit.framework;
refType = 0;
sourceTree = "<absolute>";
};
29B97325FDCFA39411CA2CEA = {
fallbackIsa = PBXFileReference;
isa = PBXFrameworkReference;
isa = PBXFileReference;
lastKnownFileType = wrapper.framework;
name = Foundation.framework;
path = /System/Library/Frameworks/Foundation.framework;
Expand Down Expand Up @@ -239,6 +237,70 @@
//322
//323
//324
//3B0
//3B1
//3B2
//3B3
//3B4
3B3F0225073EF17A003AB1C7 = {
children = (
3B3F0228073EF1A3003AB1C7,
);
isa = PBXGroup;
name = Scripts;
refType = 4;
sourceTree = "<group>";
};
3B3F0228073EF1A3003AB1C7 = {
fileEncoding = 30;
isa = PBXFileReference;
lastKnownFileType = sourcecode.scpt;
name = safari_script.scpt;
path = Scripts/safari_script.scpt;
refType = 4;
sourceTree = "<group>";
};
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 = "<group>";
};
3B3F024A073EF405003AB1C7 = {
fileEncoding = 30;
isa = PBXFileReference;
lastKnownFileType = sourcecode.c.objc;
name = "NSAppleScript+HandlerCalls.m";
path = "Utilities/NSAppleScript+HandlerCalls.m";
refType = 4;
sourceTree = "<group>";
};
3B3F024B073EF405003AB1C7 = {
fileRef = 3B3F0249073EF405003AB1C7;
isa = PBXBuildFile;
settings = {
};
};
3B3F024C073EF405003AB1C7 = {
fileRef = 3B3F024A073EF405003AB1C7;
isa = PBXBuildFile;
settings = {
};
};
//3B0
//3B1
//3B2
//3B3
//3B4
//490
//491
//492
Expand Down Expand Up @@ -293,8 +355,6 @@
//4A3
//4A4
4A9504CCFFE6A4B311CA0CBA = {
buildRules = (
);
buildSettings = {
COPY_PHASE_STRIP = NO;
DEBUGGING_SYMBOLS = YES;
Expand All @@ -310,8 +370,6 @@
name = Development;
};
4A9504CDFFE6A4B311CA0CBA = {
buildRules = (
);
buildSettings = {
COPYING_PRESERVES_HFS_DATA = NO;
COPY_PHASE_STRIP = YES;
Expand Down Expand Up @@ -802,6 +860,8 @@
};
6BD8D4A707205205008CA80A = {
children = (
3B3F0249073EF405003AB1C7,
3B3F024A073EF405003AB1C7,
6BA936C20655EEC400AE7890,
6BA936C30655EEC400AE7890,
6B212599071D935F0044339D,
Expand Down Expand Up @@ -1014,6 +1074,7 @@
6B34D116072A2CBA00D08DB1,
6B34D18F072A2E7400D08DB1,
49FE4957073A0868004027E5,
3B3F024B073EF405003AB1C7,
);
isa = PBXHeadersBuildPhase;
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -1031,6 +1092,7 @@
8D11072B0486CEB800E47090,
6BA939AE065692DC00AE7890,
6B0D0363072EC7B6009A7725,
3B3F0229073EF1A3003AB1C7,
);
isa = PBXResourcesBuildPhase;
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1068,6 +1130,7 @@
6B34D117072A2CBA00D08DB1,
6B34D190072A2E7400D08DB1,
49FE4958073A0868004027E5,
3B3F024C073EF405003AB1C7,
);
isa = PBXSourcesBuildPhase;
runOnlyForDeploymentPostprocessing = 0;
Expand Down
24 changes: 24 additions & 0 deletions Utilities/NSAppleScript+HandlerCalls.h
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>

@interface NSAppleScript (HandlerCalls)

- (NSAppleEventDescriptor *) callHandler: (NSString *) handler withArguments: (NSAppleEventDescriptor *) arguments errorInfo: (NSDictionary **) errorInfo;

@end
51 changes: 51 additions & 0 deletions Utilities/NSAppleScript+HandlerCalls.m
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5799c86

Please sign in to comment.