Patch from Benjamin Piwowar - see ticket #8185:

In order to interact with native osx applications, AppleScript support is a plus.
Here is a patch that makes LyX respond to a simple command (run) and that allows to communicate with LyX as with the LyX client.
Example of use:
	tell application "LyX" to run "server-get-filename" with argument ""'
returns
	message:/Users/bpiwowar/newfile1.lyx, code:0
with a message and the error code
This commit is contained in:
Stephan Witt 2012-08-19 22:27:20 +02:00
parent 8bf0652b9c
commit 4674c3e1c5
10 changed files with 191 additions and 1 deletions

View File

@ -37,5 +37,9 @@
<string>@VERSION@</string> <string>@VERSION@</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>OLYX</string> <string>OLYX</string>
<key>NSAppleScriptEnabled</key>
<string>YES</string>
<key>OSAScriptingDefinition</key>
<string>LyX.sdef</string>
</dict> </dict>
</plist> </plist>

View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<!--
This file is part of LyX, the document processor.
Licence details can be found in the file COPYING.
author: Benjamin Piwowarski
Full author contact details are available in file CREDITS.
-->
<!-- declare the namespace for using XInclude so we can include the standard suite -->
<dictionary xmlns:xi="http://www.w3.org/2003/XInclude">
<!-- use XInclude to include the standard suite -->
<!-- <xi:include href="file:///System/Library/ScriptingDefinitions/CocoaStandard.sdef" xpointer="xpointer(/dictionary/suite)"/> -->
<!-- our special scripting suite for this example -->
<suite name="Lyx" code="LYX " description="LyX scripting facilities.">
<record-type name="LyX return value" code="LyxR">
<property name="code" code="code" type="integer"
description="Error code (0 in case of success).">
<cocoa key="code"/>
</property>
<property name="message" code="mess" type="text"
description="The returned message.">
<cocoa key="message"/>
</property>
</record-type>
<command name="run" code="SLyxComm" description="run a simple command with one parameter">
<cocoa class="LyxCommand"/>
<direct-parameter description="The command to be executed.">
<type type="text" list="no"/>
</direct-parameter>
<parameter name="with argument" code="args" type="text">
<cocoa key="arg"/>
</parameter>
<result type="LyX return value" description="Contains a code (0 for success) and the message returned by LyX"/>
</command>
</suite>
</dictionary>

View File

@ -10,7 +10,7 @@ nodist_bundle_DATA = Info.plist
dist_bin_SCRIPTS = lyxeditor dist_bin_SCRIPTS = lyxeditor
dist_pkgdata_DATA = COPYING LyXapp.icns LyX.icns dist_pkgdata_DATA = COPYING LyXapp.icns LyX.icns LyX.sdef
nodist_pkgdata_DATA = lyxrc.dist nodist_pkgdata_DATA = lyxrc.dist

View File

@ -574,6 +574,7 @@ HEADERS += \
../../src/support/gzstream.h \ ../../src/support/gzstream.h \
../../src/support/lassert.h \ ../../src/support/lassert.h \
../../src/support/limited_stack.h \ ../../src/support/limited_stack.h \
../../src/support/linkback/AppleScript.h \
../../src/support/linkback/LinkBack.h \ ../../src/support/linkback/LinkBack.h \
../../src/support/linkback/LinkBackProxy.h \ ../../src/support/linkback/LinkBackProxy.h \
../../src/support/linkback/LinkBackServer.h \ ../../src/support/linkback/LinkBackServer.h \

View File

@ -72,6 +72,7 @@
#include "support/Systemcall.h" #include "support/Systemcall.h"
#ifdef Q_WS_MACX #ifdef Q_WS_MACX
#include "support/AppleScript.h"
#include "support/linkback/LinkBackProxy.h" #include "support/linkback/LinkBackProxy.h"
#endif #endif
@ -843,6 +844,8 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
/// A translator suitable for the entries in the LyX menu. /// A translator suitable for the entries in the LyX menu.
/// Only needed with Qt/Mac. /// Only needed with Qt/Mac.
installTranslator(new MenuTranslator(this)); installTranslator(new MenuTranslator(this));
///
setupApplescript();
#endif #endif
#ifdef Q_WS_X11 #ifdef Q_WS_X11

32
src/support/AppleScript.h Normal file
View File

