StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Yuichi  
#1 Posted : Saturday, June 15, 2019 10:40:13 PM(UTC)
Yuichi

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 9/13/2018(UTC)
Posts: 54
Poland

Thanks: 18 times
Was thanked: 18 time(s) in 13 post(s)
Hi
I wrote this code for applications that should not be closed or should not be closed by accident.Blushing
Nothing special, but it's something.

Plugin dll for new window:
CloseWindowExceptionFormLibrary
Unlock dll before start.
Thanks to Rob i can finally create plugins.BigGrin

The exceptions are stored in the txt file. When the file is created, please convert it to "UTF-8", characters other than English may not be recognized.
You can choose which parameters will be stored, such as Exe name, Title and Class name.
But be careful what you put in the file. The less data you provide, the more applications may not be closed.
For example, if you only save Exe name like "explorer.exe" then all applications from the explorer will not be closed.

Remember, an empty string is still a value for correctly recognizing windows.


Close App (main):


Code:
var wnd = sp.WindowFromPoint(sp.GetCurrentMousePoint(), true);
var lines = File.ReadAllLines("C:\\S+Net\\AppCloseExceptionInfo.txt"); // file for exceptions
var t_tmp = []; // for names in line (file)
var s_noName = "nameNotUsed"; // if you uncheck/disable a name (Add Exception), "s_noName" will replace that name from the line
var s_exe = s_noName;
var s_title = s_noName;
var s_class = s_noName;
var b_exe = false;
var b_title = false;
var b_class = false;
var b_found = false;


// COMPARING
for (i = 0; i < lines.Length; i++) {
    t_tmp = lines[i].split(";");

    if (lines[i].trim()) { // do not work with an empty line

         // SEPARATE NAMES FROM LINE/FILE
        for (j = 0; j < t_tmp.length; j++) {
            if (/E\*/.exec(t_tmp[j])) s_exe = t_tmp[j].slice(2,t_tmp[j].length).trim();
            if (/T\*/.exec(t_tmp[j])) s_title = t_tmp[j].slice(2,t_tmp[j].length).trim();
            if (/C\*/.exec(t_tmp[j])) s_class = t_tmp[j].slice(2,t_tmp[j].length).trim();
        }

        // CHECK WHICH NAMES MATCH
        if (s_exe != s_noName) { // exe / if name is enabled
            // I'm not use "Process.MainModule.ModuleName" because can create problem like this: "unable to enumerate the process modules"
            if (s_exe == wnd.Process.ProcessName+".exe") b_exe = true; // check if the current name is in line
        } else { // if name is disabled let "b_exe" to set as "true" by any app
            b_exe = true;
        }

        if (s_title != s_noName) { // title
            if (s_title == wnd.Title) b_title = true;
        } else {
            b_title = true;
        }

        if (s_class != s_noName) { // class
            var error = null;
            try { // catch any error
                wnd.ClassName;
            } catch(err) {
                error = err.message;
                b_class = true; // if any error appear, set as true
            }
            if (!error) if (s_class == wnd.ClassName) b_class = true;
        } else {
            b_class = true;
        }

        // FOUND APP
        if (b_exe && b_title && b_class)  { // all bool's must be "true" to correct recognize app
            b_found = true;
            break;
        }

        // RESET
        s_exe = s_noName; s_title = s_noName; s_class = s_noName;
        b_exe = false; b_title = false; b_class = false;
    }
}


// CLOSE APP
if (!b_found) {
    wnd.SendClose();
}


Close App Add Exception To File:

Code:
var wnd = sp.WindowFromPoint(sp.GetCurrentMousePoint(), true);
var f_path = "C:\\S+Net\\AppCloseExceptionInfo.txt"; // file for exceptions
var s_newLine = ""; // names to save
var s_displayMsg = ""; // str to display in "displayText" function
var b_found = false;

var s_exe = wnd.Process.ProcessName+".exe";
var s_title = wnd.Title;
var s_class = "";
var error = null;
try {
    wnd.ClassName;
} catch(err) {
    error = err.message;
}
if (!error) s_class = wnd.ClassName;

