]> git.lyx.org Git - features.git/commitdiff
Patch from Benjamin Piwowar - see ticket #8185:
authorStephan Witt <switt@lyx.org>
Sun, 19 Aug 2012 20:27:20 +0000 (22:27 +0200)
committerStephan Witt <switt@lyx.org>
Sun, 19 Aug 2012 20:27:20 +0000 (22:27 +0200)
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

development/MacOSX/Info.plist.in
development/MacOSX/LyX.sdef [new file with mode: 0644]
development/MacOSX/Makefile.am
development/qmake/lyx.pro
src/frontends/qt4/GuiApplication.cpp
src/support/AppleScript.h [new file with mode: 0644]
src/support/AppleScript.m [new file with mode: 0644]
src/support/AppleScriptProxy.cpp [new file with mode: 0644]
src/support/CMakeLists.txt
src/support/Makefile.am

index 3145c1a8caa9661529734ee45d3c5367a7bed467..32accf4c5df36d150e793dae8a8c408833f26bf6 100644 (file)
@@ -37,5 +37,9 @@
        <string>@VERSION@</string>
        <key>CFBundleSignature</key>
        <string>OLYX</string>
+       <key>NSAppleScriptEnabled</key>
+       <string>YES</string>
+       <key>OSAScriptingDefinition</key>
+    <string>LyX.sdef</string>
 </dict>
 </plist>
diff --git a/development/MacOSX/LyX.sdef b/development/MacOSX/LyX.sdef
new file mode 100644 (file)
index 0000000..41f7408
--- /dev/null
@@ -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>
\ No newline at end of file
index 4714c7f5c3ac7959d8d7beccfd1d14f4a0a5b3b8..89b1a715bb65008f5e2be8a885823e1e5a41e891 100644 (file)
@@ -10,7 +10,7 @@ nodist_bundle_DATA = Info.plist
 
 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
 
 
index cef45696530caa5547042a923637b776db3708f4..de5624bcf7541c61b8efb10dcc6a482a9f9a02d2 100644 (file)
@@ -574,6 +574,7 @@ HEADERS += \
 ../../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 \
index 701b488eabde7faafe2bed83be653ac42a73139f..0790b46440104877ea9e084d97d6997fdad200b4 100644 (file)
@@ -72,6 +72,7 @@
 #include "support/Systemcall.h"
 
 #ifdef Q_WS_MACX
+#include "support/AppleScript.h"
 #include "support/linkback/LinkBackProxy.h"
 #endif
 
@@ -843,6 +844,8 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
        /// 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
diff --git a/src/support/AppleScript.h b/src/support/AppleScript.h
new file mode 100644 (file)
index 0000000..6ac23b6
--- /dev/null
@@ -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
\ No newline at end of file
diff --git a/src/support/AppleScript.m b/src/support/AppleScript.m
new file mode 100644 (file)
index 0000000..bf06b02
--- /dev/null
@@ -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() {
+}
+
diff --git a/src/support/AppleScriptProxy.cpp b/src/support/AppleScriptProxy.cpp
new file mode 100644 (file)
index 0000000..23272e2
--- /dev/null
@@ -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;
+}
+
index b42f08cf582c77a5f1b44c6cdf6448f0781e64f1..521a42a255d558a41e0c5f446c9ad06fef728090 100644 (file)
@@ -11,6 +11,7 @@ file(GLOB moc_files ${TOP_SRC_DIR}/src/support/${LYX_MOC_FILES})
 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})
index f0819cf146db3035f5cd64a73b7bc81a0d76d354..f7fad7c4cdc8b02c4070e16232416ce43a9d7d31 100644 (file)
@@ -120,6 +120,9 @@ if INSTALL_MACOSX
 liblyxsupport_a_SOURCES += \
        AppleSpeller.h \
        AppleSpeller.m \
+       AppleScript.h \
+       AppleScript.m \
+       AppleScriptProxy.cpp \
        linkback/LinkBack.h \
        linkback/LinkBack.m \
        linkback/LinkBackProxy.h \