C equivalent to applescript "tell X to open Y"

182 views Asked by At

Does anybody have a C library function that does the equivalent of AppleScript "Tell application X to open Y"? I'm not using cocoa, just plain old C, and ideally I would like to be able to do this from a commandl ine application that has no GUI interface.

This doesn't have to be fancy, it would be all right if I don't even wait for the success-or-failure response. Just needs to send the right Apple event.

3

There are 3 answers

3
r3mainer On

I think by far the simplest solution would be to issue a system call to the osascript command line utility.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  system("osascript -e 'tell application \"X\" to open POSIX file \"/path/to/Y\"'");
  return 0;
}
1
foo On

OP's kinda creating a rod for his own back here, as except for CoreFoundation most OS X-specific C APIs are now legacy/deprecated. Used to be you could call LaunchServices' LSOpenApplication(), but that got deprecated in 10.10.

If NSWorkspace isn't an option then use open via fork() and execv(), which allows arguments to be passed directly.

3
Joymaker On

Thanks for the helpful thoughts, they really got me pointed in the right direction. Here's the synthesis of these answers that seems to be working well for me, code that assumes that the path and the filename will be variable:

char wdname[1000], obuf[1000];
const char* filename = "WavySwirl.stl";

getwd(wdname);
sprintf(obuf, "open %s/%s", wdname, filename);
system(obuf);