MacOS: how to run AppleScript script from a Qt app?
-
MacOS X. I want to automate an AppleScript-scriptable application, i.e. to execute applescript without using any native apps or command-line tools.
There're some pieces of relevant code "here":http://habrahabr.ru/blogs/qt_software/104633/, but Carbon is used. And I'd like to use Cocoa
-
[quote author="Volker" date="1291395979"]At least you must call the AppleScript interpreter[/quote]
I clearly understand that :) The question is how exactly to do that? code samples would be great
[quote author="Volker" date="1291395979"]or create an executable of your script and call it with QProcess[/quote]
That's not desirable. Some API would be better.
-
Of course there's no API in Qt for that :)
-
I'm asking someone to point out to proper MacOS API or, ideally, some 3rd party Qt lib for that (which, most likely, doesn't exist, but nevertheless).
-
This may give you a start:
@
#include <QApplication>
#include <QProcess>int main(int argc, char **argv)
{
QApplication a(argc, argv);QString aScript = "tell application \"System Events\"\n" " activate\n" " display dialog \"Hello world\"\n" "end tell\n"; QString osascript = "/usr/bin/osascript"; QStringList processArguments; processArguments << "-l" << "AppleScript"; QProcess p; p.start(osascript, processArguments); p.write(aScript.toUtf8()); p.closeWriteChannel(); p.waitForFinished();
}
@ -
I've added a wiki page (http://developer.qt.nokia.com/wiki/Call_an_AppleScript_from_Qt) in the how to section for this.
-
Volker, great! thank you
-
Well, I'm thinking about binding Qt's JavaScript to AppleScript, mixing Qt objects and AppleScript-scriptable apps. About executing AppleScript line-by-line. About accessing AppleScript's scripting runtime environment.
-
[quote author="Volker" date="1291404790"]I don't know if that's possible. I doubt one can "translate" JavaScript to AppleScript in that way.[/quote]
It depends. For instance the following code
@tell application "System Events"
activate
display dialog "Hello world"
end tell;@obviously can be expressed in Java/ECMAScript
-
I mean if we know the object model and grammar of a scripting language, we can tell if some script valid. And subset of features that is common for both scripting languages can be supported
-
[quote author="infoctopus" date="1291468109"]I mean if we know the object model and grammar of a scripting language, we can tell if some script valid. And subset of features that is common for both scripting languages can be supported[/quote]
From what I understood, you want to create a transformer app that kind of "translates" JavaScript to/from AppleScript. That seems to be a pretty huge task.
-
Well, I have own simplified scripting language (and simple IDE for it) that is mapped to JavaScript.
I'm thinking about switching to JavaScript completely, i.e. IDE has to be seriously upgraded.
And also to engane AppleScript as much as possible. For this, own scripting language can be left untouched, but mapping to AppleScript can be added. The problem is that I don't know how to control (exec line by line, debug, etc.) AppleScript execution, if it's possible at all. Most likely I have to dig apple docs -
Volker, thank you :) I think that the task is solvable, but not sure about the amount of time/money needed for the solution, even roughly