Пример #1
0
        public void OpenActionKeyword(String actword)
        {
            ProcessStartInfo info;
            var command = actword.Trim();

            command = Environment.ExpandEnvironmentVariables(command);
            var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

            var parts = command.Split(new[] { ' ' }, 2);

            if (parts.Length == 2)
            {
                var filename = parts[0];
                if (ExistInPath(filename))
                {
                    var arguments = parts[1];
                    info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments);
                }
                else
                {
                    info = ShellCommand.SetProcessStartInfo(command, workingDirectory);
                }
            }
            else
            {
                info = ShellCommand.SetProcessStartInfo(command, workingDirectory);
            }
            info.UseShellExecute = true;
            Process.Start(info);
        }
        public void ShellRun(string cmd, string filename = "cmd.exe")
        {
            var args = filename == "cmd.exe" ? $"/C {cmd}" : $"{cmd}";

            var startInfo = ShellCommand.SetProcessStartInfo(filename, arguments: args, createNoWindow: true);

            ShellCommand.Execute(startInfo);
        }
Пример #3
0
            public List <ContextMenuResult> ContextMenus(IPublicAPI api)
            {
                var contextMenus = new List <ContextMenuResult>();

                if (CanRunElevated)
                {
                    contextMenus.Add(
                        new ContextMenuResult
                    {
                        PluginName           = Assembly.GetExecutingAssembly().GetName().Name,
                        Title                = api.GetTranslation("wox_plugin_program_run_as_administrator"),
                        Glyph                = "\xE7EF",
                        FontFamily           = "Segoe MDL2 Assets",
                        AcceleratorKey       = "Enter",
                        AcceleratorModifiers = "Control,Shift",
                        Action               = _ =>
                        {
                            string command = "shell:AppsFolder\\" + UniqueIdentifier;
                            command.Trim();
                            command = Environment.ExpandEnvironmentVariables(command);

                            var info             = ShellCommand.SetProcessStartInfo(command, verb: "runas");
                            info.UseShellExecute = true;

                            Process.Start(info);
                            return(true);
                        }
                    }
                        );
                }
                contextMenus.Add(
                    new ContextMenuResult
                {
                    PluginName           = Assembly.GetExecutingAssembly().GetName().Name,
                    Title                = api.GetTranslation("wox_plugin_program_open_containing_folder"),
                    Glyph                = "\xE838",
                    FontFamily           = "Segoe MDL2 Assets",
                    AcceleratorKey       = "E",
                    AcceleratorModifiers = "Control,Shift",
                    Action               = _ =>
                    {
                        Main.StartProcess(Process.Start, new ProcessStartInfo("explorer", Package.Location));

                        return(true);
                    }
                });


                return(contextMenus);
            }
Пример #4
0
        public Result Result(string query, IPublicAPI api)
        {
            var result = new Result
            {
                SubTitle    = FullPath,
                IcoPath     = IcoPath,
                ContextData = this,
                Action      = e =>
                {
                    var info = new ProcessStartInfo
                    {
                        FileName         = FullPath,
                        WorkingDirectory = ParentDirectory
                    };

                    if (e.SpecialKeyState.CtrlPressed)
                    {
                        Main.StartProcess(Process.Start, ShellCommand.SetProcessStartInfo(info.FileName, info.WorkingDirectory, "", "runas"));
                    }
                    else
                    {
                        Main.StartProcess(Process.Start, info);
                    }

                    return(true);
                }
            };

            var match = StringMatcher.FuzzySearch(query, Name);

            result.Title = Name;
            result.Score = match.Score;
            result.TitleHighlightData = match.MatchData;

            return(result);
        }
Пример #5
0
        private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdministrator = false)
        {
            command = command.Trim();
            command = Environment.ExpandEnvironmentVariables(command);
            var workingDirectory      = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? string.Empty : "runas";

            ProcessStartInfo info;

            if (_settings.Shell == ExecutionShell.Cmd)
            {
                var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause";

                info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsAdministratorArg);
            }
            else if (_settings.Shell == ExecutionShell.Powershell)
            {
                string arguments;
                if (_settings.LeaveShellOpen)
                {
                    arguments = $"-NoExit \"{command}\"";
                }
                else
                {
                    arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\"";
                }

                info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsAdministratorArg);
            }
            else if (_settings.Shell == ExecutionShell.RunCommand)
            {
                // Open explorer if the path is a file or directory
                if (Directory.Exists(command) || File.Exists(command))
                {
                    info = ShellCommand.SetProcessStartInfo("explorer.exe", arguments: command, verb: runAsAdministratorArg);
                }
                else
                {
                    var parts = command.Split(new[] { ' ' }, 2);
                    if (parts.Length == 2)
                    {
                        var filename = parts[0];
                        if (ExistInPath(filename))
                        {
                            var arguments = parts[1];
                            info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsAdministratorArg);
                        }
                        else
                        {
                            info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
                        }
                    }
                    else
                    {
                        info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }

            info.UseShellExecute = true;

            _settings.AddCmdHistory(command);

            return(info);
        }
