• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSAppleScript question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSAppleScript question


  • Subject: Re: NSAppleScript question
  • From: has <email@hidden>
  • Date: Thu, 5 Jun 2008 00:31:17 +0100


Matthew Delves wrote:

Greetings and salutations,
Is there a way to pass arguments into an AppleScript file using
NSAppleScript and other cocoa/obj-c classes?

Yes, see NSAppleEventDescriptor and -[NSAppleScript executeAppleEvent:error:]. It's a bit of a chore for anything non- trivial, particularly since NSAppleEventDescriptor provides only limited bridging between Cocoa classes and AE types, although there are ways to simplify the process somewhat (e.g. see the CallAppleScriptHandler sample project in the objc-appscript svn repository).



The reason that I ask is I am wanting to find out the path to a song
in iTunes. I have a script which does this for hard coded values,
though am uncertain as to how to accomplish it (or if it is indeed
possible) using NSAppleScript.

NSAppleScript is best used in situations where you want users to supply the scripts to be executed (what's known as making your application attachable). For interacting with applications yourself, the lower-level Carbon Apple Event Manager (the AEBuild* functions and AESendMessage are okay for simple tasks, although they get dreadfully tedious for anything complex), or one of the high-level Cocoa-Apple event bridges. Example using objc-appscript:



#import "ITGlue/ITGlue.h"

// To create iTunes glue: osaglue -o ITGlue -p IT iTunes

int main (int argc, const char * argv[]) {
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	NSError *error;
	NSString *trackName = @"Real Men";

ITApplication *itunes = [[ITApplication alloc] initWithBundleID: @"com.apple.itunes"];

ITReference *trackRef = [[[[itunes libraryPlaylists] at: 1] fileTracks] byName: trackName];
id location = [[trackRef location] getItemWithError: &error];


if ([location isKindOfClass: [ASAlias class]])
NSLog(@"Track's file is at: %@", [location path]);
else if ([location isEqual: [ASConstant missingValue]])
NSLog(@"Track's file is missing.");
else
NSLog(@"Some error occurred: %@", error); // e.g. -1728, object not found

[itunes release];
[pool drain];
return 0;
}



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


  • Prev by Date: Re: invoking quicklook via code
  • Next by Date: Re: Manipulating images with meta-data
  • Previous by thread: Re: NSAppleScript question
  • Next by thread: Itunes video wall core animation
  • Index(es):
    • Date
    • Thread