add run as different user option to context menu

This commit is contained in:
Jeremy Wu
2019-12-12 20:55:41 +11:00
parent 2e9acc1fa8
commit da798e7a5a
4 changed files with 54 additions and 22 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

View File

@@ -5,6 +5,7 @@
<system:String x:Key="wox_plugin_cmd_relace_winr">Replace Win+R</system:String> <system:String x:Key="wox_plugin_cmd_relace_winr">Replace Win+R</system:String>
<system:String x:Key="wox_plugin_cmd_leave_cmd_open">Do not close Command Prompt after command execution</system:String> <system:String x:Key="wox_plugin_cmd_leave_cmd_open">Do not close Command Prompt after command execution</system:String>
<system:String x:Key="wox_plugin_cmd_always_run_as_administrator">Always run as administrator</system:String> <system:String x:Key="wox_plugin_cmd_always_run_as_administrator">Always run as administrator</system:String>
<system:String x:Key="wox_plugin_cmd_run_as_different_user">Run as different user</system:String>
<system:String x:Key="wox_plugin_cmd_plugin_name">Shell</system:String> <system:String x:Key="wox_plugin_cmd_plugin_name">Shell</system:String>
<system:String x:Key="wox_plugin_cmd_plugin_description">Allows to execute system commands from Wox. Commands should start with ></system:String> <system:String x:Key="wox_plugin_cmd_plugin_description">Allows to execute system commands from Wox. Commands should start with ></system:String>
<system:String x:Key="wox_plugin_cmd_cmd_has_been_executed_times">this command has been executed {0} times</system:String> <system:String x:Key="wox_plugin_cmd_cmd_has_been_executed_times">this command has been executed {0} times</system:String>

View File

@@ -1,8 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using WindowsInput; using WindowsInput;
using WindowsInput.Native; using WindowsInput.Native;
@@ -84,7 +86,7 @@ namespace Wox.Plugin.Shell
IcoPath = Image, IcoPath = Image,
Action = c => Action = c =>
{ {
Execute(m); Execute(Process.Start, PrepareProcessStartInfo(m));
return true; return true;
} }
})); }));
@@ -117,7 +119,7 @@ namespace Wox.Plugin.Shell
IcoPath = Image, IcoPath = Image,
Action = c => Action = c =>
{ {
Execute(m.Key); Execute(Process.Start, PrepareProcessStartInfo(m.Key));
return true; return true;
} }
}; };
@@ -136,7 +138,7 @@ namespace Wox.Plugin.Shell
IcoPath = Image, IcoPath = Image,
Action = c => Action = c =>
{ {
Execute(cmd); Execute(Process.Start, PrepareProcessStartInfo(cmd));
return true; return true;
} }
}; };
@@ -154,14 +156,14 @@ namespace Wox.Plugin.Shell
IcoPath = Image, IcoPath = Image,
Action = c => Action = c =>
{ {
Execute(m.Key); Execute(Process.Start, PrepareProcessStartInfo(m.Key));
return true; return true;
} }
}).Take(5); }).Take(5);
return history.ToList(); return history.ToList();
} }
private void Execute(string command, bool runAsAdministrator = false) private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdministrator = false)
{ {
command = command.Trim(); command = command.Trim();
command = Environment.ExpandEnvironmentVariables(command); command = Environment.ExpandEnvironmentVariables(command);
@@ -212,19 +214,33 @@ namespace Wox.Plugin.Shell
} }
else else
{ {
return; throw new NotImplementedException();
} }
info.UseShellExecute = true; info.UseShellExecute = true;
_settings.AddCmdHistory(command);
return info;
}
private void Execute(Func<ProcessStartInfo, Process> startProcess,ProcessStartInfo info)
{
try try
{ {
Process.Start(info); startProcess(info);
_settings.AddCmdHistory(command);
} }
catch (FileNotFoundException e) catch (FileNotFoundException e)
{ {
MessageBox.Show($"Command not found: {e.Message}"); var name = "Plugin: Shell";
var message = $"Command not found: {e.Message}";
_context.API.ShowMsg(name, message);
}
catch(Win32Exception e)
{
var name = "Plugin: Shell";
var message = $"Error running the command: {e.Message}";
_context.API.ShowMsg(name, message);
} }
} }
@@ -306,19 +322,31 @@ namespace Wox.Plugin.Shell
public List<Result> LoadContextMenus(Result selectedResult) public List<Result> LoadContextMenus(Result selectedResult)
{ {
return new List<Result> var resultlist = new List<Result>
{ {
new Result new Result
{ {
Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"), Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_different_user"),
Action = c => Action = c =>
{ {
Execute(selectedResult.Title, true); Task.Run(() =>Execute(ShellCommand.RunAsDifferentUser, PrepareProcessStartInfo(selectedResult.Title)));
return true; return true;
}, },
IcoPath = Image IcoPath = "Images/user.png"
} },
}; new Result
{
Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"),
Action = c =>
{
Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, true));
return true;
},
IcoPath = Image
}
};
return resultlist;
} }
} }
} }

View File

@@ -70,6 +70,9 @@
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Images\user.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Content Include="Languages\en.xaml"> <Content Include="Languages\en.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>