A tiny C library to execute AppleScript
Copyright © 2014-2018 by Renaud Guillard (dev@nore.fr) Distributed under the terms of the MIT License, see LICENSE
libapplescript
is a macOS C library that wraps
the Objective-C AppleScript API
of the Cocoa framework
make -C build
The Makefile was generated by premake5
using the script located in scripts/build/premake5.lua
premake5 --file=scripts/build/premake5.lua [--location=] [--targetdir=] gmake
With
location
: The place where to put the build files (Makefile etc.)targetdir
: The base path of the targets output. Equivalent of the--prefix
on mostconfigure
scripts
A program using libapplescript
have to be linked against the Cocoa framework too
int res = applescript_execute_cstring("tell application \"iTunes\" to quit", NULL, NULL);
if (res == kAppleScriptResultInvalidScript)
{
fprintf(stderr, "Invalid AppleScript code\n");
}
else if (res != 0)
{
fprintf(stderr, "An error occured while executing the script (exit code: %d)\n", res);
}
Or simply
applescript_tell_application_cstring("iTunes", "quit", NULL, NULL);
AppleEvents generated by executing AppleScript code can be catched by passing a callback to the execution functions.
#include <stdio.h>
void my_callback (int event_index, size_t event_count, int event_type, const char *event_value, void *cookie);
{
if (event_index >= 0)
{
fprintf (stdout, "#%d %s\n", event_index + 1, event_value);
++(*((int*)cookie));
}
else
{
fprintf (stderr, "Execution failure (code %d, message %s)\n", event_type, event_value);
}
}
int main ()
{
int count = 0;
int result = applescript_tell_application_cstring("System Events", "get the name of every login item", my_callback, &count);
fprintf (stdout, "Total event count: %d\n", count);
return result;
}