OpenInFileBrowser

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Added code to determine OS)
 
Line 12: Line 12:
 
get
 
get
 
{
 
{
return SystemInfo.operatingSystem.IndexOf("Mac OS") != -1;
+
return UnityEngine.SystemInfo.operatingSystem.IndexOf("Mac OS") != -1;
 
}
 
}
 
}
 
}
 
+
 
public static bool IsInWinOS
 
public static bool IsInWinOS
 
{
 
{
 
get
 
get
 
{
 
{
return SystemInfo.operatingSystem.IndexOf("Windows") != -1;
+
return UnityEngine.SystemInfo.operatingSystem.IndexOf("Windows") != -1;
 
}
 
}
 
}
 
}
 
+
[MenuItem("Window/Test OpenInFileBrowser")]
+
[UnityEditor.MenuItem("Window/Test OpenInFileBrowser")]
 
public static void Test()
 
public static void Test()
 
{
 
{
//string path = "/Users/Ferds/Unity Projects/BuildReportTool/BuildReportUnityProject/Assets/BuildReportDebug";
+
Open(UnityEngine.Application.dataPath);
//string path = "/Users/Ferds/Unity Projects/BuildReportTool/BuildReportUnityProject/Assets/BuildReportDebug/EditorMorel.log.txt";
+
//string path = "/Users/Ferds/UnityBuildReports/";
+
string path = "/Users/Ferds/UnityBuildReports/test4.xml";
+
 
+
Open(path);
+
 
}
 
}
 
+
 
+
 
public static void OpenInMac(string path)
 
public static void OpenInMac(string path)
 
{
 
{
 
bool openInsidesOfFolder = false;
 
bool openInsidesOfFolder = false;
 
+
 
// try mac
 
// try mac
 
string macPath = path.Replace("\\", "/"); // mac finder doesn't like backward slashes
 
string macPath = path.Replace("\\", "/"); // mac finder doesn't like backward slashes
 
+
if (Directory.Exists(macPath)) // if path requested is a folder, automatically open insides of that folder
+
if ( System.IO.Directory.Exists(macPath) ) // if path requested is a folder, automatically open insides of that folder
 
{
 
{
 
openInsidesOfFolder = true;
 
openInsidesOfFolder = true;
 
}
 
}
 
+
//Debug.Log("macPath: " + macPath);
+
if ( !macPath.StartsWith("\"") )
//Debug.Log("openInsidesOfFolder: " + openInsidesOfFolder);
+
 
+
if (!macPath.StartsWith("\""))
+
 
{
 
{
 
macPath = "\"" + macPath;
 
macPath = "\"" + macPath;
 
}
 
}
if (!macPath.EndsWith("\""))
+
 +
if ( !macPath.EndsWith("\"") )
 
{
 
{
 
macPath = macPath + "\"";
 
macPath = macPath + "\"";
 
}
 
}
 +
 
string arguments = (openInsidesOfFolder ? "" : "-R ") + macPath;
 
string arguments = (openInsidesOfFolder ? "" : "-R ") + macPath;
//Debug.Log("arguments: " + arguments);
+
 
try
 
try
 
{
 
{
 
System.Diagnostics.Process.Start("open", arguments);
 
System.Diagnostics.Process.Start("open", arguments);
 
}
 
}
catch(System.ComponentModel.Win32Exception e)
+
catch ( System.ComponentModel.Win32Exception e )
 
{
 
{
 
// tried to open mac finder in windows
 
// tried to open mac finder in windows
Line 73: Line 66:
 
}
 
}
 
}
 
}
 
+
 
public static void OpenInWin(string path)
 
public static void OpenInWin(string path)
 
{
 
{
 
bool openInsidesOfFolder = false;
 
bool openInsidesOfFolder = false;
 
+
 
// try windows
 
// try windows
 
string winPath = path.Replace("/", "\\"); // windows explorer doesn't like forward slashes
 
string winPath = path.Replace("/", "\\"); // windows explorer doesn't like forward slashes
 
+
if (Directory.Exists(winPath)) // if path requested is a folder, automatically open insides of that folder
+
if ( System.IO.Directory.Exists(winPath) ) // if path requested is a folder, automatically open insides of that folder
 
{
 
{
 
openInsidesOfFolder = true;
 
openInsidesOfFolder = true;
 
}
 
}
 +
 
try
 
try
 
{
 
{
 
System.Diagnostics.Process.Start("explorer.exe", (openInsidesOfFolder ? "/root," : "/select,") + winPath);
 
System.Diagnostics.Process.Start("explorer.exe", (openInsidesOfFolder ? "/root," : "/select,") + winPath);
 
}
 
}
catch(System.ComponentModel.Win32Exception e)
+
catch ( System.ComponentModel.Win32Exception e )
 
{
 
{
 
// tried to open win explorer in mac
 
// tried to open win explorer in mac
Line 97: Line 91:
 
}
 
}
 
}
 
}
 
