Skip to content

Instantly share code, notes, and snippets.

@dvdsgl
Created June 29, 2011 17:14
Show Gist options
  • Save dvdsgl/1054340 to your computer and use it in GitHub Desktop.
Save dvdsgl/1054340 to your computer and use it in GitHub Desktop.
Extensions to NSApplication for hiding Dock icon and relaunching.
using System;
using System.IO;
using System.Diagnostics;
using MonoMac.AppKit;
using MonoMac.Foundation;
namespace Awareness
{
public static class NSApplicationExtensions
{
public static bool IsShownInDock (this NSApplication self)
{
var path = Path.Combine (NSBundle.MainBundle.BundlePath, "Contents/Info.plist");
var plist = NSMutableDictionary.FromFile (path);
var uie = plist[(NSString) "LSUIElement"] as NSNumber;
return uie == null ? true : !uie.BoolValue;
}
public static void SetIsShownInDock (this NSApplication self, bool visible = true, bool relaunch = false)
{
if (visible == self.IsShownInDock ()) return;
var path = Path.Combine (NSBundle.MainBundle.BundlePath, "Contents/Info.plist");
var plist = NSMutableDictionary.FromFile (path);
plist[(NSString) "LSUIElement"] = NSNumber.FromBoolean (!visible);
if (plist.WriteToFile (path, true)) {
Process.Start ("/usr/bin/touch", string.Format ("-fmac \"{0}\"", path));
if (relaunch) self.Relaunch ();
}
}
public static void Relaunch (this NSApplication self)
{
var pid = NSProcessInfo.ProcessInfo.ProcessIdentifier;
var script = string.Format (@"
while [ `ps -p {0} | wc -l` -gt 1 ]; do sleep 0.1; done; open '{1}'
", pid, NSBundle.MainBundle.BundlePath);
NSTask.LaunchFromPath ("/bin/sh", new [] { "-c", script });
self.Terminate (self);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment