Re: Won't Applescript command use defined ivars?
Re: Won't Applescript command use defined ivars?
- Subject: Re: Won't Applescript command use defined ivars?
- From: Chris Paveglio <email@hidden>
- Date: Wed, 31 Aug 2011 07:58:26 -0700 (PDT)
Ah OK that sheds some light on things. Thanks Martin.
Yes I have subclassed NSScriptCommand, as it seemed to be the only way I could get any Applescript command to work by using "performDefaultImplementation". But I am going to need the application to use the instance of the class from when it's started, not make a new instance every time the script command is given. I have to connect to a database, and for performance, would rather not have to create/open/use/close/destroy that connection every time it's called (could be called a lot). I would rather have the connection created and ready to use, and then just pass in my calls as needed, until the app quits (it's going to run in the background).
(Sorry for my poor use of terminology, I come from an Applescript background and still cross pollinate my terms sometimes.)
I was looking at the Apple sample code "simple scripting verbs" and "simple scripting". I could not get a verb command to work unless I did it this way, but there must be a way where I don't have to use "performDefaultImplementation" and can specify what method to run in the SDEF.
#import <Cocoa/Cocoa.h>
#import "ODBCKit/ODBCKit.h"
@interface Clipper_Tracking_DaemonAppDelegate : NSScriptCommand
{
NSWindow*window;
NSString*userName;
ODBCConnection*theConnection;
}
-(id)performDefaultImplementation;
-(void)logEventDoc:(NSString *)docPath operType:(NSString *)opType withTC:(long)tcNumber;
@property(assign) IBOutletNSWindow *window;
@end
--------------
@implementation Clipper_Tracking_DaemonAppDelegate
@synthesizewindow;
-(id)performDefaultImplementation
{
//self directParameter is the first data in the script command
NSDictionary * theArguments = [self evaluatedArguments];
NSString *filePath = [self directParameter];
//argument are what you put in the <cocoa key="operationType"/> in the SDEF
//and pull out with the objectForKey (string)
//it is not the actual argument in the method name
NSNumber *tcNumber = [theArguments objectForKey:@"tcNumber"];
NSString *operationType = [theArguments objectForKey:@"operationType"];
[self logEventDoc:filePath operType:operationType withTC:[tcNumber longValue]];
returnnil;
}
-(void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
userName = NSUserName(); //I also may have tried to retain it but no difference in outcome when Applescript calls the method
theConnection = [[ODBCConnection alloc] init];
[theConnection setDsn:@"DatabaseName"];
[theConnection setUserName:@"theserverlogin"];
[theConnection setPassword:@"theserverpassw"];
}
-(void)logEventDoc:(NSString *)docPath operType:(NSString *)opType withTC:(long)tcNumber
{
//post data to SQL
NSString *theCommand = [NSString stringWithFormat:@"SpLogEventEtc %@ %D %@ %@", docPath, tcNumber, opType, userName];
//[theConnection execCommand:theCommand]; //this is the ODBCKit command to talk to the sql server
//for testing, let's just do our favorite NSLog
NSLog(@"%@", theCommand);
//[theConnection close]; //needed?
}
@end
---------------
Here's the SDEF in case that helps:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<!-- declare the namespace for using XInclude so we can include the standard suite -->
<dictionary xmlns:xi="http://www.w3.org/2003/XInclude">
<!-- use XInclude to include the standard suite -->
<xi:include href="file:///System/Library/ScriptingDefinitions/CocoaStandard.sdef" xpointer="xpointer(/dictionary/suite)"/>
<!-- specific suite(s) for the application follow... -->
<suite name="Notification Suite" code="cmTD" description="Commands to activate the ESP logging system">
<command name="log" code="cmTDLogZ" description="Logs open or save of ID documents.">
<cocoa class="Clipper_Tracking_DaemonAppDelegate"/>
<direct-parameter description="file path of the document" type="text"/>
<parameter name="operation type" code="OPty" type="text" description="operation open or save">
<cocoa key="operationType"/>
</parameter>
<parameter name="with TC" code="TCnb" type="number" description="the turbo copy number of the document">
<cocoa key="tcNumber"/>
</parameter>
</command>
</suite>
</dictionary>
----- Original Message -----
From: Martin Wierschin <email@hidden>
To: Chris Paveglio <email@hidden>
Cc: Cocoa Dev List <email@hidden>
Sent: Tuesday, August 30, 2011 5:25 PM
Subject: Re: Won't Applescript command use defined ivars?
> If I have an ivar that is global in scope,
As Scott mentioned, your ivar isn't global. Each instance of your class has its own ivar. Also as he mentioned, you seem to be ignoring the memory management guidelines. That's not the immediate cause of your troubles, but is important for you to understand and fix.
What exactly is your class? Your post omits important parts:
> @interface...
> {
> NSString *userName;
> }
Are you subclassing NSScriptCommand? If so, then I'm guessing that the AppleScript machinery is creating a new instance of your class every time you run your AppleScript. Which means that your instance is not in existence when the -applicationDidFinishLaunching method might be called, and hence why your ivar is nil.
~Martin
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden