Re: External Program Control from within Cocoa App
Re: External Program Control from within Cocoa App
- Subject: Re: External Program Control from within Cocoa App
- From: has <email@hidden>
- Date: Tue, 4 Nov 2008 00:12:09 +0000
Ronald Ramdhan wrote:
I am working on a project where I would like to control external
programs from within my Cocoa Application. For example, the ability to
control a program like PowerPoint from within my code.
Is there anyway to do this programmatically without the use of Apple
Script? I have already looked at NSWorkspace and noticed the commands
to launch files, but what I need is the ability to control the
launched program(e.g. Start a Slideshow).
Using objc-appscript (runs on 10.3.9+; most reliable solution after
AppleScript itself; see my sig for link; yada-yada-yada):
#import "MPGlue/MPGlue.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MPApplication *powerpoint = [MPApplication applicationWithName:
@"Microsoft PowerPoint"];
[[powerpoint activate] send];
MPReference * slideShowSettings = [[powerpoint
activePresentation] slideShowSettings];
[[slideShowSettings showType] setItem: [MPConstant
slideShowTypeSpeaker]];
[[slideShowSettings advanceMode] setItem: [MPConstant
slideShowAdvanceUseSlideTimings]];
[[[[powerpoint activePresentation] slideShowSettings]
runSlideShow] send];
[pool drain];
return 0;
}
(Error handling omitted for simplicity. See documentation and examples
in the objc-appscript repository for more.)
The glue code was generated via ASDictionary 0.11.0 (it's easier than
using objc-appscript's osaglue tool which requires extra
installation): just select the application(s) you want glues for,
check the 'objc-appscript glue' option, edit the class name prefixes
for each application in the table view if you want (I'll do nicer
default prefixes in the next release), and hit 'Export'. ASDictionary
can also export application dictionaries in objc-appscript format,
which is much easier to read than Script Editor's dictionary viewer
(which only supports AppleScript syntax) or the raw ObjC headers
(which only contain the information that objc-appscript actually needs).
Also, I will confess that I've never scripted Powerpoint before, so
just did a web search to find an existing AppleScript example for
running slide shows at <http://aron.ahmadia.net/article/30/script-and-application-for-starting-a-powerpoint-show
>:
tell application "Microsoft PowerPoint"
set mySSS to slide show settings of active presentation
set show type of mySSS to slide show type speaker
set advance mode of mySSS to slide show advance use slide timings
set sShow to run slide show mySSS
end tell
I then ran this script in ASTranslate to convert each Powerpoint
command to ObjC syntax, then refactored that raw output into the form
you see above.
The only other thing was that I noticed Powerpoint wouldn't start the
show unless I inserted an 'activate' command to bring it frontmost
first. This extra requirement wasn't obvious (unless it's in the
Powerpoint scripting manual, which I think you can get from the MS
website), but after using AppleScript a few years you get used to
doing this sort of guesswork.
Depending on your exact requirements you may need to change it a bit,
but at least it's a starting point.
For getting help with automating specific applications you're best
asking over on AppleScript-users as that's where most of the experts
are (you'll generally get AppleScript-based solutions, of course, and
will need to convert to ObjC yourself), or maybe the Mac Office
newsgroups if it's Office in particular.
Oh, and while I get the impression that you probably don't like
AppleScript very much (fair enough), if you do need to do much
automation work then as a practical matter you're going to have to
learn it a bit anyway. It's what the vast majority of documentation
and examples are written in, and what most experienced application
scripters use, and you'll have a hard time understanding them without
it. Personally I'd recommend getting a copy of Matt Neuburg's
'AppleScript: The Definitive Guide', 2nd ed. [1] which provides a
programmer-friendly introduction to AppleScript, and isn't shy about
discussing the language's many flaws as well as its features.
HTH
has
[1] Covers up to 10.4; hopefully Matt'll do an update for 10.6 as text
handling changed a bit in 10.5, but still full of good and useful
information.
--
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