Support special folder variables in Actions

This commit is contained in:
Jaex 2019-10-09 23:03:36 +03:00
parent a8f992a203
commit c85f23cc96
7 changed files with 60 additions and 13 deletions

View file

@ -52,9 +52,16 @@ public ExternalProgram(string name, string path) : this()
Path = path; Path = path;
} }
public string GetFullPath()
{
return Helpers.ExpandFolderVariables(Path);
}
public string Run(string inputPath) public string Run(string inputPath)
{ {
if (!string.IsNullOrEmpty(Path) && File.Exists(Path) && !string.IsNullOrWhiteSpace(inputPath)) string path = GetFullPath();
if (!string.IsNullOrEmpty(path) && File.Exists(path) && !string.IsNullOrWhiteSpace(inputPath))
{ {
inputPath = inputPath.Trim('"'); inputPath = inputPath.Trim('"');
@ -91,7 +98,7 @@ public string Run(string inputPath)
{ {
ProcessStartInfo psi = new ProcessStartInfo() ProcessStartInfo psi = new ProcessStartInfo()
{ {
FileName = Path, FileName = path,
Arguments = arguments, Arguments = arguments,
UseShellExecute = false, UseShellExecute = false,
CreateNoWindow = HiddenWindow CreateNoWindow = HiddenWindow

View file

@ -325,13 +325,18 @@ public static string GetRandomLineFromFile(string path)
public static string GetValidFileName(string fileName, string separator = "") public static string GetValidFileName(string fileName, string separator = "")
{ {
char[] invalidFileNameChars = Path.GetInvalidFileNameChars(); char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
if (string.IsNullOrEmpty(separator)) if (string.IsNullOrEmpty(separator))
{ {
return new string(fileName.Where(c => !invalidFileNameChars.Contains(c)).ToArray()); return new string(fileName.Where(c => !invalidFileNameChars.Contains(c)).ToArray());
} }
else else
{ {
invalidFileNameChars.ForEach(x => fileName = fileName.Replace(x.ToString(), separator)); foreach (char invalidFileNameChar in invalidFileNameChars)
{
fileName = fileName.Replace(invalidFileNameChar.ToString(), separator);
}
return fileName.Trim().Replace(separator + separator, separator); return fileName.Trim().Replace(separator + separator, separator);
} }
} }
@ -730,6 +735,11 @@ public static bool BrowseFile(string title, TextBox tb, string initialDirectory
{ {
string path = tb.Text; string path = tb.Text;
if (detectSpecialFolders)
{
path = ExpandFolderVariables(path);
}
if (!string.IsNullOrEmpty(path)) if (!string.IsNullOrEmpty(path))
{ {
path = Path.GetDirectoryName(path); path = Path.GetDirectoryName(path);
@ -750,7 +760,15 @@ public static bool BrowseFile(string title, TextBox tb, string initialDirectory
if (ofd.ShowDialog() == DialogResult.OK) if (ofd.ShowDialog() == DialogResult.OK)
{ {
tb.Text = detectSpecialFolders ? GetVariableFolderPath(ofd.FileName) : ofd.FileName; string fileName = ofd.FileName;
if (detectSpecialFolders)
{
fileName = GetVariableFolderPath(fileName);
}
tb.Text = fileName;
return true; return true;
} }
} }
@ -796,7 +814,10 @@ public static string GetVariableFolderPath(string path)
{ {
try try
{ {
GetEnums<Environment.SpecialFolder>().ForEach(x => path = path.Replace(Environment.GetFolderPath(x), $"%{x}%", StringComparison.InvariantCultureIgnoreCase)); foreach (Environment.SpecialFolder specialFolder in GetEnums<Environment.SpecialFolder>())
{
path = path.Replace(Environment.GetFolderPath(specialFolder), $"%{specialFolder}%", StringComparison.OrdinalIgnoreCase);
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -813,7 +834,11 @@ public static string ExpandFolderVariables(string path)
{ {
try try
{ {
GetEnums<Environment.SpecialFolder>().ForEach(x => path = path.Replace($"%{x}%", Environment.GetFolderPath(x), StringComparison.InvariantCultureIgnoreCase)); foreach (Environment.SpecialFolder specialFolder in GetEnums<Environment.SpecialFolder>())
{
path = path.Replace($"%{specialFolder}%", Environment.GetFolderPath(specialFolder), StringComparison.OrdinalIgnoreCase);
}
path = Environment.ExpandEnvironmentVariables(path); path = Environment.ExpandEnvironmentVariables(path);
} }
catch (Exception e) catch (Exception e)
@ -825,6 +850,18 @@ public static string ExpandFolderVariables(string path)
return path; return path;
} }
public static string OutputSpecialFolders()
{
StringBuilder sb = new StringBuilder();
foreach (Environment.SpecialFolder specialFolder in GetEnums<Environment.SpecialFolder>())
{
sb.AppendLine(string.Format("{0,-25}{1}", specialFolder, Environment.GetFolderPath(specialFolder)));
}
return sb.ToString();
}
public static bool WaitWhile(Func<bool> check, int interval, int timeout = -1) public static bool WaitWhile(Func<bool> check, int interval, int timeout = -1)
{ {
Stopwatch timer = Stopwatch.StartNew(); Stopwatch timer = Stopwatch.StartNew();

View file

@ -112,12 +112,14 @@ public override void OnDraw(Graphics g)
protected void DrawFreehand(Graphics g) protected void DrawFreehand(Graphics g)
{ {
int borderSize = Math.Max(BorderSize, 1);
if (Shadow) if (Shadow)
{ {
DrawFreehand(g, ShadowColor, Math.Max(BorderSize, 1), positions.Select(x => x.Add(ShadowOffset)).ToArray()); DrawFreehand(g, ShadowColor, borderSize, positions.Select(x => x.Add(ShadowOffset)).ToArray());
} }
DrawFreehand(g, BorderColor, Math.Max(BorderSize, 1), positions.ToArray()); DrawFreehand(g, BorderColor, borderSize, positions.ToArray());
} }
protected void DrawFreehand(Graphics g, Color borderColor, int borderSize, Point[] points) protected void DrawFreehand(Graphics g, Color borderColor, int borderSize, Point[] points)

View file

@ -137,6 +137,8 @@ public override void OnDraw(Graphics g)
protected void DrawLine(Graphics g) protected void DrawLine(Graphics g)
{ {
int borderSize = Math.Max(BorderSize, 1);
if (Shadow) if (Shadow)
{ {
Point[] shadowPoints = new Point[Points.Length]; Point[] shadowPoints = new Point[Points.Length];
@ -146,10 +148,10 @@ protected void DrawLine(Graphics g)
shadowPoints[i] = Points[i].Add(ShadowOffset); shadowPoints[i] = Points[i].Add(ShadowOffset);
} }
DrawLine(g, ShadowColor, Math.Max(BorderSize, 1), shadowPoints); DrawLine(g, ShadowColor, borderSize, shadowPoints);
} }
DrawLine(g, BorderColor, Math.Max(BorderSize, 1), Points); DrawLine(g, BorderColor, borderSize, Points);
} }
protected void DrawLine(Graphics g, Color borderColor, int borderSize, Point[] points) protected void DrawLine(Graphics g, Color borderColor, int borderSize, Point[] points)

View file

@ -153,7 +153,6 @@ private void InitializeComponent()
this.Controls.Add(this.lblArgs); this.Controls.Add(this.lblArgs);
this.Controls.Add(this.lblPath); this.Controls.Add(this.lblPath);
this.Controls.Add(this.lblName); this.Controls.Add(this.lblName);
this.MaximizeBox = false;
this.Name = "ActionsForm"; this.Name = "ActionsForm";
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();

View file

@ -56,7 +56,7 @@ public ActionsForm(ExternalProgram fileAction)
private void btnPathBrowse_Click(object sender, EventArgs e) private void btnPathBrowse_Click(object sender, EventArgs e)
{ {
Helpers.BrowseFile(txtPath); Helpers.BrowseFile(txtPath, "", true);
} }
private void txtOutputExtension_TextChanged(object sender, EventArgs e) private void txtOutputExtension_TextChanged(object sender, EventArgs e)

View file

@ -895,7 +895,7 @@ private void UpdateActionsMenu(string filePath)
try try
{ {
using (Icon icon = NativeMethods.GetFileIcon(action.Path, true)) using (Icon icon = NativeMethods.GetFileIcon(action.GetFullPath(), true))
{ {
if (icon != null && icon.Width > 0 && icon.Height > 0) if (icon != null && icon.Width > 0 && icon.Height > 0)
{ {