// SHOW DIALOG BOX
var form = new CloseWindowExceptionLibrary().GetForm();
form.Text = "Close Window Exception";
form.label1.Text = "Exe:";
form.label2.Text = "Title:";
form.label3.Text = "Class:";
form.groupBox1.Text = "Choose parameters to save:";
form.button1.Text = "Ok";
form.button2.Text = "Cancel";
form.checkBox1.Text = s_exe;
form.checkBox2.Text = s_title;
form.checkBox3.Text = s_class;
var result = form.ShowDialog();

if(result.ToString() == "OK") {

    // CONCAT NAMES
    // "E*" = exe name, "T*" = title, "C*" = class name
    if (form.checkBox1.Checked) { // exe
        s_newLine = s_newLine + "E*" + s_exe + ";";
        s_displayMsg = s_displayMsg + "Exe:    " + s_exe;
    }

    if (form.checkBox2.Checked) { // title
        s_newLine = s_newLine + "T*" + s_title + ";";
        if (s_displayMsg) s_displayMsg = s_displayMsg + "\n"; // add new line
        s_displayMsg = s_displayMsg + "Title:  " + s_title;
    }

    if (form.checkBox3.Checked) { // class
        s_newLine = s_newLine + "C*" + s_class + ";";
        if (s_displayMsg) s_displayMsg = s_displayMsg + "\n"; // add new line
        s_displayMsg = s_displayMsg + "Class: "+s_class;
    }

    // SEARCH FOR THE SAME APP
    if (!File.Exists(f_path)) File.WriteAllText(f_path, ""); // create a file if not exists
    var lines = File.ReadAllLines(f_path);
    for (i = 0; i < lines.Length; i++) {
        if (s_newLine == lines[i].trim()) {
            b_found = true;
            break;
        }
    }

    // ADD LINE TO FILE
    if (!b_found) {
        var f = File.AppendText(f_path);
        f.WriteLine(s_newLine);
        f.Close();
        displayText("An exception has been added:", s_displayMsg, "green");
    } else {
        displayText("This exception already exists:", s_displayMsg, "red");
    }

} // clicked ok

// DISPLAY TEXT
function displayText(title, msg, cl) {
    var info = new DisplayTextInfo();
    info.Title = title;
    info.TitleAlignment = "Center";
    info.Message = msg;
    info.MessageAlignment = "Left";
    info.Duration = 7000;
    info.Opacity = 0.90;
    info.Location = "top";
    info.TitleFont = new Font("Segoe UI", 12, host.flags(FontStyle.Bold));
    info.MessageFont = new Font("Segoe UI Semibold", 12);
    info.BackColor = cl;
    info.ForeColor = "white";
    info.Padding = 15;
    info.UsePrimaryScreen = true;
    sp.DisplayText(info);
}


Close App Remove Exception From File (by simply run the file):

Code:
sp.RunProgram("C:\\Windows\\notepad.exe", "C:\\S+Net\\AppCloseExceptionInfo.txt", "", "", false, true, false);

Edited by user Monday, June 17, 2019 9:07:03 PM(UTC)  | Reason: An updated dll file, if all items are unchecked, the OK button will be disabled and "Close App

Rob  
#2 Posted : Sunday, June 16, 2019 4:42:28 AM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Alright! Someone else is finally dipping their toes into the plug-ins :)

Also FYI, you could add these in the plug-in form, based on the button they click:

Code:
this.DialogResult = DialogResult.OK;
//or
this.DialogResult = DialogResult.Cancel;


Then in the script, have a different action based on the response:

Code:
var result = form.ShowDialog();
if(result.ToString() == "Cancel") {
    sp.MessageBox("You clicked Cancel", "Cancel");
} else if(result.ToString() == "OK") {
    sp.MessageBox("You clicked OK", "Ok!");
}


thanks 1 user thanked Rob for this useful post.
Yuichi on 6/16/2019(UTC)
Yuichi  
#3 Posted : Tuesday, June 18, 2019 7:16:48 AM(UTC)
Yuichi

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 9/13/2018(UTC)
Posts: 54
Poland

Thanks: 18 times
Was thanked: 18 time(s) in 13 post(s)
Message for edit reason is too short.
All message: "An updated dll file, if all items are unchecked, the OK button will be disabled and "Close App Add Exception To File" is updated too. (Rob advice)"
Rob  
#4 Posted : Tuesday, June 18, 2019 1:07:17 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Yeah, it's only meant for a short note. I would say make your longer notes at the end of the post itself.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.