Hi scottedge,
I wanted to play with using applescript in a cocoa app a bit more, and instead of putting the code into the
main() as a string, I wanted to do it as a source file.
I had some luck with this but still have some things to sort out…
But while I was goggling, I was reminded of the Scripting Bridge.( and noted you posted about it)
And wanted to share what I have worked out so far…
In short this has allowed me to not use any applescript as such to do the same thing and I think maybe simpler
once the learning curve is topped…
Have a look at the Scripting Bridge docs.
The script below works in a cocoa app, and the setup is very simple. Which I put comments in for you to follow.
(which also maybe longer than the code :D)
Getting the syntax is the main learning curve.
And you can look in the header file for the converted definitions.
one thing, when you create the header file for Airfoil. You will get an error:
error: duplicate declaration of method ‘-open:’
This is simple to fix.
In the “Airfoil.h” file you will have two methods with the same selector name.
[i]- (void) open:(NSURL *)x; // Open an object.
- (AirfoilDocument *) open:(NSURL *)x; // Open an objec[/i]t.
just change one of the names, and rebuild
- (void) openx:(NSURL *)x; // Open an object
I do not think its something to worry about.
I should point out I am a beginner at Objective C, and the only other language i know a bit about is AS. and
a little scattering of Unix.
I have not even got to the parts about pointers and casting them blah blah which i know I am going to need…
And I say this because I know that in this script most likely reflects that.
//
// main.m
// AirfoilCococa
//
// Created by markhunte on 25/10/2009.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <ScriptingBridge/ScriptingBridge.h> // add foundation//
#import "Airfoil.h"
/* //COMMENT/
Run the command below in Terminal, it will create a header file for the application you want to control.
This header is the converted form of the applescipt dictionary for the app.
Note the form: sdef /path/to/the.app | sdp -basename shortNameOfappWithoutExtension
The file will be created in the current directory of the terminal.
Add the file to your project. and build.
then add the #import for it.
command below:
sdef /Applications/Airfoil.app | sdp -fh --basename Airfoil
*/
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
/* //COMMENT/
Quote from the apple docs:
The recommended method for creating an instance of a scriptable application is applicationWithBundleIdentifier:. The method can locate an application on a system even if the user has renamed the application, and it doesn't require you to know where an application is installed in the file system (which could be anywhere). he following line of code creates an instance of the iTunes application:
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
*/
AirfoilApplication *Airfoil = [SBApplication applicationWithBundleIdentifier:@"com.rogueamoeba.Airfoil"];
SBElementArray * speaker =[Airfoil speakers] ;
id item;
NSString * state;
NSString * searchForMe;
NSNumber *theState, * num1;
num1 =[NSNumber numberWithUnsignedInt:1];
NSLog(@"======= Avalible Speakers %i ========",[speaker count]);
if ([speaker count] > 0) {
for (item in speaker) {
NSString * theSpeaker = [item name];
theState = [NSNumber numberWithUnsignedInt: [item connected]];
if (theState == num1) {
searchForMe = @"true";
state = @"ON";
}else {
searchForMe = @"false";
state = @"OFF";
}
NSLog(@" %@ : %@ ",theSpeaker,state);
}
}
[pool drain];
return 0;
}