0

Hello I know that I can use something like Applescript to control different aspects of operating systems like keyboard inputs:

osascript -e 'tell application "System Events" to key code 45'

or mouse clicks:

osascript -e 'tell app "System Events" to click at {100,200}'

and many other features like volume up/down, open/close app, go to url in web browser.

Now I consider how could I control this without Applescript on OS X (macOS) I think about some low level API, preferably in C (eventually in Objective-C) that could do similar things. I am interested mainly in mouse/keyboard/pad software control (like If I will be writing virtual keyboard), opening apps, invoking shortcuts in apps. I think I will use Applescript at the end and execute it's scripts via C. But I am also interested in more low level programming and libraries in C?

What is the best way to execute such apple scripts from other languages like C? I think about something like

system(osascript -e 'tell application "System Events" to key code 45')

But maybe there are better functions/libraries like osascript("here cmd");

Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
  • Forget C for Mac-specific programming; anything interesting requires Cocoa, so use ObjC. OS X does have an Apple event bridge for ObjC, but it's a pile of crap. The quickest, easiest, and most reliable way is to call AppleScript handlers directly from ObjC via the AppleScript-ObjC bridge. For direct GUI manipulation use OS X's Accessibility APIs, but avoid GUI automation like the plague as it's ridiculously fragile and brittle with abysmal portability. Depending what you're doing, you might find an existing solution like Hammerspoon a better choice. – foo Aug 19 '16 at 01:54
  • But I can execute applescript from C like system() etc. – Michał Ziobro Aug 19 '16 at 07:57
  • I think Objective-C also is callable from C? this Quartz Event Services API https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/#//apple_ref/c/func/CGEventCreateKeyboardEvent – Michał Ziobro Aug 19 '16 at 07:58
  • The correct way to execute AS from C would be to use the Open Scripting C API, but that's all legacy/deprecated APIs and a gigantic PITA to use, so avoid it. And for practical purposes, no, you can't call ObjC APIs (Foundation, Cocoa, etc) from C. ObjC is a superset of C that adds Smalltalk-like OO on top of the procedural C language. Honestly, why would you want to use C, other than to make a rod for your own back? You can do everything in ObjC that you can in C - call BSD APIs, call Carbon C APIs - _and_ call ObjC APIs too. – foo Aug 19 '16 at 10:06
  • I have libraries in C and I would like to stick to C as much as I can. Cocoa or Apple Script will be only used for some system calls like mouse, keyboard events. I have tried to compile C program in CMake, gcc with -framework ApplicationServices and it works correctyl and enable to use of this Quartz Event Services functions in C (I think that this low level Objective-C APIs are procedural and written in C mainly so whats the problem to stick to C rather then use Objective-C when it isn't necessery – Michał Ziobro Aug 19 '16 at 11:28
  • 1. You can use all your C libraries in ObjC. 2. Quartz Event Services is a C API, not an ObjC one. 3. You need to crack a book on ObjC and teach yourself how to use it so you can make informed decisions for yourself. – foo Aug 19 '16 at 17:42

1 Answers1

0

How about, before going from AppleScript upto C, I recommend a half-way step by using the "AppleScript Objective C" Programing Language, Its simple because your probably good at applescript and you can also use a much larger range of code from Objective C, ITS AppleScript In Objective C!! :D You can do it in Xcode! hope this might help a little :)

YeaTheMans
  • 1,005
  • 8
  • 19