📄 platformmanagerimpl.java
字号:
// Public utility methods not shared across the interface
/**
* Plays the system alert (the jingle is specified by the user in System Preferences)
*/
public void playSystemAlert()
{
try
{
performRuntimeExec(new String[]{"beep"});
}
catch (IOException e)
{
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING,
"Cannot play system alert"));
Logger.log(new LogEvent(LOGID, "", e));
}
}
/**
* <p>Shows the given file or directory in Finder</p>
* @param path Absolute path to the file or directory
*/
public void showInFinder(File path)
{
boolean useOSA = !NativeInvocationBridge.sharedInstance().isEnabled() || !NativeInvocationBridge.sharedInstance().showInFinder(path);
if(useOSA)
{
StringBuffer sb = new StringBuffer();
sb.append("tell application \"");
sb.append(getFileBrowserName());
sb.append("\" to reveal (posix file \"");
sb.append(path);
sb.append("\" as alias)");
try
{
performOSAScript(sb);
}
catch (IOException e)
{
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, e
.getMessage()));
}
}
}
/**
* <p>Shows the given file or directory in Terminal by executing cd /absolute/path/to</p>
* @param path Absolute path to the file or directory
*/
public void showInTerminal(String path)
{
showInTerminal(new File(path));
}
/**
* <p>Shows the given file or directory in Terminal by executing cd /absolute/path/to</p>
* @param path Absolute path to the file or directory
*/
public void showInTerminal(File path)
{
if (path.isFile())
{
path = path.getParentFile();
}
if (path != null && path.isDirectory())
{
StringBuffer sb = new StringBuffer();
sb.append("tell application \"");
sb.append("Terminal");
sb.append("\" to do script \"cd ");
sb.append(path.getAbsolutePath().replaceAll(" ", "\\ "));
sb.append("\"");
try
{
performOSAScript(sb);
}
catch (IOException e)
{
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, e
.getMessage()));
}
}
else
{
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "Cannot find "
+ path.getName()));
}
}
// Internal utility methods
/**
* Compiles a new AppleScript instance and runs it
* @param cmd AppleScript command to execute; do not surround command with extra quotation marks
* @return Output of the script
* @throws IOException If the script failed to execute
*/
protected static String performOSAScript(CharSequence cmd) throws IOException
{
return performOSAScript(new CharSequence[]{cmd});
}
/**
* Compiles a new AppleScript instance and runs it
* @param cmds AppleScript Sequence of commands to execute; do not surround command with extra quotation marks
* @return Output of the script
* @throws IOException If the script failed to execute
*/
protected static String performOSAScript(CharSequence[] cmds) throws IOException
{
long start = System.currentTimeMillis();
Debug.outNoStack("Executing OSAScript: ");
for (int i = 0; i < cmds.length; i++)
{
Debug.outNoStack("\t" + cmds[i]);
}
String[] cmdargs = new String[2 * cmds.length + 1];
cmdargs[0] = "osascript";
for (int i = 0; i < cmds.length; i++)
{
cmdargs[i * 2 + 1] = "-e";
cmdargs[i * 2 + 2] = String.valueOf(cmds[i]);
}
Process osaProcess = performRuntimeExec(cmdargs);
BufferedReader reader = new BufferedReader(new InputStreamReader(osaProcess.getInputStream()));
String line = reader.readLine();
reader.close();
Debug.outNoStack("OSAScript Output: " + line);
reader = new BufferedReader(new InputStreamReader(osaProcess.getErrorStream()));
String errorMsg = reader.readLine();
reader.close();
Debug.outNoStack("OSAScript Error (if any): " + errorMsg);
Debug.outNoStack(MessageFormat.format("OSAScript execution ended ({0}ms)", new Object[]{String.valueOf(System.currentTimeMillis() - start)}));
if (errorMsg != null)
{
throw new IOException(errorMsg);
}
return line;
}
/**
* Compiles a new AppleScript instance and runs it
* @param script AppleScript file (.scpt) to execute
* @return Output of the script
* @throws IOException If the script failed to execute
*/
protected static String performOSAScript(File script) throws IOException
{
long start = System.currentTimeMillis();
Debug.outNoStack("Executing OSAScript from file: " + script.getPath());
Process osaProcess = performRuntimeExec(new String[]{"osascript", script.getPath()});
BufferedReader reader = new BufferedReader(new InputStreamReader(osaProcess.getInputStream()));
String line = reader.readLine();
reader.close();
Debug.outNoStack("OSAScript Output: " + line);
reader = new BufferedReader(new InputStreamReader(osaProcess.getErrorStream()));
String errorMsg = reader.readLine();
reader.close();
Debug.outNoStack("OSAScript Error (if any): " + errorMsg);
Debug.outNoStack(MessageFormat.format("OSAScript execution ended ({0}ms)", new Object[]{String.valueOf(System.currentTimeMillis() - start)}));
if (errorMsg != null)
{
throw new IOException(errorMsg);
}
return line;
}
/**
* Compiles a new AppleScript instance to the specified location
* @param cmd Command to compile; do not surround command with extra quotation marks
* @param destination Destination location of the AppleScript file
* @return True if compiled successfully
*/
protected static boolean compileOSAScript(CharSequence cmd, File destination)
{
return compileOSAScript(new CharSequence[]{cmd}, destination);
}
/**
* Compiles a new AppleScript instance to the specified location
* @param cmds Sequence of commands to compile; do not surround command with extra quotation marks
* @param destination Destination location of the AppleScript file
* @return True if compiled successfully
*/
protected static boolean compileOSAScript(CharSequence[] cmds, File destination)
{
long start = System.currentTimeMillis();
Debug.outNoStack("Compiling OSAScript: " + destination.getPath());
for (int i = 0; i < cmds.length; i++)
{
Debug.outNoStack("\t" + cmds[i]);
}
String[] cmdargs = new String[2 * cmds.length + 3];
cmdargs[0] = "osacompile";
for (int i = 0; i < cmds.length; i++)
{
cmdargs[i * 2 + 1] = "-e";
cmdargs[i * 2 + 2] = String.valueOf(cmds[i]);
}
cmdargs[cmdargs.length - 2] = "-o";
cmdargs[cmdargs.length - 1] = destination.getPath();
String errorMsg;
try
{
Process osaProcess = performRuntimeExec(cmdargs);
BufferedReader reader = new BufferedReader(new InputStreamReader(osaProcess.getErrorStream()));
errorMsg = reader.readLine();
reader.close();
}
catch (IOException e)
{
Debug.outNoStack("OSACompile Execution Failed: " + e.getMessage());
Debug.printStackTrace(e);
return false;
}
Debug.outNoStack("OSACompile Error (if any): " + errorMsg);
Debug.outNoStack(MessageFormat.format("OSACompile execution ended ({0}ms)", new Object[]{String.valueOf(System.currentTimeMillis() - start)}));
return (errorMsg == null);
}
/**
* @see Runtime#exec(String[])
*/
protected static Process performRuntimeExec(String[] cmdargs) throws IOException
{
try
{
return Runtime.getRuntime().exec(cmdargs);
}
catch (IOException e)
{
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, e.getMessage(), e));
throw e;
}
}
/**
* <p>Gets the preferred file browser name</p>
* <p>Currently supported browsers are Path Finder and Finder. If Path Finder is currently running
* (not just installed), then "Path Finder is returned; else, "Finder" is returned.</p>
* @return "Path Finder" if it is currently running; else "Finder"
*/
private static String getFileBrowserName()
{
try
{
// slowwwwwwww
if ("true".equalsIgnoreCase(performOSAScript("tell application \"System Events\" to exists process \"Path Finder\"")))
{
Debug.outNoStack("Path Finder is running");
return "Path Finder";
}
else
{
return "Finder";
}
}
catch (IOException e)
{
Debug.printStackTrace(e);
Logger.log(new LogEvent(LOGID, e.getMessage(), e));
return "Finder";
}
}
public boolean
testNativeAvailability(
String name )
throws PlatformManagerException
{
throw new PlatformManagerException("Unsupported capability called on platform manager");
}
public void
traceRoute(
InetAddress interface_address,
InetAddress target,
PlatformManagerPingCallback callback )
throws PlatformManagerException
{
throw new PlatformManagerException("Unsupported capability called on platform manager");
}
public void
ping(
InetAddress interface_address,
InetAddress target,
PlatformManagerPingCallback callback )
throws PlatformManagerException
{
throw new PlatformManagerException("Unsupported capability called on platform manager");
}
public void
addListener(
PlatformManagerListener listener )
{
}
public void
removeListener(
PlatformManagerListener listener )
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -