Skip to content

Commit 32f95aa

Browse files
committed
Fixed code from #122 and implemented environment path support for open actions
Made a safe version of getting environment variables, as some might not exist on some computers, resulting in issues. This should be completely exception-free,
1 parent 6d00525 commit 32f95aa

File tree

2 files changed

+48
-86
lines changed

2 files changed

+48
-86
lines changed

AssistantComputerControl/Actions.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* AssistantComputerControl
33
* Made by Albert MN.
4-
* Updated: v1.4.4, 19-05-2021
4+
* Updated: v1.4.6, 17-01-2022
55
*
66
* Use:
77
* - Functions for all the actions
@@ -402,8 +402,10 @@ public void Music(string parameter) {
402402
}
403403

404404
public void Open(string parameter) {
405-
string location = ActionChecker.GetSecondaryParam(parameter)[0], arguments = (ActionChecker.GetSecondaryParam(parameter).Length > 1 ? ActionChecker.GetSecondaryParam(parameter)[1] : null);
406-
string fileLocation = (!location.Contains(@":\") || !location.Contains(@":/")) ? "" : location;
405+
string location = ActionChecker.CheckForEnvironmentPath(ActionChecker.GetSecondaryParam(parameter)[0]),
406+
arguments = (ActionChecker.GetSecondaryParam(parameter).Length > 1 ? ActionChecker.GetSecondaryParam(parameter)[1] : null),
407+
fileLocation = (!location.Contains(@":\") || !location.Contains(@":/")) ? "" : location;
408+
407409
if (fileLocation == "") {
408410
string combinedPath = "";
409411
try {
@@ -448,7 +450,7 @@ public void Open(string parameter) {
448450
}
449451
public void OpenAny(string parameter) {
450452
string fileLocation = MainProgram.shortcutLocation;
451-
Regex rx = new Regex("^" + parameter + Regex.Escape(".") + ".*", RegexOptions.IgnoreCase);
453+
Regex rx = new Regex("^" + ActionChecker.CheckForEnvironmentPath(parameter) + Regex.Escape(".") + ".*", RegexOptions.IgnoreCase);
452454

453455
if (Directory.Exists(fileLocation) || Uri.IsWellFormedUriString(fileLocation, UriKind.Absolute)) {
454456
DirectoryInfo d = new DirectoryInfo(fileLocation);
@@ -478,7 +480,7 @@ public void OpenAny(string parameter) {
478480
}
479481
}
480482
public void OpenAll(string parameter) {
481-
string fileLocation = (!parameter.Contains(@":\") || !parameter.Contains(@":/")) ? Path.Combine(MainProgram.shortcutLocation, parameter) : parameter;
483+
string fileLocation = ActionChecker.CheckForEnvironmentPath((!parameter.Contains(@":\") || !parameter.Contains(@":/")) ? Path.Combine(MainProgram.shortcutLocation, parameter) : parameter);
482484

483485
if (Directory.Exists(fileLocation) || Uri.IsWellFormedUriString(fileLocation, UriKind.Absolute)) {
484486
DirectoryInfo d = new DirectoryInfo(fileLocation);

AssistantComputerControl/actionChecker.cs

Lines changed: 41 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* AssistantComputerControl
33
* Made by Albert MN.
4-
* Updated: v1.4.4, 19-05-2021
4+
* Updated: v1.4.6, 17-01-2022
55
*
66
* Use:
77
* - Checks and execute action files
@@ -397,71 +397,48 @@ private static void CheckAction(string theLine, string theFile) {
397397
}
398398
}
399399

400-
private static string GetPathFolder(string winEnvPathVar)
401-
/* Get windows environment path as string */
402-
{
403-
string folderPath = "";
404-
switch (winEnvPathVar)
405-
{
406-
case "%USERPROFILE%":
407-
return Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
408-
409-
case "%DESKTOP%":
410-
return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
411-
412-
case "%PROGRAMS%":
413-
return Environment.GetFolderPath(Environment.SpecialFolder.Programs);
414-
415-
case "%DOCUMENTS%":
416-
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
417-
418-
case "%MUSIC%":
419-
return Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
420-
421-
case "%VIDEOS%":
422-
return Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
423-
424-
case "%IMAGES%":
425-
return Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
426-
427-
case "%COMPUTER%":
428-
return Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
429-
430-
case "%FAVORITES%":
431-
return Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
432-
433-
case "%COOKIES%":
434-
return Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
435-
436-
case "%FONTS%":
437-
return Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
438-
439-
case "%HISTORY%":
440-
return Environment.GetFolderPath(Environment.SpecialFolder.History);
441-
442-
case "%PERSONAL%":
443-
return Environment.GetFolderPath(Environment.SpecialFolder.Personal);
444-
445-
case "%VIRTUALNETWORK%":
446-
return Environment.GetFolderPath(Environment.SpecialFolder.NetworkShortcuts);
447-
448-
case "%VIRTUALPRINT%":
449-
return Environment.GetFolderPath(Environment.SpecialFolder.PrinterShortcuts);
450-
451-
case "%APPDATA%":
452-
return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
453-
454-
case "%PROGRAMFILES%":
455-
return Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
456-
457-
case "%DIRSYSTEM%":
458-
return Environment.GetFolderPath(Environment.SpecialFolder.System);
459-
460-
case "%WINDOWS%":
461-
return Environment.GetFolderPath(Environment.SpecialFolder.Windows);
400+
private static string CheckEnvironmentVar(string var) {
401+
try {
402+
return Environment.GetFolderPath((Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), var));
403+
} catch { }
404+
return null;
405+
}
462406

407+
/* Get Windows environment path from string */
408+
public static string CheckForEnvironmentPath(string str) {
409+
Dictionary<string, string> envVariables = new Dictionary<string, string>() {
410+
{ "%USERPROFILE%", "UserProfile" },
411+
{ "%DESKTOP%", "Desktop" },
412+
{ "%PROGRAMS%", "Programs" },
413+
{ "%DOCUMENTS%", "MyDocuments" },
414+
{ "%MUSIC%", "MyMusic" },
415+
{ "%VIDEOS%", "MyVideos" },
416+
{ "%IMAGES%", "MyPictures" },
417+
{ "%COMPUTER%", "MyComputer" },
418+
{ "%FAVORITES%", "Favorites" },
419+
{ "%COOKIES%", "Cookies" },
420+
{ "%FONTS%", "Fonts" },
421+
{ "%HISTORY%", "History" },
422+
{ "%PERSONAL%", "Personal" },
423+
{ "%VIRTUALNETWORK%", "NetworkShortcuts" },
424+
{ "%VIRTUALPRINT%", "PrinterShortcuts" },
425+
{ "%APPDATA%", "ApplicationData" },
426+
{ "%PROGRAMFILES%", "ProgramFiles" },
427+
{ "%DIRSYSTEM%", "System" },
428+
{ "%WINDOWS%", "Windows" },
429+
};
430+
431+
foreach (KeyValuePair<string, string> var in envVariables) {
432+
if (str.Contains(var.Key) || str.Contains(var.Key.ToLower())) {
433+
string parsed = CheckEnvironmentVar(var.Value);
434+
if (parsed != null) {
435+
str = str.Replace(var.Key, parsed);
436+
str = str.Replace(var.Key.ToLower(), parsed);
437+
}
438+
}
463439
}
464-
return folderPath;
440+
441+
return str;
465442
}
466443

467444
public static string[] GetSecondaryParam(string param) {
@@ -474,23 +451,6 @@ public static string[] GetSecondaryParam(string param) {
474451
}
475452
return toReturn;
476453
}
477-
// check for any environment variables that starts with %
478-
if (param.StartsWith("%"))
479-
{
480-
// environment variables between two characters (e.g. %ENV%)
481-
if (Regex.Matches(param, "%").Count == 2)
482-
{
483-
// a subfolder or file was specified
484-
if (!param.EndsWith("%"))
485-
{
486-
var rawPath = param.Split(Convert.ToChar("%"));
487-
var finalPath = GetPathFolder("%" + rawPath[1] + "%") + rawPath[2].Replace("/", "\\");
488-
return new[] {finalPath};
489-
}
490-
// else, translate only %ENV%
491-
return new[] {GetPathFolder(param)};
492-
}
493-
}
494454

495455
return new string[1] { param };
496456
}

0 commit comments

Comments
 (0)