Пример #6
0
        private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdministrator = false)
        {
            command = command.Trim();
            command = Environment.ExpandEnvironmentVariables(command);
            var workingDirectory      = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? "" : "runas";

            ProcessStartInfo info;

            if (_settings.Shell == Shell.Cmd)
            {
                var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause";

                info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsAdministratorArg);
            }
            else if (_settings.Shell == Shell.Powershell)
            {
                string arguments;
                if (_settings.LeaveShellOpen)
                {
                    arguments = $"-NoExit \"{command}\"";
                }
                else
                {
                    arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\"";
                }

                info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsAdministratorArg);
            }
            else if (_settings.Shell == Shell.RunCommand)
            {
                var parts = command.Split(new[] { ' ' }, 2);
                if (parts.Length == 2)
                {
                    var filename = parts[0];
                    if (ExistInPath(filename))
                    {
                        var arguments = parts[1];
                        info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsAdministratorArg);
                    }
                    else
                    {
                        info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
                    }
                }
                else
                {
                    info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
                }
            }
            else if (_settings.Shell == Shell.Bash && _settings.SupportWSL)
            {
                string arguments;
                if (_settings.LeaveShellOpen)
                {
                    // FIXME: How to deal with commands containing single quote?
                    arguments = $"-c \'{command} ; $SHELL\'";
                }
                else
                {
                    arguments = $"-c \'{command} ; echo -n Press any key to exit... ; read -n1\'";
                }
                info = new ProcessStartInfo
                {
                    FileName  = "bash.exe",
                    Arguments = arguments
                };
            }
            else
            {
                throw new NotImplementedException();
            }

            info.UseShellExecute = true;

            _settings.AddCmdHistory(command);

            return(info);
        }
Пример #7
0
        public List <ContextMenuResult> ContextMenus(string queryArguments, IPublicAPI api)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            var contextMenus = new List <ContextMenuResult>();

            if (CanRunElevated)
            {
                contextMenus.Add(
                    new ContextMenuResult
                {
                    PluginName           = Assembly.GetExecutingAssembly().GetName().Name,
                    Title                = Properties.Resources.wox_plugin_program_run_as_administrator,
                    Glyph                = "\xE7EF",
                    FontFamily           = "Segoe MDL2 Assets",
                    AcceleratorKey       = Key.Enter,
                    AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
                    Action               = _ =>
                    {
                        string command = "shell:AppsFolder\\" + UniqueIdentifier;
                        command        = Environment.ExpandEnvironmentVariables(command.Trim());

                        var info             = ShellCommand.SetProcessStartInfo(command, verb: "runas");
                        info.UseShellExecute = true;
                        info.Arguments       = queryArguments;
                        Process.Start(info);
                        return(true);
                    },
                });
            }

            contextMenus.Add(
                new ContextMenuResult
            {
                PluginName           = Assembly.GetExecutingAssembly().GetName().Name,
                Title                = Properties.Resources.wox_plugin_program_open_containing_folder,
                Glyph                = "\xE838",
                FontFamily           = "Segoe MDL2 Assets",
                AcceleratorKey       = Key.E,
                AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
                Action               = _ =>
                {
                    Main.StartProcess(Process.Start, new ProcessStartInfo("explorer", Package.Location));

                    return(true);
                },
            });

            contextMenus.Add(new ContextMenuResult
            {
                PluginName           = Assembly.GetExecutingAssembly().GetName().Name,
                Title                = Properties.Resources.wox_plugin_program_open_in_console,
                Glyph                = "\xE756",
                FontFamily           = "Segoe MDL2 Assets",
                AcceleratorKey       = Key.C,
                AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
                Action               = (context) =>
                {
                    try
                    {
                        Helper.OpenInConsole(Package.Location);
                        return(true);
                    }
                    catch (Exception e)
                    {
                        Log.Exception($"Failed to open {Name} in console, {e.Message}", e, GetType());
                        return(false);
                    }
                },
            });

            return(contextMenus);
        }
