public static int OpenApplication(ApplicationStartInfo application)
        {
            if (application == null)
            {
                throw new ArgumentNullException("application");
            }

            if (string.IsNullOrEmpty(application.Application) || !System.IO.Directory.Exists(application.Application))
            {
                throw new ArgumentException("Application is not valid");
            }

            NSUrl appUrl = NSUrl.FromFilename(application.Application);

            // TODO: Once the above bug is fixed, we can replace the code below with
            //NSRunningApplication app = NSWorkspace.SharedWorkspace.LaunchApplication (appUrl, 0, new NSDictionary (), null);

            var config = new NSMutableDictionary();

            if (application.Args != null && application.Args.Length > 0)
            {
                var args = new NSMutableArray();
                foreach (string arg in application.Args)
                {
                    args.Add(new NSString(arg));
                }
                config.Add(new NSString("NSWorkspaceLaunchConfigurationArguments"), args);
            }

            if (application.Environment != null && application.Environment.Count > 0)
            {
                var envValueStrings = application.Environment.Values.Select(t => new NSString(t)).ToArray();
                var envKeyStrings   = application.Environment.Keys.Select(t => new NSString(t)).ToArray();

                var envDict = new NSMutableDictionary();
                for (int i = 0; i < envValueStrings.Length; i++)
                {
                    envDict.Add(envKeyStrings[i], envValueStrings[i]);
                }

                config.Add(new NSString("NSWorkspaceLaunchConfigurationEnvironment"), envDict);
            }

            UInt32 options = 0;

            if (application.Async)
            {
                options |= (UInt32)LaunchOptions.NSWorkspaceLaunchAsync;
            }
            if (application.NewInstance)
            {
                options |= (UInt32)LaunchOptions.NSWorkspaceLaunchNewInstance;
            }

            IntPtr error;
            var    appHandle         = IntPtr_objc_msgSend_IntPtr_UInt32_IntPtr_IntPtr(NSWorkspace.SharedWorkspace.Handle, launchApplicationAtURLOptionsConfigurationErrorSelector, appUrl.Handle, options, config.Handle, out error);
            NSRunningApplication app = (NSRunningApplication)ObjCRuntime.Runtime.GetNSObject(appHandle);

            return(app.ProcessIdentifier);
        }
Exemplo n.º 2
0
        public override void DidFinishLaunching(NSNotification notification)
        {
            var apps = NSRunningApplication.GetRunningApplications(NSBundle.MainBundle.BundleIdentifier);

            if (apps.Length > 1)
            {
                Console.WriteLine($"アプリケーションはすでに起動しています。");
                NSApplication.SharedApplication.Terminate(this);
            }
        }
Exemplo n.º 3
0
        public void TestLaunchProcessAPIsForInvalidAppBundles()
        {
            var path = Util.CreateTmpDir("NonExisting.app");

            Assert.AreEqual(-1, LaunchServices.OpenApplication(path));
            Assert.AreEqual(-1, LaunchServices.OpenApplication(new ApplicationStartInfo(path)));

            NSRunningApplication app = LaunchServices.OpenApplicationInternal(new ApplicationStartInfo(path));

            Assert.IsNull(app);
        }
Exemplo n.º 4
0
        public void TestLaunchProcessAndTerminate()
        {
            NSRunningApplication app = LaunchServices.OpenApplicationInternal(new ApplicationStartInfo("/Applications/Calculator.app"));

            try {
                Assert.IsNotNull(app);
                Assert.That(app.ProcessIdentifier, Is.GreaterThan(-1));
            } finally {
                Assert.IsTrue(app.Terminate(), "Could not kill Calculator app");
            }
        }
Exemplo n.º 5
0
        public static bool KillProcess(int pid)
        {
            NSRunningApplication runningApp = NSRunningApplication.GetRunningApplication(pid);

            if (runningApp == null)
            {
                return(false);
            }

            return(runningApp.ForceTerminate());
        }
