]> git.bundesbrandschatzamt.de Git - emacsconfig.git/commitdiff
adding ob-applescript
authorAndreas Gerler <baronNOSPAMbundesbrandschatzamt.de>
Fri, 7 Oct 2016 05:58:06 +0000 (07:58 +0200)
committerAndreas Gerler <baronNOSPAMbundesbrandschatzamt.de>
Fri, 7 Oct 2016 05:58:06 +0000 (07:58 +0200)
bba.org
lisp/ob-applescript.el [new file with mode: 0644]

diff --git a/bba.org b/bba.org
index 39a74a57831fe37c54c25f1f6c2195eb66b39a05..e477cd2da700d694ff6556cf10e0297ccdc63689 100644 (file)
--- a/bba.org
+++ b/bba.org
@@ -803,6 +803,19 @@ javascript:location.href='org-protocol://capture:/l/'%20+%20encodeURIComponent(l
 
 #+END_SRC
 
+** [[https://github.com/stig/ob-applescript.el][ob-applescript]]
+[2016-09-27 Tue 09:34]
+
+This module lets you run AppleScript from AppleScript source code
+blocks with Org Babel.
+
+This package is a direct result of attempting to answer a question on
+this [[http://emacs.stackexchange.com/q/26374/10625][emacs.stackexchange.com thread]].
+
+#+BEGIN_SRC emacs-lisp
+  (require 'ob-applescript)
+#+END_SRC
+
 ** [[https://github.com/stig/ox-jira.el][ox-jira]]
 [2016-05-22 Sun 20:53]
 
diff --git a/lisp/ob-applescript.el b/lisp/ob-applescript.el
new file mode 100644 (file)
index 0000000..4441424
--- /dev/null
@@ -0,0 +1,97 @@
+;;; ob-applescript.el --- org-babel functions for template evaluation
+
+;; Copyright (C) Stig Brautaset
+
+;; Author: Stig Brautaset
+;; Keywords: literate programming, reproducible research, mac
+;; Homepage: http://github.com/stig/ob-applescript.el
+;; Version: 0.01
+
+;;; License:
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3, or (at your option)
+;; any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;;; Requirements:
+
+;; Use this section to list the requirements of this language.  Most
+;; languages will require that at least the language be installed on
+;; the user's system, and the Emacs major mode relevant to the
+;; language be installed as well.
+
+ ;;; Code:
+(require 'ob)
+(require 'ob-core)
+(require 'ob-ref)
+(require 'ob-eval)
+
+;; Define tangle extension.
+(add-to-list 'org-babel-tangle-lang-exts '("applescript" . "scpt"))
+(add-to-list 'org-babel-tangle-lang-exts '("apples" . "scpt"))
+
+;; (defvar org-babel-default-header-args:applescript '(:var))
+
+
+(defun org-babel-variable-assignments:applescript (params)
+  "Return list of AppleScript statements assigning the block's variables."
+  (mapcar
+   (lambda (pair)
+     (format "set %s to %s\n"
+             (car pair)
+             (org-babel-applescript-var-to-applescript (cdr pair))))
+   (mapcar #'cdr (org-babel-get-header params :var))))
+
+(defun org-babel-applescript-var-to-applescript (var)
+  "Convert an elisp var into a string of AppleScript source code
+ specifying a var of the same value."
+  (format "%S" var))
+
+(defun org-babel-expand-body:applescript (body params)
+  (concat (apply #'concat
+                 (org-babel-variable-assignments:applescript params))
+          "\n"
+          body
+          "\n"))
+
+(defun org-babel-execute:applescript (body params)
+  "Execute a block of AppleScript code with org-babel.
+ This function is called by `org-babel-execute-src-block'"
+  (message "executing AppleScript source code block")
+  (let* ((processed-params (org-babel-process-params params))
+         (full-body (org-babel-expand-body:applescript body processed-params)))
+    (org-babel-applescript-table-or-string
+     (org-babel-eval "osascript" full-body)
+     params)))
+
+(defun org-babel-execute:apples (body params)
+  "Execute a block of AppleScript with org-babel."
+  (org-babel-execute:applescript body params))
+
+(defun org-babel-applescript-table-or-string (results params)
+  "If the results look like a table, then convert them into an
+ Emacs-lisp table, otherwise return the results as a string."
+  (org-babel-reassemble-table
+   (org-babel-result-cond (cdr (assoc :result-params params))
+     (org-babel-read results)
+     (let ((tmp-file (org-babel-temp-file "c-")))
+       (with-temp-file tmp-file (insert results))
+       (org-babel-import-elisp-from-file tmp-file)))
+   (org-babel-pick-name
+    (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
+   (org-babel-pick-name
+    (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
+
+(provide 'ob-applescript)
+;;; ob-applescript.el ends here