• 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: Passing Variables to AppleScript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing Variables to AppleScript


  • Subject: Re: Passing Variables to AppleScript
  • From: has <email@hidden>
  • Date: Sun, 19 Oct 2008 14:14:36 +0100

Pierce Freeman wrote:

I am attempting to create a little application that will take an application
name from the user, and then close it for them. I am attempting do this by
getting the string in Cocoa, then passing this to AppleScript... But I don't
know if Cocoa can pass variables to AppleScript.


There are ways of passing values to AppleScript, and there are ways of sending Apple events directly from ObjC.
Those are probably overkill for sending a basic 'quit' event though, which is a simple cut-n-paste solution. Here's what I use:


#include <Carbon/Carbon.h>

OSStatus QuitApplicationProcessWithPID(pid_t pid) {
   AppleEvent evt, res;
   AEDesc errDesc;
   OSStatus err;

// build and send a 'quit' event
err = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication,
typeKernelProcessID,
&pid, sizeof(pid),
kAutoGenerateReturnID,
kAnyTransactionID,
&evt, NULL, "");
if (err) return err;
err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
AEDisposeDesc(&evt);
// note: process may quit without replying
if (err == connectionInvalid) return noErr;
if (err) return err;
// check if reply event contains an error number, e.g. userCanceledErr
err = AEGetParamDesc(&res, keyErrorNumber, typeSInt32, &errDesc);
if (err == noErr) {
AEGetDescData(&errDesc, &err, sizeof(err));
AEDisposeDesc(&res);
} else if (err == errAEDescNotFound)
err = noErr;
return err;
}



Use -[NSWorkspace launchedApplications] to look up the application's process id based on its name.


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: Views, frames, and bounds
  • Next by Date: Re: Views, frames, and bounds
  • Previous by thread: Re: Passing Variables to AppleScript
  • Next by thread: Resize an image view
  • Index(es):
    • Date
    • Thread