MacOS: how to run AppleScript script from a Qt app?
-
wrote on 3 Dec 2010, 16:38 last edited by
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
-
wrote on 3 Dec 2010, 17:06 last edited by
At least you must call the AppleScript interpreter or create an executable of your script and call it with QProcess.
-
wrote on 3 Dec 2010, 17:09 last edited by
[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.
-
wrote on 3 Dec 2010, 17:16 last edited by
There is no API in Qt to do that. You must include native API calls (Objective C, most probably) or call some command line tool (osascript) via QProcess.
-
wrote on 3 Dec 2010, 17:19 last edited by
Of course there's no API in Qt for that :)
-
wrote on 3 Dec 2010, 17:20 last edited by
Then I don't understand you problem. What are you trying to do?
-
wrote on 3 Dec 2010, 17:22 last edited by
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).
-
wrote on 3 Dec 2010, 17:43 last edited by
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();
}
@ -
wrote on 3 Dec 2010, 17:50 last edited by
I've added a wiki page (http://developer.qt.nokia.com/wiki/Call_an_AppleScript_from_Qt) in the how to section for this.
-
wrote on 3 Dec 2010, 18:02 last edited by
The wiki now contains a sample how to read the output of the script.
-
wrote on 3 Dec 2010, 18:27 last edited by
Volker, great! thank you
-
wrote on 3 Dec 2010, 18:28 last edited by
You're welcome. If you have any further questions, don't hesitate to ask.
-
wrote on 3 Dec 2010, 19:15 last edited by
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.
-
wrote on 3 Dec 2010, 19:33 last edited by
I don't know if that's possible. I doubt one can "translate" JavaScript to AppleScript in that way.
-
wrote on 4 Dec 2010, 13:05 last edited by
[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
-
wrote on 4 Dec 2010, 13:08 last edited by
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
-
wrote on 4 Dec 2010, 18:10 last edited by
[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.
-
wrote on 5 Dec 2010, 16:35 last edited by
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 -
wrote on 5 Dec 2010, 21:35 last edited by
I don't know if thats possible with AppleScript at all. I wish you good luck for your research!
-
wrote on 6 Dec 2010, 15:09 last edited by
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
1/36