@ -0,0 +1,32 @@
// -*- C++ -*-
/**
* \file AppleScript.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Benjamin Piwowarski
*
* Full author contact details are available in file CREDITS.
*/
#ifndef LYX_SUPPORT_APPLESCRIPT_H
#define LYX_SUPPORT_APPLESCRIPT_H
#ifdef __cplusplus
extern "C" {
#endif
/// What is returned by applescript_execute_command
typedef struct {
int code;
char *message;
} LyXFunctionResult;
LyXFunctionResult applescript_execute_command(const char *cmd, const char *args);
/// Sets up apple script support
void setupApplescript();
#ifdef __cplusplus
}
#endif
#endif

46
src/support/AppleScript.m Normal file
View File

@ -0,0 +1,46 @@
/**
* \file AppleScript.m
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Benjamin Piwowarski
*
* Full author contact details are available in file CREDITS.
*/
#import <Cocoa/Cocoa.h>
#include "AppleScript.h"
@interface LyxCommand : NSScriptCommand {
}
- (id)performDefaultImplementation;
@end
@implementation LyxCommand
- (id)performDefaultImplementation {
// Get the command and argument
NSDictionary * theArguments = [self evaluatedArguments];
NSString * directParameter = [self directParameter];
NSString *arg = [theArguments objectForKey: @"arg"];
// Execute the command
LyXFunctionResult result = applescript_execute_command([directParameter UTF8String], [arg UTF8String]);
// Construct the result record
NSString *message = [NSString stringWithCString:result.message encoding:NSUTF8StringEncoding];
free(result.message);
NSDictionary *objcResult = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:result.code], @"code", message, @"message", nil];
return objcResult;
}
@end
/// Needed by AppleScript in order to discover LyxCommand
void setupApplescript() {
}

View File

@ -0,0 +1,46 @@
// -*- C++ -*-
/**
* \file AppleScriptProxy.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Benjamin Piwowarski
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "AppleScript.h"
#include "DispatchResult.h"
#include "FuncRequest.h"
#include "LyX.h"
#include "LyXAction.h"
#include "frontends/Application.h"
#include "support/docstring.h"
#include "support/debug.h"
using namespace std;
using namespace lyx;
extern "C" LyXFunctionResult applescript_execute_command(const char *cmd, const char *arg) {
LYXERR(Debug::ACTION, "Running command [" << cmd << "] with arguments [" << arg << "]");
FuncRequest fr(lyxaction.lookupFunc(cmd), arg);
fr.setOrigin(FuncRequest::LYXSERVER);
DispatchResult dr;
theApp()->dispatch(fr, dr);
string const rval = to_utf8(dr.message());
char *cstr =(char*) malloc((rval.size()+1)*sizeof(rval[0]));
strcpy (cstr, rval.c_str());
// Returns the result
LyXFunctionResult result;
result.code = dr.error() ? -1 : 0;
result.message = cstr;
return result;
}

View File

@ -11,6 +11,7 @@ file(GLOB moc_files ${TOP_SRC_DIR}/src/support/${LYX_MOC_FILES})
list(REMOVE_ITEM support_sources ${moc_files} .) list(REMOVE_ITEM support_sources ${moc_files} .)
if(APPLE) if(APPLE)
list(APPEND dont_merge ${TOP_SRC_DIR}/src/support/AppleSpeller.m) list(APPEND dont_merge ${TOP_SRC_DIR}/src/support/AppleSpeller.m)
list(APPEND dont_merge ${TOP_SRC_DIR}/src/support/AppleScript.m)
endif() endif()
file(GLOB support_headers ${TOP_SRC_DIR}/src/support/${LYX_HPP_FILES}) file(GLOB support_headers ${TOP_SRC_DIR}/src/support/${LYX_HPP_FILES})

View File

@ -120,6 +120,9 @@ if INSTALL_MACOSX
liblyxsupport_a_SOURCES += \ liblyxsupport_a_SOURCES += \
AppleSpeller.h \ AppleSpeller.h \
AppleSpeller.m \ AppleSpeller.m \
AppleScript.h \
AppleScript.m \
AppleScriptProxy.cpp \
linkback/LinkBack.h \ linkback/LinkBack.h \
linkback/LinkBack.m \ linkback/LinkBack.m \
linkback/LinkBackProxy.h \ linkback/LinkBackProxy.h \