mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-09-30 00:56:56 +13:00
Rework macos app launches
This commit is contained in:
parent
dc50b0b155
commit
6fc48a7d74
4 changed files with 15 additions and 61 deletions
|
@ -11,7 +11,6 @@ import io.xpipe.core.process.ShellControl;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public abstract class ExternalApplicationType implements PrefsChoiceValue {
|
public abstract class ExternalApplicationType implements PrefsChoiceValue {
|
||||||
|
@ -43,46 +42,15 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue {
|
||||||
this.applicationName = applicationName;
|
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
|
@Override
|
||||||
public boolean isAvailable() {
|
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
|
@Override
|
||||||
|
|
|
@ -194,14 +194,9 @@ public interface ExternalEditorType extends PrefsChoiceValue {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void launch(Path file) throws Exception {
|
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()
|
ExternalApplicationHelper.startAsync(CommandBuilder.of()
|
||||||
.add("open", "-a")
|
.add("open", "-a")
|
||||||
.addFile(execFile.orElseThrow().toString())
|
.addQuoted(applicationName)
|
||||||
.addFile(file.toString()));
|
.addFile(file.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -549,33 +549,23 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void launch(LaunchConfiguration configuration) throws Exception {
|
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()) {
|
try (ShellControl pc = LocalShell.getShell()) {
|
||||||
var a = app.get().toString();
|
|
||||||
pc.osascriptCommand(String.format(
|
pc.osascriptCommand(String.format(
|
||||||
"""
|
"""
|
||||||
if application "%s" is not running then
|
if application "iTerm" is not running then
|
||||||
launch application "%s"
|
launch application "iTerm"
|
||||||
delay 1
|
delay 1
|
||||||
tell application "%s"
|
tell application "iTerm"
|
||||||
tell current tab of current window
|
tell current tab of current window
|
||||||
close
|
close
|
||||||
end tell
|
end tell
|
||||||
end tell
|
end tell
|
||||||
end if
|
end if
|
||||||
tell application "%s"
|
tell application "iTerm"
|
||||||
activate
|
activate
|
||||||
create window with default profile command "%s"
|
create window with default profile command "%s"
|
||||||
end tell
|
end tell
|
||||||
""",
|
""",
|
||||||
a,
|
|
||||||
a,
|
|
||||||
a,
|
|
||||||
a,
|
|
||||||
configuration.getScriptFile().toString().replaceAll("\"", "\\\\\"")))
|
configuration.getScriptFile().toString().replaceAll("\"", "\\\\\"")))
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,9 +81,10 @@ public interface WezTerminalType extends ExternalTerminalType {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void launch(LaunchConfiguration configuration) throws Exception {
|
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()
|
var c = CommandBuilder.of()
|
||||||
.addFile(getApplicationPath()
|
.addFile(Path.of(path)
|
||||||
.orElseThrow()
|
|
||||||
.resolve("Contents")
|
.resolve("Contents")
|
||||||
.resolve("MacOS")
|
.resolve("MacOS")
|
||||||
.resolve("wezterm-gui")
|
.resolve("wezterm-gui")
|
||||||
|
|
Loading…
Reference in a new issue