From 5eef1265f8311fbe1e89bb2df69dcf5afa01a386 Mon Sep 17 00:00:00 2001 From: Hraban Luyat Date: Thu, 2 Nov 2023 20:33:28 -0400 Subject: [PATCH] feat: sync-trampolines allin1 --- README.org | 13 +++++++++++++ main.lisp | 48 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/README.org b/README.org index 54fddf5..9043ab9 100644 --- a/README.org +++ b/README.org @@ -2,6 +2,7 @@ - =mktrampoline= :: Create a “trampoline” launcher app - =sync-dock= :: Update persistent apps in the Dock +- =sync-trampolines= :: Create a directory with trampolines to all your apps Made with Nix in mind. @@ -31,6 +32,18 @@ It will find an old persistent item by the name of "Foo" and update it to the ne N.B.: This is currently limited only to Nix apps, but actually it could work for anything. I’ve just kept it conservative to be on the safe side. +** sync-trampolines + +Combines =mktrampoline= and =sync-dock= to create a fresh directory with a fresh trampoline for every source app. E.g.: + +#+begin_src shell +$ nix run github:hraban/mac-app-util -- sync-trampolines ~/special/apps/ ~/Applications/Special/ +#+end_src + +Will create a fresh directory (=~/Applications/Special=), deleting if it already existed. In that directory it will create a trampoline app for every single =*.app= file it finds in =~/special/apps/=. + +This helps register apps from outside of your =~/Applications= directory with Spotlight and the Launchpad. + * License mac-app-util - Manage Mac App launchers diff --git a/main.lisp b/main.lisp index 20f9a6c..cd2d6a9 100755 --- a/main.lisp +++ b/main.lisp @@ -119,14 +119,19 @@ (find ,to-cnts -name "*.icns" -delete) (rsync :include "*.icns" :exclude "*" :recursive ,from-cnts ,to-cnts))))) -(defun create-trampoline (app trampoline) - (let* ((aapp (merge-pathnames app (uiop:getcwd))) - (atrampoline (merge-pathnames trampoline (uiop:getcwd))) - (cmd (format NIL "do shell script \"open '~A'\"" aapp))) - (rm-rf atrampoline) - (sh `("/usr/bin/osacompile" #\o ,atrampoline #\e ,cmd)) - (sync-icons aapp atrampoline) - (copy-paths (infoplist aapp) (infoplist atrampoline) *copyable-app-props*))) +(defgeneric mktrampoline (from to)) + +(defmethod mktrampoline ((app string) (trampoline string)) + (mktrampoline (to-abs-dir app) (to-abs-dir trampoline))) + +(defmethod mktrampoline ((app pathname) (trampoline pathname)) + (uiop:ensure-pathname app :ensure-absolute t) + (uiop:ensure-pathname trampoline :ensure-absolute t) + (let ((cmd (format NIL "do shell script \"open '~A'\"" app))) + (rm-rf trampoline) + (sh `("/usr/bin/osacompile" #\o ,trampoline #\e ,cmd)) + (sync-icons app trampoline) + (copy-paths (infoplist app) (infoplist trampoline) *copyable-app-props*))) ;;; sync-dock @@ -171,6 +176,29 @@ Also resolves symlinks, if relevant. ;; only restart exactly once. (sh `(dockutil :add ,(realpath app) :replacing ,existing)))))) + +;;; sync-trampolines + +(defun to-abs-dir (d) + "Transform d into an absolute directory pathname." + (uiop:ensure-pathname d + :ensure-absolute t + :defaults (uiop:getcwd) + :ensure-directory t)) + +(defun directory-name (d) + ;; Weird lispism + (first (last (pathname-directory d)))) + +(defun sync-trampolines (&rest args) + (destructuring-bind (from to) (mapcar #'to-abs-dir args) + (rm-rf to) + (ensure-directories-exist to) + (let ((apps (directory (merge-pathnames #p"*.app" from)))) + (dolist (app apps) + (mktrampoline app (merge-pathnames (directory-name app) to))) + (sync-dock apps)))) + ;;; CLI @@ -197,9 +225,11 @@ directory, without having to check which one is pinned etc. (uiop:quit 0)) (trivia:match args ((list "mktrampoline" from to) - (create-trampoline from to)) + (mktrampoline from to)) ((list* "sync-dock" apps) (sync-dock apps)) + ((list "sync-trampolines" from to) + (sync-trampolines from to)) (_ (print-usage) (uiop:quit 1))))))