Objective-C and AppleScript
Objective-C and AppleScript
- Subject: Objective-C and AppleScript
- From: John Love <email@hidden>
- Date: Mon, 1 Sep 2008 15:49:06 -0400
I am trying to convert as much as I can of my former Studio code over
to Obj-C and thanks to this Mailing List I have been successful so
far .. but here's a stumper or two:
Here are 2 AppleScript statements that work in Studio:
-- #1 works in Obj-C, so my question is = isn't there a more *direct*
Obj-C call to do the same thing,
-- rather than call NSAppleScript's executeAndReturnError method? It
just seems that a one or two
-- system calls should effect the same result?
1) tell application "System Events" to return (name of every
application process contains "Microsoft Excel") -- see ExcelAppActive
below
=====
-- If I hard-code the actual name of the file
stringByAppendingString:@"some title" (see theWorkbookActive method
below),
-- everything works dandy.
--
-- But ... this name is actually a instance parameter, NSString*
itsFileName, defined in my .h file, and dynamically set in my .m file.
-- So, I type stringByAppendingString:itsFileName -- but then my app
crashes.
2) tell application "Microsoft Excel" to return (name of every window
contains "some title")
Here is the standard (I believe) method to execute the passed Script:
- (NSAppleEventDescriptor*) ExecAppleScript:(NSString*)theScript {
NSAppleScript *scriptObject;
NSDictionary *errorInfo;
NSAppleEventDescriptor *execResult;
scriptObject = [[NSAppleScript alloc] initWithSource:theScript];
errorInfo = [[NSDictionary alloc] init];
execResult = [scriptObject executeAndReturnError:&errorInfo];
return execResult; // success = (execResult != nil)
}
- (BOOL) ExcelAppActive {
BOOL ExcelActive = FALSE;
NSAppleEventDescriptor *execResult;
NSArray *ExcelActiveScriptArray = [NSArray arrayWithObjects:
@"tell application \"System Events\"",
@"return (name of every application process contains
\"Microsoft Excel\")",
@"end tell",
nil];
NSString *ExcelActiveScriptString =
[ExcelActiveScriptArray componentsJoinedByString:@"\n"];
/*
tell application "System Events"
return (name of every application process contains "Microsoft Excel")
end tell
*/
execResult = [self ExecAppleScript:ExcelActiveScriptString];
if (execResult != nil) { // success
ExcelActive = [execResult booleanValue];
}
if (!ExcelActive) {
// do something here
}
return ExcelActive;
}
- (BOOL) theWorkbookActive {
BOOL wbActive = FALSE;
NSAppleEventDescriptor *execResult;
if ([self ExcelAppActive]) {
NSString *WorkbookActiveScript =
@"tell application \"Microsoft Excel\" to return (name of every
window contains \"";
WorkbookActiveScript = [WorkbookActiveScript
stringByAppendingString:itsFileName];
WorkbookActiveScript = [WorkbookActiveScript
stringByAppendingString:@"\")"];
/*
tell application "Microsoft Excel" to return (name of every window
contains "some title")
*/
NSLog(WorkbookActiveScript);
execResult = [self ExecAppleScript:WorkbookActiveScript];
if (execResult != nil) { // success
wbActive = [execResult booleanValue];
}
if (!wbActive) {
// do something here
}
}
else {
// [self ExcelAppActive] sets and displays Error
}
return wbActive;
}
John Love
_______________________________________________
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