From 9a0cb694f72aa258044a35903bbeac7ce2bbb9e3 Mon Sep 17 00:00:00 2001 From: Jarek Sacha Date: Sat, 10 Jun 2023 09:06:17 -0400 Subject: [PATCH] Better inference of ImageJ directory on macOS - consider launcher being in subdirectory "Contents/MacOS" #7 --- .../ij_plugins/imagej_launcher/IJDir.scala | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/scala/ij_plugins/imagej_launcher/IJDir.scala b/src/main/scala/ij_plugins/imagej_launcher/IJDir.scala index 0239b36..e871ea1 100644 --- a/src/main/scala/ij_plugins/imagej_launcher/IJDir.scala +++ b/src/main/scala/ij_plugins/imagej_launcher/IJDir.scala @@ -30,18 +30,28 @@ object IJDir: logger.debug(" Considering application directory") val appPath = Native.applicationPath() logger.debug(s" Application directory: '$appPath'") - if isIJDir(appPath) then - Right(appPath) - else - logger.debug(" Application directory is not an ImageJ directory") - logger.debug(" Considering current working directory") - val cwd = os.pwd - logger.debug(s" Current working directory: '$cwd'") - if isIJDir(cwd) then - Right(cwd) - else - logger.debug(" Current working directory is not an ImageJ directory.") - Left("Cannot locate ImageJ directory.") + inferIJDir(appPath) match + case Some(p) => + Right(p) + case None => + logger.debug(" Application directory is not an ImageJ directory") + logger.debug(" Considering current working directory") + val cwd = os.pwd + logger.debug(s" Current working directory: '$cwd'") + inferIJDir(cwd) match + case Some(p) => + Right(p) + case None => + logger.debug(" Current working directory is not an ImageJ directory.") + Left("Cannot locate ImageJ directory.") + + private def inferIJDir(path: Path): Option[Path] = + if isIJDir(path) then + Option(path) + else if path.endsWith(os.rel / "Contents" / "MacOS") then + Option(path / os.up / os.up) + else + None private def isIJDir(path: Path): Boolean = os.exists(path) &&