Rework macos app launches

This commit is contained in:
crschnick 2024-04-29 22:49:30 +00:00
parent dc50b0b155
commit 6fc48a7d74
4 changed files with 15 additions and 61 deletions

View file

@ -11,7 +11,6 @@ import io.xpipe.core.process.ShellControl;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Optional;
public abstract class ExternalApplicationType implements PrefsChoiceValue {
@ -43,46 +42,15 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue {
this.applicationName = applicationName;
}
protected Optional<Path> getApplicationPath() {
try (ShellControl pc = LocalShell.getShell().start()) {
try (var c = pc.command(String.format(
"/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister "
+ "-dump | grep -o \"/.*%s.app\" | grep -v -E \"Caches|TimeMachine|Temporary|.Trash|/Volumes/%s\" | uniq",
applicationName, applicationName))
.start()) {
var path = c.readStdoutDiscardErr();
if (c.getExitCode() != 0 || path.isBlank()) {
return Optional.empty();
}
// Check if returned paths are actually valid
// Also sort them by length to prevent finding a deeply buried app
var valid = path.lines()
.filter(s -> {
try {
return Files.exists(Path.of(s));
} catch (Exception ex) {
return false;
}
})
.sorted(Comparator.comparingInt(value -> value.length()))
.toList();
// Require app in proper applications directory
var app = valid.stream()
.filter(s -> s.contains("Applications"))
.findFirst();
return app.map(Path::of);
}
} catch (Exception e) {
ErrorEvent.fromThrowable(e).omit().handle();
return Optional.empty();
}
}
@Override
public boolean isAvailable() {
return getApplicationPath().isPresent();
try (ShellControl pc = LocalShell.getShell().start()) {
return pc.command(String.format(
"mdfind -name '%s' -onlyin /Applications -onlyin ~/Applications -onlyin /System/Applications", applicationName)).executeAndCheck();
} catch (Exception e) {
ErrorEvent.fromThrowable(e).handle();
return false;
}
}
@Override

View file

@ -194,14 +194,9 @@ public interface ExternalEditorType extends PrefsChoiceValue {
@Override
public void launch(Path file) throws Exception {
var execFile = getApplicationPath();
if (execFile.isEmpty()) {
throw new IOException("Application " + applicationName + ".app not found");
}
ExternalApplicationHelper.startAsync(CommandBuilder.of()
.add("open", "-a")
.addFile(execFile.orElseThrow().toString())
.addQuoted(applicationName)
.addFile(file.toString()));
}
}

View file

@ -549,33 +549,23 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
@Override
public void launch(LaunchConfiguration configuration) throws Exception {
var app = this.getApplicationPath();
if (app.isEmpty()) {
throw new IllegalStateException("iTerm installation not found");
}
try (ShellControl pc = LocalShell.getShell()) {
var a = app.get().toString();
pc.osascriptCommand(String.format(
"""
if application "%s" is not running then
launch application "%s"
if application "iTerm" is not running then
launch application "iTerm"
delay 1
tell application "%s"
tell application "iTerm"
tell current tab of current window
close
end tell
end tell
end if
tell application "%s"
tell application "iTerm"
activate
create window with default profile command "%s"
end tell
""",
a,
a,
a,
a,
configuration.getScriptFile().toString().replaceAll("\"", "\\\\\"")))
.execute();
}

View file

@ -81,9 +81,10 @@ public interface WezTerminalType extends ExternalTerminalType {
@Override
public void launch(LaunchConfiguration configuration) throws Exception {
var path = LocalShell.getShell().command(String.format(
"mdfind -name '%s' -onlyin /Applications -onlyin ~/Applications -onlyin /System/Applications 2>/dev/null", applicationName)).readStdoutOrThrow();
var c = CommandBuilder.of()
.addFile(getApplicationPath()
.orElseThrow()
.addFile(Path.of(path)
.resolve("Contents")
.resolve("MacOS")
.resolve("wezterm-gui")