Ejemplo n.º 1
0
Archivo: Run.cs Proyecto: alexfordc/Au
 /// <summary>
 /// Starts task process.
 /// Returns (processId, processHandle). Throws if failed.
 /// </summary>
 static (int pid, WaitHandle hProcess) _StartProcess(_SpUac uac, string exeFile, string args, string wrPipeName)
 {
     if (wrPipeName != null)
     {
         wrPipeName = "ATask.WriteResult.pipe=" + wrPipeName;
     }
     if (uac == _SpUac.admin)
     {
         if (wrPipeName != null)
         {
             throw new AuException($"*start process '{exeFile}' as admin and enable ATask.WriteResult");                                //cannot pass environment variables. //rare //FUTURE
         }
         var k = AExec.Run(exeFile, args, RFlags.Admin | RFlags.NeedProcessHandle, "");
         return(k.ProcessId, k.ProcessHandle);
         //note: don't try to start task without UAC consent. It is not secure.
         //	Normally Au editor runs as admin in admin user account, and don't need to go through this.
     }
     else
     {
         var ps   = new Au.Util.ProcessStarter_(exeFile, args, "", envVar: wrPipeName, rawExe: true);
         var need = Au.Util.ProcessStarter_.Result.Need.WaitHandle;
         var psr  = uac == _SpUac.userFromAdmin ? ps.StartUserIL(need) : ps.Start(need, inheritUiaccess: uac == _SpUac.uiAccess);
         return(psr.pid, psr.waitHandle);
     }
 }
Ejemplo n.º 2
0
    void _Toolbar_Common1()
    {
        var t = new AToolbar("_Toolbar_Common1");

        if (!t.SettingsModified)
        {
            //first time set initial properties that are in the right-click menu. Later use saved values.
            t.AutoSize = true;
        }
        //set other properties
        t.BorderColor = System.Drawing.Color.Olive;

        //add buttons
        t["A"] = o => {  };
        t["B"] = o => {  };
        t.MenuButton("C", m => { //drop-down menu
            m["X"] = o => {  };
            m["Y"] = o => {  };
        });
        t.Group("Examples"); //horizontal separator, optionally with text
        t.NoText         = true;
        t["Run program"] = o => AExec.Run(AFolders.System + @"notepad.exe");
        t["Run script"]  = o => ATask.Run("Script example1.cs");
        t["Copy-paste"]  = o => {
            string s = AClipboard.Copy();     //note: to test it, at first select some text somewhere, else it will fail
            s = s.Upper();
            AClipboard.Paste(s);
        };

        bool autoHide = false; //or true

        if (autoHide)
        {
            //An "auto-hide" toolbar actually consists of 2 toolbars:
            //	t - toolbar with many buttons. Hidden when mouse pointer isn't in toolbar t2.
            //	t2 - small toolbar with zero or few buttons. It is the owner of toolbar t.
            var t2 = new AToolbar(t.Name + "^")
            {
                Satellite = t
            };
            t2.Show();
        }
        else
        {
            t.Show();
        }
    }
Ejemplo n.º 3
0
    void HotkeyTriggers()
    {
        var hk = Triggers.Hotkey;

        //examples of hotkey triggers

        hk["Ctrl+Alt+K"]     = o => AOutput.Write(o.Trigger); //it means: when I press Ctrl+Alt+K, execute action "AOutput.Write(o.Trigger)"
        hk["Ctrl+Shift+F11"] = o => {                         //action can have multiple statements
            var w1 = AWnd.FindOrRun("* Notepad", run: () => AExec.Run(AFolders.System + "notepad.exe"));
            AKeys.Text("text");
            500.ms();
            w1.Close();
        };
        hk["Ctrl+Shift+1"] = o => TriggerActionExample();          //action code can be in other function. To find it quickly, Ctrl+click the function name here.
        hk["Ctrl+Shift+2"] = o => TriggerActionExample2(o.Window); //the function can be in any class or partial file of this project folder
        hk["Ctrl+Shift+3"] = o => ATask.Run("Script example1.cs"); //run script in separate process. Then don't need to restart triggers when editing the script.

        //triggers that work only with some windows (when the window is active)

        Triggers.Of.Window("* WordPad", "WordPadClass");

        hk["Ctrl+F5"] = o => AOutput.Write("action 1", o.Trigger, o.Window);
        //hk[""] = o => {  };
        //...

        Triggers.Of.Windows(",,notepad.exe"); //all windows of notepad.exe process

        hk["Ctrl+F5"] = o => AOutput.Write("action 2", o.Trigger, o.Window);
        //hk[""] = o => {  };
        //...

        //...

        //disable/enable triggers
        Triggers.Of.AllWindows();
        hk["Ctrl+Alt+Win+D"]  = o => ActionTriggers.DisabledEverywhere ^= true;
        hk.Last.EnabledAlways = true;

        //To add triggers can be used snippets. Start typing "trig" and you will see snippets in the completion list.
        //For more info click word ActionTriggers above and press F1.
    }
Ejemplo n.º 4
0
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args)   //;;;
//Click the ► button on the toolbar to run the script.

                                 {
                                     AOutput.Write("The programming language is C#.");
                                     if (ADialog.ShowYesNo("Run Notepad?", "The script will add some text and close Notepad after 2 s."))
                                     {
                                         AExec.Run(AFolders.System + @"Notepad.exe");
                                         var w = AWnd.Wait(5, active: true, "*- Notepad"); //to create this code can be used the Code menu
                                         50.ms();
                                         AKeys.Text("some text");
                                         2.s();
                                         AKeys.Key("Ctrl+Z"); //Undo
                                         w.Close();
                                     }

                                     string linkText = "Read about code editor features",
                                            linkUrl  = "https://www.quickmacros.com/au/help/editor/Code editor.html";

                                     AOutput.Write($"<><link \"{linkUrl}\">{linkText}</link>");
                                 }