+
 
public static void Open(string path)
 
public static void Open(string path)
 
{
 
{
if (IsInWinOS)
+
if ( IsInWinOS )
 
{
 
{
OpenInWinFileBrowser(path);
+
OpenInWin(path);
 
}
 
}
else if (IsInMacOS)
+
else if ( IsInMacOS )
 
{
 
{
OpenInMacFileBrowser(path);
+
OpenInMac(path);
 
}
 
}
 
else // couldn't determine OS
 
else // couldn't determine OS
 
{
 
{
OpenInWinFileBrowser(path);
+
OpenInWin(path);
OpenInMacFileBrowser(path);
+
OpenInMac(path);
 
}
 
}
 
}
 
}

Latest revision as of 15:12, 9 April 2014

Use `OpenInFileBrowser.Open()` for a cross-platform way of opening any file/folder. This gives your code a "Reveal in Finder" or "Open in Explorer" functionality. Can be useful for editor scripts but probably can be used for non-editor scripts for desktop builds also (for in-game mod editors perhaps?).

If you specify a path with a file at the end, the code will open your file browser with that file selected/highlighted. If you specify a folder instead, it will open the contents of that folder with nothing in it selected.

I place this code in the public domain. Feel free to use it!

public static class OpenInFileBrowser
{
	public static bool IsInMacOS
	{
		get
		{
			return UnityEngine.SystemInfo.operatingSystem.IndexOf("Mac OS") != -1;
		}
	}
 
	public static bool IsInWinOS
	{
		get
		{
			return UnityEngine.SystemInfo.operatingSystem.IndexOf("Windows") != -1;
		}
	}
 
	[UnityEditor.MenuItem("Window/Test OpenInFileBrowser")]
	public static void Test()
	{
		Open(UnityEngine.Application.dataPath);
	}
 
	public static void OpenInMac(string path)
	{
		bool openInsidesOfFolder = false;
 
		// try mac
		string macPath = path.Replace("\\", "/"); // mac finder doesn't like backward slashes
 
		if ( System.IO.Directory.Exists(macPath) ) // if path requested is a folder, automatically open insides of that folder
		{
			openInsidesOfFolder = true;
		}
 
		if ( !macPath.StartsWith("\"") )
		{
			macPath = "\"" + macPath;
		}
 
		if ( !macPath.EndsWith("\"") )
		{
			macPath = macPath + "\"";
		}
 
		string arguments = (openInsidesOfFolder ? "" : "-R ") + macPath;
 
		try
		{
			System.Diagnostics.Process.Start("open", arguments);
		}
		catch ( System.ComponentModel.Win32Exception e )
		{
			// tried to open mac finder in windows
			// just silently skip error
			// we currently have no platform define for the current OS we are in, so we resort to this
			e.HelpLink = ""; // do anything with this variable to silence warning about not using it
		}
	}
 
	public static void OpenInWin(string path)
	{
		bool openInsidesOfFolder = false;
 
		// try windows
		string winPath = path.Replace("/", "\\"); // windows explorer doesn't like forward slashes
 
		if ( System.IO.Directory.Exists(winPath) ) // if path requested is a folder, automatically open insides of that folder
		{
			openInsidesOfFolder = true;
		}
 
		try
		{
			System.Diagnostics.Process.Start("explorer.exe", (openInsidesOfFolder ? "/root," : "/select,") + winPath);
		}
		catch ( System.ComponentModel.Win32Exception e )
		{
			// tried to open win explorer in mac
			// just silently skip error
			// we currently have no platform define for the current OS we are in, so we resort to this
			e.HelpLink = ""; // do anything with this variable to silence warning about not using it
		}
	}
 
	public static void Open(string path)
	{
		if ( IsInWinOS )
		{
			OpenInWin(path);
		}
		else if ( IsInMacOS )
		{
			OpenInMac(path);
		}
		else // couldn't determine OS
		{
			OpenInWin(path);
			OpenInMac(path);
		}
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox