December 3, 2010

infoctopus infoctopus
Lab Rat
128 posts

MacOS: how to run AppleScript script from a Qt app?

Page  
1

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 [habrahabr.ru], but Carbon is used. And I’d like to use Cocoa

 Signature 

Qt rulez

34 replies

December 3, 2010

Volker Volker
Ant Farmer
5428 posts

At least you must call the AppleScript interpreter or create an executable of your script and call it with QProcess.

December 3, 2010

infoctopus infoctopus
Lab Rat
128 posts
Volker wrote:
At least you must call the AppleScript interpreter

I clearly understand that :) The question is how exactly to do that? code samples would be great

Volker wrote:
or create an executable of your script and call it with QProcess

That’s not desirable. Some API would be better.

 Signature 

Qt rulez

December 3, 2010

Volker Volker
Ant Farmer
5428 posts

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.

December 3, 2010

infoctopus infoctopus
Lab Rat
128 posts

Of course there’s no API in Qt for that :)

 Signature 

Qt rulez

December 3, 2010

Volker Volker
Ant Farmer
5428 posts

Then I don’t understand you problem. What are you trying to do?

December 3, 2010

infoctopus infoctopus
Lab Rat
128 posts

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).

 Signature 

Qt rulez

December 3, 2010

Volker Volker
Ant Farmer
5428 posts

This may give you a start:

  1. #include <QApplication>
  2. #include <QProcess>
  3.  
  4. int main(int argc, char **argv)
  5. {
  6.     QApplication a(argc, argv);
  7.  
  8.     QString aScript =
  9.             "tell application \"System Events\"\n"
  10.             "    activate\n"
  11.             "    display dialog \"Hello world\"\n"
  12.             "end tell\n";
  13.  
  14.     QString osascript = "/usr/bin/osascript";
  15.     QStringList processArguments;
  16.     processArguments << "-l" << "AppleScript";
  17.  
  18.     QProcess p;
  19.     p.start(osascript, processArguments);
  20.     p.write(aScript.toUtf8());
  21.     p.closeWriteChannel();
  22.     p.waitForFinished();
  23. }

December 3, 2010

Volker Volker
Ant Farmer
5428 posts

I’ve added a wiki page (http://developer.qt.nokia.com/wiki/Call_an_AppleScript_from_Qt) in the how to section for this.

December 3, 2010

Volker Volker
Ant Farmer
5428 posts

The wiki now contains a sample how to read the output of the script.

December 3, 2010

infoctopus infoctopus
Lab Rat
128 posts

Volker, great! thank you

 Signature 

Qt rulez

December 3, 2010

Volker Volker
Ant Farmer
5428 posts

You’re welcome. If you have any further questions, don’t hesitate to ask.

December 3, 2010

infoctopus infoctopus
Lab Rat
128 posts

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.

 Signature 

Qt rulez

December 3, 2010

Volker Volker
Ant Farmer
5428 posts

I don’t know if that’s possible. I doubt one can “translate” JavaScript to AppleScript in that way.

December 4, 2010

infoctopus infoctopus
Lab Rat
128 posts
Volker wrote:
I don’t know if that’s possible. I doubt one can “translate” JavaScript to AppleScript in that way.

It depends. For instance the following code

  1. tell application "System Events"
  2.     activate
  3.     display dialog "Hello world"
  4. end tell;

obviously can be expressed in Java/ECMAScript

 Signature 

Qt rulez

December 4, 2010

infoctopus infoctopus
Lab Rat
128 posts

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

 Signature 

Qt rulez

Page  
1

  ‹‹ MOC problem      Non-virtual destructor in QNetworkAccessManager ››

You must log in to post a reply. Not a member yet? Register here!