javamacostextedit

How to find TextEdit on Mac?


My Java app will detect file extension and open it in Windows using wordpad, like this :

  public static Process Display_File(String File_Path)
  {
    String Command,Program,Suffix=File_Path.toLowerCase();
    Process process=null;

    if (Suffix.endsWith("txt") || Suffix.endsWith("json")) Program="C:\\Program Files (x86)\\Windows NT\\Accessories\\word_pad.exe ";

    Command=Program+"\""+File_Path+"\"";

    try { process=Runtime.getRuntime().exec(Command); }
    catch (Exception e) { e.printStackTrace(); }

    return process;
  }

But it won't work on Mac, I know there is TextEdit.app on Mac, so how to change the above code to run it on Mac ?

After the change, it looks like this :

  public static Process Display_File_On_Mac(String File_Path)
  {
    String Command,Program,Suffix=File_Path.toLowerCase();
    Process process=null;

    if (Suffix.endsWith("txt") || Suffix.endsWith("json")) Program="/Applications/TextEdit.app ";

    Command=Program+"\""+File_Path+"\"";

    try { process=Runtime.getRuntime().exec(Command); }
    catch (Exception e) { e.printStackTrace(); }

    return process;
  }

But I got this error :

java.io.IOException: Cannot run program "/Applications/TextEdit.app": error=13, Permission denied

How to fix it ?


Solution

  • On El-capitan, giving the below path worked for me: Program="open /Applications/TextEdit.app/Contents/MacOS/TextEdit";

    You can Navigate into the TextEdit.app folder from a terminal window and make sure you have the executable in the right place before trying it out.

    Also, you need to change the setting of command like this: Command = Program+ " "+File_Path;