Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How can I start a process to open Finder to point to a file?

Discussion in 'Scripting' started by burnumd, Dec 2, 2011.

  1. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    (X-Posted from http://answers.unity3d.com/questions/191242/how-can-i-start-a-process-to-open-finder-to-point.html if you care a great deal for garnering points)

    I'm working on a standalone for OSX and Windows that will output some data to a text file, and I'd like for Unity to open the native file browser to point to that file when it's finished (so users can email me the file, etc.). So far I have the following:

    Code (csharp):
    1. void OnDataParsed (string path)
    2. {
    3. #if UNITY_STANDALONE_OSX || UNITY_EDITOR
    4.     try
    5.     {
    6.         Process process = new Process ();
    7.         process.StartInfo.FileName = "open";
    8.         process.StartInfo.Arguments = "-n -R " + path.Replace (" ", "\\ ");
    9.         UnityEngine.Debug.Log (process.StartInfo.FileName);
    10.         process.StartInfo.UseShellExecute = false;
    11.         process.StartInfo.RedirectStandardError = true;
    12.         process.ErrorDataReceived += OnFinderError;
    13.         if (process.Start ())
    14.         {
    15.             UnityEngine.Debug.Log ("Started properly");
    16.         }
    17.         else
    18.         {
    19.             UnityEngine.Debug.Log ("Error");
    20.         }
    21.     }
    22.     catch (System.Exception e)
    23.     {
    24.         UnityEngine.Debug.Log (e.Message);
    25.     }
    26. #endif
    27. }
    28.  
    29. void OnFinderError (object sender, DataReceivedEventArgs args)
    30. {
    31.     UnityEngine.Debug.Log (args.Data);
    32. }
    33.  
    Which, on running in the editor, prints "Started properly" but no finder window shows up. I see the same result regardless of whether "UseShellExecute" is true or false (I set it to false before I copy/pasted to redirect output). Any ideas on what I might be doing wrong?
     
  2. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Should I file this as a bug? Does anyone see a problem with this code?
     
  3. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    This problem appears to be a known issue with the version of Mono used by Unity (and it's currently listed as resolved). See
    here and here. Escaped spaces are ignored in arguments. The workaround is to put the entire path inside quotes.