Re: Objective-C and AppleScript
Re: Objective-C and AppleScript
- Subject: Re: Objective-C and AppleScript
- From: has <email@hidden>
- Date: Mon, 1 Sep 2008 22:44:17 +0100
John Love wrote:
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 you use objc-appscript (I won't suggest the sdef/sdp/Scripting
Bridge toolchain as an alternative as I know it has problems with
Excel), you can check if Excel is running by calling the MEApplication
object's -isRunning method:
// To create glue: osaglue -o MEGlue -p ME Microsoft\ Excel
MEApplication *microsoftExcel = [MEApplication
applicationWithBundleID: @"com.microsoft.excel"];
if ([microsoftExcel isRunning]) {
// do stuff here...
}
-- 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.
Code generation is fundamentally evil; if you need to pass values to a
script, pack them into an NSAppleEventDescriptor and pass them to a
handler in a compiled script via -[NSAppleScript
executeAppleEvent:error:].
That said, appscript should suffice for general Apple event IPC, so
the only time you should need to mess about with NSAppleScript is if
you need to execute user-supplied scripts (appscript is pretty handy
for that too, btw).
2) tell application "Microsoft Excel" to return (name of every
window contains "some title")
I would suggest doing:
tell application "Microsoft Excel" to return (exists window "some
title")
as it's a bit clearer in meaning. The objc-appscript equivalent would
be:
MEReference *ref = [[microsoftExcel windows] byName: @"some title"];
id result = [[ref exists] send];
BOOL windowExists = [result boolValue];
The ASTranslate tool on the appscript website is very handy if you
need help translating application commands from AppleScript to ObjC
syntax; there's also ASDictionary for exporting application
dictionaries in appscript format.
HTH
has
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net
_______________________________________________
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