Exemplo n.º 6
0
        public void DisplayExistingUI()
        {
            var thisProcess = Process.GetCurrentProcess();
            var processes   = Process.GetProcessesByName(thisProcess.ProcessName).Where(p => p.Id != thisProcess.Id);

            foreach (Process runningProcess in processes)
            {
                NSRunningApplication app = NSRunningApplication.GetRunningApplication(runningProcess.Id);
                app.Activate(NSApplicationActivationOptions.ActivateAllWindows);
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Because we are creating our own mac application delegate we are removing / overriding
 /// the one that Avalonia creates. This causes the application to not be handled as it should.
 /// This is the Avalonia Implementation: https://github.com/AvaloniaUI/Avalonia/blob/5a2ef35dacbce0438b66d9f012e5f629045beb3d/native/Avalonia.Native/src/OSX/app.mm
 /// So what we are doing here is re-creating this implementation to mimick their behavior.
 /// </summary>
 /// <param name="notification"></param>
 public override void WillFinishLaunching(NSNotification notification)
 {
     if (NSApplication.SharedApplication.ActivationPolicy != NSApplicationActivationPolicy.Regular)
     {
         foreach (var x in NSRunningApplication.GetRunningApplications(@"com.apple.dock"))
         {
             x.Activate(NSApplicationActivationOptions.ActivateIgnoringOtherWindows);
             break;
         }
         NSApplication.SharedApplication.ActivationPolicy = NSApplicationActivationPolicy.Regular;
     }
 }
Exemplo n.º 8
0
        public static bool TerminateApplication(int pid, bool waitForExit = true)
        {
            var app = NSRunningApplication.GetRunningApplication(pid);

            if (app == null)
            {
                return(false);
            }

            if (app.Terminate())
            {
                while (null != NSRunningApplication.GetRunningApplication(pid))
                {
                }
            }
            return(true);
        }
Exemplo n.º 9
0
        public static bool IsIVPNAppIsRunning(out NSRunningApplication runningApplication)
        {
            runningApplication = null;

            NSWorkspace workspace = new NSWorkspace();

            foreach (var application in workspace.RunningApplications)
            {
                if (application.LocalizedName == "IVPN" &&
                    application.ProcessIdentifier != NSProcessInfo.ProcessInfo.ProcessIdentifier)
                {
                    runningApplication = application;
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 10
0
        public static IEnumerable <InspectableWindow> GetWindows(string bundleIdentifier,
                                                                 bool onScreenOnly = true)
        {
            var apps = NSRunningApplication.GetRunningApplications(bundleIdentifier);

            if (apps == null)
            {
                yield break;
            }

            var windows = GetWindowList(onScreenOnly);

            if (windows == null || windows.Length == 0)
            {
                yield break;
            }

            foreach (var app in apps)
            {
                foreach (var dict in windows)
                {
                    var windowOwnerPid = dict [keyWindowOwnerPid] as NSNumber;
                    if ((windowOwnerPid?.Int32Value ?? -1) != app.ProcessIdentifier)
                    {
                        continue;
                    }

                    CGRect bounds;
                    if (!TryGetBounds(dict, out bounds))
                    {
                        continue;
                    }

                    yield return(new InspectableWindow {
                        Application = app,
                        Title = GetTitle(dict),
                        Bounds = bounds
                    });
                }
            }
        }
Exemplo n.º 11
0
        public static bool Activate(int pid, int wait = 5000)
        {
            NSRunningApplication app = null;

            for (; wait > 0; wait -= 100)
            {
                if (app == null)
                {
                    app = NSRunningApplication.GetRunningApplication(pid);
                }

                if (app != null && app.FinishedLaunching)
                {
                    if (app.Activate(NSApplicationActivationOptions.ActivateIgnoringOtherWindows))
                    {
                        return(true);
                    }
                }

                NSThread.SleepFor(0.1);
            }
            return(false);
        }
Exemplo n.º 12
0
 public static bool IsRunning(int pid)
 {
     return(NSRunningApplication.GetRunningApplication(pid) != null);
 }