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
<string>@VERSION@</string>
<key>CFBundleSignature</key>
<string>OLYX</string>
+ <key>NSAppleScriptEnabled</key>
+ <string>YES</string>
+ <key>OSAScriptingDefinition</key>
+ <string>LyX.sdef</string>
</dict>
</plist>
--- /dev/null
+<?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>
\ No newline at end of file
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
../../src/support/gzstream.h \
../../src/support/lassert.h \
../../src/support/limited_stack.h \
+../../src/support/linkback/AppleScript.h \
../../src/support/linkback/LinkBack.h \
../../src/support/linkback/LinkBackProxy.h \
../../src/support/linkback/LinkBackServer.h \
#include "support/Systemcall.h"
#ifdef Q_WS_MACX
+#include "support/AppleScript.h"
#include "support/linkback/LinkBackProxy.h"
#endif
/// A translator suitable for the entries in the LyX menu.
/// Only needed with Qt/Mac.
installTranslator(new MenuTranslator(this));
+ ///
+ setupApplescript();
#endif
#ifdef Q_WS_X11
--- /dev/null
+// -*- 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
\ No newline at end of file
--- /dev/null
+/**
+ * \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() {
+}
+
--- /dev/null
+// -*- 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;
+}
+
list(REMOVE_ITEM support_sources ${moc_files} .)
if(APPLE)
list(APPEND dont_merge ${TOP_SRC_DIR}/src/support/AppleSpeller.m)
+ list(APPEND dont_merge ${TOP_SRC_DIR}/src/support/AppleScript.m)
endif()
file(GLOB support_headers ${TOP_SRC_DIR}/src/support/${LYX_HPP_FILES})
liblyxsupport_a_SOURCES += \
AppleSpeller.h \
AppleSpeller.m \
+ AppleScript.h \
+ AppleScript.m \
+ AppleScriptProxy.cpp \
linkback/LinkBack.h \
linkback/LinkBack.m \
linkback/LinkBackProxy.h \