;;; jao-mac.el --- Running applescript.      -*- lexical-binding: t; -*-

;; Copyright (C) 2025, 2026  Jose Antonio Ortega Ruiz

;; Author: Jose Antonio Ortega Ruiz <mail@jao.io>
;; Keywords: lisp

;; 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 of the License, 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 this program.  If not, see <https://www.gnu.org/licenses/>.

(require 'jao-shell)

;;; applescript
(defun jao-mac-applescript-prepare (&rest lines)
  (let ((script (mapconcat 'identity lines "\r"))
        (start))
    (while (string-match "\n" script)
      (setq script (replace-match "\r" t t script)))
    (while (string-match "'" script start)
      (setq start (+ 2 (match-beginning 0))
            script (replace-match "\\'" t t script)))
    script))

(defun jao-mac-run-applescript (script)
  (string-trim (shell-command-to-string (format "osascript -e '%s'" script))))

(defun jao-mac-run-applescript* (&rest lines)
  (jao-mac-run-applescript (apply #'jao-mac-applescript-prepare lines)))

(defun jao-mac-tell-app (app &rest script-lines)
  (let* ((app-id (string-split app))
         (id (if (> (length app-id) 1) (car app-id) ""))
         (app (if (> (length app-id) 1) (cadr app-id) (car app-id)))
         (pre (list (format "tell application %s %S" id app)))
         (post '("end tell\n")))
    (apply #'jao-mac-run-applescript* (append pre script-lines post))))

;;; open

(defun jao-mac-open (thing &rest args)
  "Invoke open after formatting thing with args, using `format'"
  (jao-shell-exec (format "open %s" (apply #'format thing args))))

(defun jao-mac-open-in-skim (&optional file page height)
  (interactive)
  (let* ((file (if file (expand-file-name file) (buffer-file-name)))
         (page (or page (and (derived-mode-p 'doc-view-mode)
                             (doc-view-current-page)))))
    (jao-mac-open "skim://%s%s" file (if page (format "#page=%s" page) ""))))

;; https://alvinalexander.com/macos/applescript-how-to-open-pdf-file-in-preview-go-to-page/
;; This will work as long as Preview is the default app for the file at hand.
(defun jao-mac-open-in-preview (&optional file page height)
  (interactive)
  (let ((file (if file (expand-file-name file) (buffer-file-name)))
        (page (or page (and (derived-mode-p 'doc-view-doc)
                            (doc-view-current-page)))))
    (jao-mac-run-applescript*
     (format "tell application id \"com.apple.Preview\" to open (POSIX file %S)\r\r"
             (file-truename file))
     "delay 1"
     "tell application \"System Events\""
     "keystroke \"g\" using {option down, command down}"
     (format "keystroke %s" (or page 1))
     "delay 0.1\rkeystroke return\rend tell")))

;;; notifications

(defun jao-mac-notify (title subtitle msg)
  (jao-mac-run-applescript*
   (format "display notification %S with title %S subtitle %S"
           msg title subtitle)))

(defun jao-mac-app-badge (app)
  "A very hacky, yet efficient, way of getting and apps badge."
  (let* ((c (format "lsappinfo info -app %S -only StatusLabel" app))
         (s (shell-command-to-string c)))
    (when (string-match ".*=\"\\([0-9]+\\)\" ." s)
      (string-to-number (match-string 1 s)))) )

;;; Skim
(defvar jao-skim--current-file-script
  (jao-mac-applescript-prepare
   "tell application \"Skim\""
   "  try"
   "    set theD to front document"
   "    set theP to (path of theD)"
   "    set thePg to (get index of current page of theD)"
   "    return (theP & \"::\" & thePg)"
   "  on error"
   "    return \"\""
   "  end try"
   "end tell"))

(defun jao-skim-current-doc ()
  "Returns a list of path and page number for the current Skim doc."
  (when-let* ((p (jao-mac-run-applescript jao-skim--current-file-script))
              (p (and (not (string-blank-p p)) p)))
    (let ((ps (split-string p "::")))
      (list (car ps) (string-to-number (cadr ps))))))

(defun jao-skim-open-current-doc ()
  (interactive)
  (when-let* ((ps (jao-skim-current-doc)))
    (apply 'jao-open-doc ps)))

(defvar jao-skim--current-page-text
  (jao-mac-applescript-prepare
   "tell application \"Skim\""
   "return the text of the current page of the front document"
   "end tell"))

(defun jao-skim-page-text ()
  (jao-mac-run-applescript jao-skim--current-page-text))

(defun jao-skim-view-page-text ()
  (interactive)
  (when-let* ((ps (jao-skim-current-doc))
              (p (car ps))
              (n (cadr ps))
              (txt (jao-skim-page-text))
              (bn (format "*%s - %s - txt*" p n)))
    (with-current-buffer (get-buffer-create bn)
      (delete-region (point-min) (point-max))
      (insert txt)
      (pop-to-buffer (current-buffer)))))

;;; NetNewsWire

(defvar jao-nnw--current-article-script
  (jao-mac-applescript-prepare
   "tell application \"NetNewsWire\""
   "try"
   "return (the url of the current article)"
   "on error"
   "return \"\""
   "end try"
   "end tell"))

(defun jao-nnw-current-article ()
  "The URL of the current article in NetNewsWire"
  (jao-mac-run-applescript jao-nnw--current-article-script))

(defun jao-nnw-browse-current-article ()
  "Browse the URL of the current NNW article."
  (interactive)
  (if-let* ((url (jao-nnw-current-article)))
      (unless (string-empty-p url)
        (browse-url url))
    (message "No article selected in NetNewsWire")))

(defvar jao-nnw--label-cmd
  "lsappinfo info -app NetNewsWire -only StatusLabel")

(defun jao-nnw-unread-count ()
  (jao-mac-app-badge "NetNewsWire"))

(defun jao-mac-open-nnw ()
  (interactive)
  (jao-mac-open "-a NetNewsWire"))

;;; Safari
(defun jao-safari-current-url ()
  (jao-mac-tell-app "Safari" "return URL of current tab of window 1"))

(defun jao-safari-browse-current ()
  "Browse the URL of the current Safari topmost document."
  (interactive)
  (browse-url (jao-safari-current-url)))

;;; Firefox
(defvar jao-ffox--current-url-script
  (jao-mac-applescript-prepare
   "tell application \"Firefox\""
   "  activate"
   "  delay 0.15"
   "  tell application \"System Events\""
   "    keystroke \"l\" using command down"
   "    delay 0.20"
   "    keystroke \"c\" using command down"
   "  end tell"
   "  delay 0.5"
   "end tell"
   "return the clipboard"))

(defun jao-firefox-current-url ()
  (let ((res (jao-mac-run-applescript jao-ffox--current-url-script)))
    (jao-mac-run-applescript "tell application \"Emacs\" to activate")
    res))

(defun jao-firefox-open ()
  (interactive)
  (jao-mac-open "-a Firefox"))

;;; DevonThink

(defun jao-devon-tell (&rest script-lines)
  (apply #'jao-mac-tell-app "id DNtp" script-lines ))

(defun jao-devon-find-url (file)
  (jao-devon-tell
   "repeat with db in databases"
   (format "set res to lookup records with path %S in db"
           (file-truename file))
   "if res /= {} then return the reference URL of (item 1 of res)"
   "end repeat"
   "return \"\""))

(defun jao-devon-show-search (s)
  (jao-devon-tell (format "show search %S" s) "activate"))

(defun jao-devon-open (file &optional page height)
  (let ((url (jao-devon-find-url file)))
    (if (string-empty-p (or url ""))
        (let ((jao-pdf-open-in-emacs t))
          (jao-find-or-open file page height))
      (let* ((p (if page (format "?page=%s" (- page 1)) ""))
             (u (format "%s%s" url p)))
        (jao-mac-open "%s%s" url p)))))

(provide 'jao-mac)
;;; jao-mac.el ends here
