Posts: 9
Threads: 1
Joined: Oct 2024
11-01-2024, 10:26 PM
(This post was last modified: 11-01-2024, 10:55 PM by Rudeyz.)
- "Hi, I'm trying to learn about C#, and I have no basic knowledge about coding. I am using ChatGPT as a helper, and sorry for my broken English.
I am trying to make a basic script to run continuously with start, pause, and stop, and to move to points 1, 2, 3, 4, and 5, but somehow I am already stuck and can't open Notepad.
The code is like this.
// Clears the console for a fresh display of results
print.clear();
// Grouping mouse click coordinates
// Mouse coordinates in (x, y) format
var points = new[] {
(47, 845), // Point 1
(136, 845), // Point 2
(221, 845), // Point 3
(48, 784), // Point 4
(137, 784) // Point 5
};
// Function to open Notepad if it is not active
void OpenNotepad() {
var w = wnd.find(1, "*- Notepad", "Notepad");
if (w == null) {
// If Notepad is not found, open it and wait for it to be ready
run.it(@"C:\test folder\ThisLA.txt"); // Open file in the test folder
w = wait.until(2, () => wnd.find(1, "*- Notepad", "Notepad")); // Wait until Notepad opens
if (w == null) {
print.it("Error: Notepad window not found."); // Display error message if Notepad still not found
return; // Exit function if Notepad could not be found
}
}
w.Activate(); // Activate if already open
}
// Macro HeloA
void HeloA() {
OpenNotepad();
var w = wnd.find(1, "*- Notepad", "Notepad");
if (w == null) {
print.it("Failed to run HeloA: Notepad is not open.");
return;
}
// Click on points 1, 3, and 5
for (int i = 0; i < points.Length; i += 2) {
mouse.move(points[i].Item1, points[i].Item2); // Move mouse to specified point
mouse.click(); // Left-click the mouse
500.ms(); // Wait 500 ms
}
// Type text into Notepad
keys.sendt("Hello, this is script HeloA. Have a nice day!");
}
// Macro HeloB
void HeloB() {
OpenNotepad();
var w = wnd.find(1, "*- Notepad", "Notepad");
if (w == null) {
print.it("Failed to run HeloB: Notepad is not open.");
return;
}
// Click on points 2, 4, and 6
for (int i = 1; i < points.Length; i += 2) {
mouse.move(points[i].Item1, points[i].Item2);
mouse.click();
500.ms();
}
// Type text into Notepad
keys.sendt("Hello, this is script HeloB. Have a great day!");
}
// Run the main script
HeloA(); // Call Macro HeloA
HeloB(); // Call Macro HeloB
--- and itsaid eror cant open notepad
And I want each script to have its own void so I can run or test each code. Should I make it like a script or a class?"
https://prnt.sc/tHZwIxifhCuC
And thank you
Posts: 9
Threads: 1
Joined: Oct 2024
11-04-2024, 05:59 AM
(This post was last modified: 11-04-2024, 06:11 AM by Rudeyz.)
using System;
using Au;
public class Program {
public static void Main() {
HeloA.Function1(); // Calling HeloA
}
}
using Au;
public class HeloA {
public static void Function1() {
using (opt.scope.all()) { //recorded
opt.mouse.MoveSpeed = opt.key.KeySpeed = opt.key.TextSpeed = 10;
var w1 = wnd.find(0, "Untitled - Notepad", "Notepad").Activate();
var c1 = w1.Child(1, id: 15); // Edit "Text Editor"
keys.send("^Helo This is Macro class A");
}
}
}
test HeloA.cs(6,3): error CS0103: The name 'HeloA' does not exist in the current context
yeah ... ---
https://prnt.sc/dpkU8f9gn641
Posts: 12,090
Threads: 142
Joined: Dec 2002
#1
ChatGPT usually can't generate valid C# code that uses LibreAutomate functions. It isn't familiar enough with LibreAutomate. Use ChatGPT only to generate code without LibreAutomate functions.
#2
The easiest way to make it work, rename folder "Notepad" to "@Notepad". Then all files in that folder can use classes defined anywhere in that folder. And let "testA-" be the first file in that folder.
More info in Cookbook article "Multi-file scripts, projects".
Posts: 9
Threads: 1
Joined: Oct 2024
#2 can you screenshot how ?
Im to dumb ? i read on cookbook but still didnt understand, only understand use recording tools .
https://www.libreautomate.com/cookbook/r...jump).html
https://www.libreautomate.com/cookbook/f...rate).html
I saw it but where and how i should add into
There is any one make video about Liberate?
Posts: 12,090
Threads: 142
Joined: Dec 2002
I see in your screenshot the script file and file HeloA are in folder Notepad. Right-click the Notepad folder and select Rename. Make it @Notepad.
You can find many tutorials and videos about C# statements return, for etc on the internet. They are not specific to LibreAutomate.
Posts: 9
Threads: 1
Joined: Oct 2024
Little by little, I am getting used to it. As for why my trigger isn’t working, it’s because I’m still making the script in the editor. I need to either make it into an exe or press start in the editor first for the macro/script to run, right?
https://prnt.sc/1g--2epoGtEx
for infinity loop
for (;;) { //repeat forever or until break/return/goto/throw
500.ms(); //wait 500 ms
print.it(1);
if (keys.isCtrl) break; //exit the for block
if (keys.isShift) continue; //skip remaining statements
print.it(2);
}
for (int i = 0, j = 0; i < 5 && !keys.isShift; i++, j+=10) {
print.it(i, j);
500.ms();
}
i found in cook book inside the editor not from website .
Posts: 9
Threads: 1
Joined: Oct 2024
its normal ? for pause and quit script this long ? i made it with help chatgpt and finaly works T_T
using System;
using LibreAutomate;
public class Script
{
public static void Main()
{
bool isPaused = false; // Flag to check the pause status
int i = 1;
while (true)
{
// Wait for Ctrl+P to pause or resume
if (keys.isPressed(KKey.Ctrl) && keys.isPressed(KKey.P))
{
if (!isPaused) // If not paused, then pause
{
print.it("Script paused. Press Ctrl+P again to resume.");
isPaused = true; // Set pause status
}
else // If paused, then resume
{
print.it("Resuming script...");
isPaused = false; // Set resume status
}
// Wait until Ctrl+P is released to avoid spam
while (keys.isPressed(KKey.Ctrl) && keys.isPressed(KKey.P))
{
100.ms(); // Wait 100ms
}
}
// Wait for Ctrl+R to restart
if (keys.isPressed(KKey.Ctrl) && keys.isPressed(KKey.R))
{
print.it("Restarting from the beginning...");
i = 1; // Reset the number to 1
// Wait until Ctrl+R is released
while (keys.isPressed(KKey.Ctrl) && keys.isPressed(KKey.R))
{
100.ms(); // Wait 100ms
}
}
// Wait for Ctrl+Q to exit the script
if (keys.isPressed(KKey.Ctrl) && keys.isPressed(KKey.Q))
{
print.it("Exiting the script.");
break; // Exit the loop and stop the script
}
// If not paused, continue printing numbers
if (!isPaused)
{
print.it(i.ToString());
i++;
if (i > 20) i = 1; // Reset the number to 1 after reaching 20
}
500.ms(); // Wait 500ms before continuing
}
}
}
Posts: 12,090
Threads: 142
Joined: Dec 2002
12-11-2024, 10:07 AM
(This post was last modified: 12-11-2024, 10:19 AM by Gintaras.)
No. ChatGPT does not know it. Look in Cookbook.
example
script.setup(trayIcon: true, sleepExit: true, exitKey: KKey.MediaStop, pauseKey: KKey.MediaPlayPause);
for (int i = 0; i < 100; i++) {
script.pause();
print.it(i);
500.ms();
}
|