Ejemplo n.º 5
0
        /// <summary>
        /// Toolbar button MouseUp handler. Implements context menu that allows to customize.
        /// </summary>
        private void _OnMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {
                return;
            }
            var  item        = sender as ToolStripItem;
            var  ts          = item.Owner;
            var  dd          = ts as ToolStripDropDownMenu;
            bool isMenu      = dd != null;
            bool isCustom    = ts == _tsCustom1 || ts == _tsCustom2;
            bool isSeparator = item is ToolStripSeparator;
            bool isHidden    = item.Overflow == ToolStripItemOverflow.Always;
            var  x           = item.Tag as XElement;

            var m = new AMenu();

            m["Properties..."] = o => {
                using (var f = new FSMProperties(this, item.Tag as XElement, isMenu)) {
                    if (f.ShowDialog(_form) == DialogResult.OK)
                    {
                        _Strips_Customize(6, item, null, f);
                    }
                }
            };
            if (!isMenu)
            {
                if (!isSeparator)
                {
                    m["Hide"] = o => _Strips_Customize(5, item); if (isHidden)
                    {
                        m.LastMenuItem.Checked = true;
                    }
                }
                if (isCustom || isSeparator)
                {
                    m["Remove"] = o => _Strips_Customize(3, item);
                }
                if (!isHidden)
                {
                    m["Add separator"] = o => _Strips_Customize(4, item, ts);
                }
            }
            if (isCustom)
            {
                if (_tsCustom1 != null && _tsCustom2 != null)
                {
                    if (ts == _tsCustom1)
                    {
                        m["Move to toolbar Custom2"] = o => _Strips_Customize(2, item, _tsCustom2);
                    }
                    else
                    {
                        m["Move to toolbar Custom1"] = o => _Strips_Customize(2, item, _tsCustom1);
                    }
                }
            }
            else
            {
                if (_tsCustom1 != null)
                {
                    m["Copy to toolbar Custom1"] = o => _Strips_Customize(1, item, _tsCustom1);
                }
                if (_tsCustom2 != null)
                {
                    m["Copy to toolbar Custom2"] = o => _Strips_Customize(1, item, _tsCustom2);
                }
            }
            if (!isMenu)
            {
                m.Separator();
                m["How to customize..."] = o => ADialog.ShowInfo("Customizing toolbars and menus",
                                                                 @"There are several standard toolbars and two custom toolbars (initially empty). Standard toolbar buttons cannot be added and removed, but can be hidden and reordered. Menu items cannot be added, removed, hidden and reordered.

You can find most customization options in two context menus. Right-clicking a button or menu item shows its context menu. Right-clicking before the first button shows toolbar's context menu. You can Alt+drag toolbar buttons to reorder them on the same toolbar. You can Alt+drag toolbars to dock them somewhere else. Use splitters to resize. Right click a splitter to change its thickness."
                                                                 );
                string folder = APath.GetDirectoryPath(_xmlFileCustom), link = $"<a href=\"{folder}\">{folder}</a>";
                m["How to backup, restore, reset..."] = o => {
                    ADialog.Show("How to backup, restore or reset customizations",
                                 $@"All customizations are saved in XML files in folder
{link}

To backup:  copy the file.
To restore:  exit this application and replace the file with the backup file.
To reset:  exit this application and delete the file."
                                 , icon: DIcon.Info, onLinkClick: h => { AExec.Run(h.LinkHref); });
                };
            }

            m.Show(ts);
        }
Ejemplo n.º 6
0
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;

/*
The programming language is C#.

In scripts you can use classes/functions of the automation library provided by
this program, as well as of .NET Core and everything that can be used in C#.
Also you can create and use new functions, classes, libraries and .exe programs.

Script properties are saved in /*/ /*/ comments at the very start of script.
You can change them in the Properties dialog.

Like all C# programs, a script starts with standard code: using directives,
class and function Main where the program starts. Click the small [+] box at
the top-left to see and edit that code when need. The //. and //; are used to
fold (hide) code.

To avoid 'static' everywhere, function Main creates a class instance. Your script
code is in the constructor function. The function and the class end with } and }.

To run a script, you can click the ► Run button on the toolbar, or use command line,
or call ATask.Run from another scrit, or in Options set to run at startup.

Triggers such as hotkeys, autotext, mouse and window are used to execute functions
in a running script. Also you can create custom toolbars and menus. To start
using them: menu File -> New -> Examples -> @Triggers and toolbars.
*/

//Examples of automation functions.

AOutput.Write("Main script code.");

ADialog.Show("Message box.");

AExec.Run(AFolders.System + "notepad.exe");
var w = AWnd.Wait(0, true, "*- Notepad");
AKeys.Key("F5 Enter*2");
AKeys.Text(w.Name);
2.s();
w.Close();
var w2 = AWnd.Wait(-3, true, "Notepad", "#32770");
if(!w2.Is0) {
	500.ms();
	var c = +w2.Child(null, "Button", skip: 1); // "Don't Save"
	AMouse.Click(c);
	500.ms();
}

//Examples of .NET functions.

string s = "Example";
var b = new System.Text.StringBuilder();
for(int i = 0; i < s.Length; i++) {
	b.Append(s[i]).AppendLine();
}
MessageBox.Show(b.ToString());

//Example of your function and how functions can share variables.

_sharedVariable = 1;
FunctionExample("Example");
AOutput.Write(_sharedVariable);

} //end of main function