1

I want to run a simple applescript in a cocoa application. I read the apple documentation on the matter but it was too confusing for me (a beginner) to understand.

tell application "iTunes" to play
1
  • nope. I could not get anything out of apples documentation Commented Jul 9, 2010 at 15:36

1 Answer 1

1

Per the documentation, you use the NSAppleScript class.

The very short API reference has a section called "Initializing a Script," one method of which is -initWithSource:, which takes an NSString. You'll create your object this way.

Once you have your script object, you can then either -compileAndReturnError: then -executeAndReturnError: as separate steps, or just -executeAndReturnError:, which - according to the documentation for that method - tries to compile the source first if it's not been already, then executes.

So, in theory, you could probably do all this in one line. (alloc, init..., autorelease, executeAndReturnError:) if you ignore errors like a naughty developer.

Note the warning that NSAppleScript can only be executed from the main thread (ie, not from an NSOperation/Queue or other threads).


Sign up to request clarification or add additional context in comments.

6 Comments

so heres my script object/code. NSAppleScript *ascript = [[NSAppleScript alloc]init]; [ascript executeAndReturnError:] how do i tell it what my script is
i dont see what im doing wrong. i dont know how to fill the argument after executeAndReturnError: this is literally my third day of objective c.
OK i've came up with this. But it doesn't seem to run the script because it doesnt play or pause my itunes. -(IBAction)playpause:(id)sender{ NSAppleScript *playScript; playScript = [[NSAppleScript alloc] initWithSource:@"iTunesPlayPause.scpt"]; [playScript executeAndReturnError:nil]; }
WHOOOOOOOOO!! i got it!!!!! initWithSource:@"" i should have put the applescript raw code. man i feel so accomplished.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.