From f0e656abd33789d8ad00a95c89b13bd891ae3879 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Thu, 14 Oct 2010 19:49:59 +0000 Subject: Split out AppleScript support into a category of SPAppController. --- Source/SPAppController.h | 1 - Source/SPAppController.m | 108 ++-------------------------- Source/SPAppleScriptSupport.h | 30 ++++++++ Source/SPAppleScriptSupport.m | 135 +++++++++++++++++++++++++++++++++++ sequel-pro.xcodeproj/project.pbxproj | 6 ++ 5 files changed, 176 insertions(+), 104 deletions(-) create mode 100644 Source/SPAppleScriptSupport.h create mode 100644 Source/SPAppleScriptSupport.m diff --git a/Source/SPAppController.h b/Source/SPAppController.h index 999f5202..f5e92f16 100644 --- a/Source/SPAppController.h +++ b/Source/SPAppController.h @@ -39,7 +39,6 @@ NSURL *_sessionURL; NSMutableDictionary *_spfSessionDocData; - } // Window management diff --git a/Source/SPAppController.m b/Source/SPAppController.m index c6d89c02..c619b3a4 100644 --- a/Source/SPAppController.m +++ b/Source/SPAppController.m @@ -880,106 +880,6 @@ } } -#pragma mark - -#pragma mark AppleScript support - -//////////////// Examples to catch AS core events - maybe for further stuff -// - (void)handleQuitEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent -// { -// [NSApp terminate:self]; -// } -// - (void)handleOpenEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent -// { -// NSLog(@"OPEN "); -// } -// -// - (void)applicationWillFinishLaunching:(NSNotification *)aNotification -// { -// NSAppleEventManager *aeManager = [NSAppleEventManager sharedAppleEventManager]; -// [aeManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication]; -// [aeManager setEventHandler:self andSelector:@selector(handleOpenEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEOpenApplication]; -// } - - -/** - * Is needed to interact with AppleScript for set/get internal SP variables - */ -- (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key -{ - NSLog(@"Not yet implemented."); - - return NO; -} - -/** - * AppleScript calls that method to get the available documents - */ -- (NSArray *)orderedDocuments -{ - NSMutableArray *orderedDocuments = [NSMutableArray array]; - - for (NSWindow *aWindow in [self orderedWindows]) { - if ([[aWindow windowController] isMemberOfClass:[SPWindowController class]]) { - [orderedDocuments addObjectsFromArray:[[aWindow windowController] documents]]; - } - } - - return orderedDocuments; -} - -/** - * Support for 'make new document'. - * TODO: following tab support this has been disabled - need to discuss reimplmenting vs syntax. - */ -- (void)insertInOrderedDocuments:(SPDatabaseDocument *)doc -{ - [self newWindow:self]; - - // Set autoconnection if appropriate - if ([[NSUserDefaults standardUserDefaults] boolForKey:SPAutoConnectToDefault]) { - [[self frontDocument] connect]; - } - -} - -/** - * AppleScript calls that method to get the available windows. - */ -- (NSArray *)orderedWindows -{ - return [NSApp orderedWindows]; -} - -/** - * AppleScript handler to quit Sequel Pro - * This handler is needed to allow to quit SP via the Dock or AppleScript after activating it by using AppleScript - */ -- (id)handleQuitScriptCommand:(NSScriptCommand *)command -{ - [NSApp terminate:self]; - return nil; -} - -/** - * AppleScript handler - * This handler is needed to catch an 'open' command if no argument was passed which would cause a crash - */ -- (id)handleOpenScriptCommand:(NSScriptCommand *)command -{ - return nil; -} - -/** - * AppleScript handler for print - * This handler prints the active view - */ -- (id)handlePrintScriptCommand:(NSScriptCommand *)command -{ - SPDatabaseDocument *frontDoc = [self frontDocument]; - if (frontDoc && ![frontDoc isWorking] && ![[frontDoc connectionID] isEqualToString:@"_"]) - [frontDoc startPrintDocumentOperation]; -} - #pragma mark - /** @@ -988,11 +888,13 @@ - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; - - if(_spfSessionDocData) [_spfSessionDocData release], _spfSessionDocData = nil; + [prefsController release], prefsController = nil; [aboutController release], aboutController = nil; - if(_sessionURL) [_sessionURL release], _sessionURL = nil; + + if (_sessionURL) [_sessionURL release], _sessionURL = nil; + if (_spfSessionDocData) [_spfSessionDocData release], _spfSessionDocData = nil; + [super dealloc]; } diff --git a/Source/SPAppleScriptSupport.h b/Source/SPAppleScriptSupport.h new file mode 100644 index 00000000..4878dc4b --- /dev/null +++ b/Source/SPAppleScriptSupport.h @@ -0,0 +1,30 @@ +// +// $Id$ +// +// SPAppleScriptSupport.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on October 14, 2010 +// Copyright (c) 2010 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +#import "SPAppController.h" + +@interface SPAppController (SPAppleScriptSupport) + +@end diff --git a/Source/SPAppleScriptSupport.m b/Source/SPAppleScriptSupport.m new file mode 100644 index 00000000..73975a26 --- /dev/null +++ b/Source/SPAppleScriptSupport.m @@ -0,0 +1,135 @@ +// +// $Id$ +// +// SPAppleScriptSupport.m +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on October 14, 2010 +// Copyright (c) 2010 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +#import "SPAppleScriptSupport.h" +#import "SPWindowController.h" +#import "SPConstants.h" + +@implementation SPAppController (SPAppleScriptSupport) + +//////////////// Examples to catch AS core events - maybe for further stuff +// - (void)handleQuitEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent +// { +// [NSApp terminate:self]; +// } +// - (void)handleOpenEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent +// { +// NSLog(@"OPEN "); +// } +// +// - (void)applicationWillFinishLaunching:(NSNotification *)aNotification +// { +// NSAppleEventManager *aeManager = [NSAppleEventManager sharedAppleEventManager]; +// [aeManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication]; +// [aeManager setEventHandler:self andSelector:@selector(handleOpenEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEOpenApplication]; +// } + +/** + * Is needed to interact with AppleScript for set/get internal SP variables + */ +- (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key +{ + NSLog(@"Not yet implemented."); + + return NO; +} + +/** + * AppleScript call to get the available documents. + */ +- (NSArray *)orderedDocuments +{ + NSMutableArray *orderedDocuments = [NSMutableArray array]; + + for (NSWindow *aWindow in [self orderedWindows]) + { + if ([[aWindow windowController] isMemberOfClass:[SPWindowController class]]) { + [orderedDocuments addObjectsFromArray:[[aWindow windowController] documents]]; + } + } + + return orderedDocuments; +} + +/** + * AppleScript support for 'make new document'. + * + * TODO: following tab support this has been disabled - need to discuss reimplmenting vs syntax. + */ +- (void)insertInOrderedDocuments:(SPDatabaseDocument *)doc +{ + [self newWindow:self]; + + // Set autoconnection if appropriate + if ([[NSUserDefaults standardUserDefaults] boolForKey:SPAutoConnectToDefault]) { + [[self frontDocument] connect]; + } +} + +/** + * AppleScript call to get the available windows. + */ +- (NSArray *)orderedWindows +{ + return [NSApp orderedWindows]; +} + +/** + * AppleScript handler to quit Sequel Pro + * + * This handler is required to allow termination via the Dock or AppleScript event after activating it using AppleScript + */ +- (id)handleQuitScriptCommand:(NSScriptCommand *)command +{ + [NSApp terminate:self]; + + return nil; +} + +/** + * AppleScript open handler + * + * This handler is required to catch the 'open' command if no argument was passed which would cause a crash. + */ +- (id)handleOpenScriptCommand:(NSScriptCommand *)command +{ + return nil; +} + +/** + * AppleScript print handler + * + * This handler prints the active view. + */ +- (id)handlePrintScriptCommand:(NSScriptCommand *)command +{ + SPDatabaseDocument *frontDoc = [self frontDocument]; + + if (frontDoc && ![frontDoc isWorking] && ![[frontDoc connectionID] isEqualToString:@"_"]) { + [frontDoc startPrintDocumentOperation]; + } +} + +@end diff --git a/sequel-pro.xcodeproj/project.pbxproj b/sequel-pro.xcodeproj/project.pbxproj index f622db3a..e4364456 100644 --- a/sequel-pro.xcodeproj/project.pbxproj +++ b/sequel-pro.xcodeproj/project.pbxproj @@ -69,6 +69,7 @@ 1792C13210AD752100ABE758 /* DatabaseServerVariables.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1792C13010AD752100ABE758 /* DatabaseServerVariables.xib */; }; 1792C13710AD75C800ABE758 /* SPServerVariablesController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1792C13610AD75C800ABE758 /* SPServerVariablesController.m */; }; 1792C26110AE1A2D00ABE758 /* SPConnectionDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1792C26010AE1A2D00ABE758 /* SPConnectionDelegate.m */; }; + 1798AB911267924D000D946A /* SPAppleScriptSupport.m in Sources */ = {isa = PBXBuildFile; fileRef = 1798AB901267924D000D946A /* SPAppleScriptSupport.m */; }; 179ECECA11F265FC009C6A40 /* libbz2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 179ECEC611F265EE009C6A40 /* libbz2.dylib */; }; 179F15060F7C433C00579954 /* SPEditorTokens.l in Sources */ = {isa = PBXBuildFile; fileRef = 179F15050F7C433C00579954 /* SPEditorTokens.l */; }; 17A20AC6124F9B110095CEFB /* SPServerSupport.m in Sources */ = {isa = PBXBuildFile; fileRef = 17A20AC5124F9B110095CEFB /* SPServerSupport.m */; }; @@ -587,6 +588,8 @@ 1792C25F10AE1A2D00ABE758 /* SPConnectionDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPConnectionDelegate.h; sourceTree = ""; }; 1792C26010AE1A2D00ABE758 /* SPConnectionDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPConnectionDelegate.m; sourceTree = ""; }; 1798AB0C12676CD9000D946A /* localize.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = localize.sh; sourceTree = ""; }; + 1798AB8F1267924D000D946A /* SPAppleScriptSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPAppleScriptSupport.h; sourceTree = ""; }; + 1798AB901267924D000D946A /* SPAppleScriptSupport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPAppleScriptSupport.m; sourceTree = ""; }; 179ECEC611F265EE009C6A40 /* libbz2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libbz2.dylib; path = usr/lib/libbz2.dylib; sourceTree = SDKROOT; }; 179F15040F7C433C00579954 /* SPEditorTokens.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPEditorTokens.h; sourceTree = ""; }; 179F15050F7C433C00579954 /* SPEditorTokens.l */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.lex; path = SPEditorTokens.l; sourceTree = ""; }; @@ -1679,6 +1682,8 @@ 17E6414B0EF01EF6001BC333 /* SPAppController.m */, 58A8A78F11A036C000B95749 /* SPWindowController.h */, 58A8A79011A036C000B95749 /* SPWindowController.m */, + 1798AB8F1267924D000D946A /* SPAppleScriptSupport.h */, + 1798AB901267924D000D946A /* SPAppleScriptSupport.m */, 173E70A6107FF61D008733C9 /* Main View Controllers */, 173E70D2107FF687008733C9 /* Subview Controllers */, 173E70D4107FF6E7008733C9 /* Data Controllers */, @@ -2852,6 +2857,7 @@ 17A20AC6124F9B110095CEFB /* SPServerSupport.m in Sources */, BC2898F3125F4488001B50E1 /* SPGeometryDataView.m in Sources */, 17148565125F5FF500321285 /* SPDatabaseCharacterSets.m in Sources */, + 1798AB911267924D000D946A /* SPAppleScriptSupport.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; -- cgit v1.2.3