Skip to content

Commit 7a61db9

Browse files
authored
Merge pull request #98 from jjw24/add_shellplugin_runas_different_user
Add Run as different user to Shell plugin
2 parents 2e9acc1 + da798e7 commit 7a61db9

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed
64.1 KB
Loading

Plugins/Wox.Plugin.Shell/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<system:String x:Key="wox_plugin_cmd_relace_winr">Replace Win+R</system:String>
66
<system:String x:Key="wox_plugin_cmd_leave_cmd_open">Do not close Command Prompt after command execution</system:String>
77
<system:String x:Key="wox_plugin_cmd_always_run_as_administrator">Always run as administrator</system:String>
8+
<system:String x:Key="wox_plugin_cmd_run_as_different_user">Run as different user</system:String>
89
<system:String x:Key="wox_plugin_cmd_plugin_name">Shell</system:String>
910
<system:String x:Key="wox_plugin_cmd_plugin_description">Allows to execute system commands from Wox. Commands should start with ></system:String>
1011
<system:String x:Key="wox_plugin_cmd_cmd_has_been_executed_times">this command has been executed {0} times</system:String>

Plugins/Wox.Plugin.Shell/Main.cs

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Diagnostics;
45
using System.IO;
56
using System.Linq;
7+
using System.Threading.Tasks;
68
using System.Windows;
79
using WindowsInput;
810
using WindowsInput.Native;
@@ -84,7 +86,7 @@ public List<Result> Query(Query query)
8486
IcoPath = Image,
8587
Action = c =>
8688
{
87-
Execute(m);
89+
Execute(Process.Start, PrepareProcessStartInfo(m));
8890
return true;
8991
}
9092
}));
@@ -117,7 +119,7 @@ private List<Result> GetHistoryCmds(string cmd, Result result)
117119
IcoPath = Image,
118120
Action = c =>
119121
{
120-
Execute(m.Key);
122+
Execute(Process.Start, PrepareProcessStartInfo(m.Key));
121123
return true;
122124
}
123125
};
@@ -136,7 +138,7 @@ private Result GetCurrentCmd(string cmd)
136138
IcoPath = Image,
137139
Action = c =>
138140
{
139-
Execute(cmd);
141+
Execute(Process.Start, PrepareProcessStartInfo(cmd));
140142
return true;
141143
}
142144
};
@@ -154,14 +156,14 @@ private List<Result> ResultsFromlHistory()
154156
IcoPath = Image,
155157
Action = c =>
156158
{
157-
Execute(m.Key);
159+
Execute(Process.Start, PrepareProcessStartInfo(m.Key));
158160
return true;
159161
}
160162
}).Take(5);
161163
return history.ToList();
162164
}
163165

164-
private void Execute(string command, bool runAsAdministrator = false)
166+
private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdministrator = false)
165167
{
166168
command = command.Trim();
167169
command = Environment.ExpandEnvironmentVariables(command);
@@ -212,19 +214,33 @@ private void Execute(string command, bool runAsAdministrator = false)
212214
}
213215
else
214216
{
215-
return;
217+
throw new NotImplementedException();
216218
}
217219

218220
info.UseShellExecute = true;
219221

222+
_settings.AddCmdHistory(command);
223+
224+
return info;
225+
}
226+
227+
private void Execute(Func<ProcessStartInfo, Process> startProcess,ProcessStartInfo info)
228+
{
220229
try
221230
{
222-
Process.Start(info);
223-
_settings.AddCmdHistory(command);
231+
startProcess(info);
224232
}
225233
catch (FileNotFoundException e)
226234
{
227-
MessageBox.Show($"Command not found: {e.Message}");
235+
var name = "Plugin: Shell";
236+
var message = $"Command not found: {e.Message}";
237+
_context.API.ShowMsg(name, message);
238+
}
239+
catch(Win32Exception e)
240+
{
241+
var name = "Plugin: Shell";
242+
var message = $"Error running the command: {e.Message}";
243+
_context.API.ShowMsg(name, message);
228244
}
229245
}
230246

@@ -306,19 +322,31 @@ public string GetTranslatedPluginDescription()
306322

307323
public List<Result> LoadContextMenus(Result selectedResult)
308324
{
309-
return new List<Result>
325+
var resultlist = new List<Result>
310326
{
311-
new Result
312-
{
313-
Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"),
314-
Action = c =>
315-
{
316-
Execute(selectedResult.Title, true);
317-
return true;
318-
},
319-
IcoPath = Image
320-
}
321-
};
327+
new Result
328+
{
329+
Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_different_user"),
330+
Action = c =>
331+
{
332+
Task.Run(() =>Execute(ShellCommand.RunAsDifferentUser, PrepareProcessStartInfo(selectedResult.Title)));
333+
return true;
334+
},
335+
IcoPath = "Images/user.png"
336+
},
337+
new Result
338+
{
339+
Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"),
340+
Action = c =>
341+
{
342+
Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, true));
343+
return true;
344+
},
345+
IcoPath = Image
346+
}
347+
};
348+
349+
return resultlist;
322350
}
323351
}
324352
}

Plugins/Wox.Plugin.Shell/Wox.Plugin.Shell.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
</Compile>
7171
</ItemGroup>
7272
<ItemGroup>
73+
<None Include="Images\user.png">
74+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
75+
</None>
7376
<Content Include="Languages\en.xaml">
7477
<Generator>MSBuild:Compile</Generator>
7578
<SubType>Designer</SubType>

0 commit comments

Comments
 (0)