Пример #8
0
        private List <Result> Commands()
        {
            var results = new List <Result>();

            results.AddRange(new[]
            {
                new Result
                {
                    Title    = "Shutdown",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_shutdown_computer"),
                    Glyph    = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe7e8"),
                    IcoPath  = "Images\\shutdown.png",
                    Action   = c =>
                    {
                        var reuslt = MessageBox.Show(
                            context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_shutdown_computer"),
                            context.API.GetTranslation("flowlauncher_plugin_sys_shutdown_computer"),
                            MessageBoxButton.YesNo, MessageBoxImage.Warning);
                        if (reuslt == MessageBoxResult.Yes)
                        {
                            Process.Start("shutdown", "/s /t 0");
                        }

                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Restart",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer"),
                    Glyph    = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe777"),
                    IcoPath  = "Images\\restart.png",
                    Action   = c =>
                    {
                        var result = MessageBox.Show(
                            context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_restart_computer"),
                            context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer"),
                            MessageBoxButton.YesNo, MessageBoxImage.Warning);
                        if (result == MessageBoxResult.Yes)
                        {
                            Process.Start("shutdown", "/r /t 0");
                        }

                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Restart With Advanced Boot Options",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_restart_advanced"),
                    Glyph    = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xecc5"),
                    IcoPath  = "Images\\restart_advanced.png",
                    Action   = c =>
                    {
                        var result = MessageBox.Show(
                            context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_restart_computer_advanced"),
                            context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer"),
                            MessageBoxButton.YesNo, MessageBoxImage.Warning);

                        if (result == MessageBoxResult.Yes)
                        {
                            Process.Start("shutdown", "/r /o /t 0");
                        }

                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Log Off",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_log_off"),
                    Glyph    = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe77b"),
                    IcoPath  = "Images\\logoff.png",
                    Action   = c => ExitWindowsEx(EWX_LOGOFF, 0)
                },
                new Result
                {
                    Title    = "Lock",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_lock"),
                    Glyph    = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe72e"),
                    IcoPath  = "Images\\lock.png",
                    Action   = c =>
                    {
                        LockWorkStation();
                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Sleep",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_sleep"),
                    Glyph    = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xec46"),
                    IcoPath  = "Images\\sleep.png",
                    Action   = c => FormsApplication.SetSuspendState(PowerState.Suspend, false, false)
                },
                new Result
                {
                    Title    = "Hibernate",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_hibernate"),
                    Glyph    = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe945"),
                    IcoPath  = "Images\\hibernate.png",
                    Action   = c =>
                    {
                        var info             = ShellCommand.SetProcessStartInfo("shutdown", arguments: "/h");
                        info.WindowStyle     = ProcessWindowStyle.Hidden;
                        info.UseShellExecute = true;

                        ShellCommand.Execute(info);

                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Empty Recycle Bin",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_emptyrecyclebin"),
                    IcoPath  = "Images\\recyclebin.png",
                    Glyph    = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xe74d"),
                    Action   = c =>
                    {
                        // http://www.pinvoke.net/default.aspx/shell32/SHEmptyRecycleBin.html
                        // FYI, couldn't find documentation for this but if the recycle bin is already empty, it will return -2147418113 (0x8000FFFF (E_UNEXPECTED))
                        // 0 for nothing
                        var result = SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0);
                        if (result != (uint)HRESULT.S_OK && result != (uint)0x8000FFFF)
                        {
                            MessageBox.Show($"Error emptying recycle bin, error code: {result}\n" +
                                            "please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137",
                                            "Error",
                                            MessageBoxButton.OK, MessageBoxImage.Error);
                        }

                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Exit",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_exit"),
                    IcoPath  = "Images\\app.png",
                    Action   = c =>
                    {
                        Application.Current.MainWindow.Close();
                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Save Settings",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_save_all_settings"),
                    IcoPath  = "Images\\app.png",
                    Action   = c =>
                    {
                        context.API.SaveAppAllSettings();
                        context.API.ShowMsg(context.API.GetTranslation("flowlauncher_plugin_sys_dlgtitle_success"),
                                            context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_all_settings_saved"));
                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Restart Flow Launcher",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_restart"),
                    IcoPath  = "Images\\app.png",
                    Action   = c =>
                    {
                        context.API.RestartApp();
                        return(false);
                    }
                },
                new Result
                {
                    Title    = "Settings",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_setting"),
                    IcoPath  = "Images\\app.png",
                    Action   = c =>
                    {
                        context.API.OpenSettingDialog();
                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Reload Plugin Data",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_reload_plugin_data"),
                    IcoPath  = "Images\\app.png",
                    Action   = c =>
                    {
                        // Hide the window first then show msg after done because sometimes the reload could take a while, so not to make user think it's frozen.
                        Application.Current.MainWindow.Hide();

                        context.API.ReloadAllPluginData().ContinueWith(_ =>
                                                                       context.API.ShowMsg(
                                                                           context.API.GetTranslation("flowlauncher_plugin_sys_dlgtitle_success"),
                                                                           context.API.GetTranslation(
                                                                               "flowlauncher_plugin_sys_dlgtext_all_applicableplugins_reloaded")));

                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Check For Update",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_check_for_update"),
                    IcoPath  = "Images\\checkupdate.png",
                    Action   = c =>
                    {
                        Application.Current.MainWindow.Hide();
                        context.API.CheckForNewUpdate();
                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Open Log Location",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_open_log_location"),
                    IcoPath  = "Images\\app.png",
                    Action   = c =>
                    {
                        var logPath = Path.Combine(DataLocation.DataDirectory(), "Logs", Constant.Version);
                        context.API.OpenDirectory(logPath);
                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Flow Launcher Tips",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_open_docs_tips"),
                    IcoPath  = "Images\\app.png",
                    Action   = c =>
                    {
                        context.API.OpenUrl(Constant.Documentation);
                        return(true);
                    }
                },
                new Result
                {
                    Title    = "Flow Launcher UserData Folder",
                    SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_open_userdata_location"),
                    IcoPath  = "Images\\app.png",
                    Action   = c =>
                    {
                        context.API.OpenDirectory(DataLocation.DataDirectory());
                        return(true);
                    }
                }
            });

            return(results);
        }