Author Topic: Opening a program, moving to a certain line, deleting text, and adding new text?  (Read 3348 times)

VirdsEyeBiew

  • Guest
Is that even possible? For example:
1: Opening a notepad document with coding in it
2: Moving over to a line of code
3: Deleting a certain amount of characters
4: Typing in new characters to replace it
5: Saving the notepad to put the new command into effect

I'm just now starting out with VA and trying to get used to it however I'd love to know if this is possible or not, and if not, is there a workaround? Thank you!

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
Can you provide a concrete example?

As with any problem, there are a number of solutions, but the most suitable solution depends on the exact parameters of your problem.

VirdsEyeBiew

  • Guest
So for example, I am trying to turn some line from TRUE to FALSE and vice versa when I dictate what I want. I.E:

"Show my view"

Opens file browser, goes to document (Notepad) and you see these lines -

[thirdPerson=True
fov=90]

Changes 'thirdPerson=True'
to 'thirdPerson=False' -

Saves edit and closes document (Notepad)

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
So you have to look at the file first? Or could this just happen in the background, without the file visibly opening?

VirdsEyeBiew

  • Guest
So you have to look at the file first? Or could this just happen in the background, without the file visibly opening?

I'd like it to open in the background without me actually having to do it myself. The words "show my view" would open the document, change the code, save it, and exit out of the document.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
This will do it in the background:
Code: [Select]
Set Text [~file] to [C:\VAtest.txt]
Set Text [~find] to 'thirdPerson=True'
Set Text [~replace] to 'thirdPerson=False'
Inline C# Function: Find and replace, wait until execution finishes
Begin Text Compare : [{TXT:~output}] Does Not Equal 'Not Set'
    Write (overwrite), '{TXT:~output}' to file 'C:\VAtest.txt'
End Condition

Where "Find and replace" contains
Code: [Select]
using System;
using System.Text.RegularExpressions;

public class VAInline
{
public void main()
{
string text = VA.GetText("~file");
string find = VA.GetText("~find");
string replace = VA.GetText("~replace");

if (text.IndexOf(find, StringComparison.OrdinalIgnoreCase) >= 0)
{
VA.SetText("~output", Regex.Replace(text, Regex.Escape(find), replace.Replace("$","$$"), RegexOptions.IgnoreCase));
VA.WriteToLog("Text found and replaced", "green");
}
else
{
VA.WriteToLog("Text not found", "red");
}
}
}


This will attempt to do it in the foreground:
Code: [Select]
Set Text [~file] to 'C:\VAtest.txt'
Run application 'notepad' -with parameters '"{TXT:~file}"'
Set Text [~windowTitle] to 'Notepad*'
Start Loop While : [{WINDOWEXISTS:~windowTitle}] Equals '0'
End Loop
Set Text [~find] to 'thirdPerson=True'
Set Text [~replace] to 'thirdPerson=False'
Start Loop While : [1] Equals [1]
    Press Home key and hold for 0,06 seconds and release
    Press Left Shift+End keys and hold for 0,06 seconds and release
    Press Left Ctrl+C keys and hold for 0,06 seconds and release
    Inline C# Function: Find and replace, wait until execution finishes
    Begin Boolean Compare : [~stopSearch] Equals True
        Begin Text Compare : [{TXT:~output}] Does Not Equal 'Not Set'
            Set Windows clipboard to '{TXT:~output}'
            Press Left Ctrl+V keys and hold for 0,06 seconds and release
            Press Left Ctrl+S keys and hold for 0,06 seconds and release
            Press Left Alt+F4 keys and hold for 0,06 seconds and release
        End Condition
        Loop Break
    End Condition
    Press Down key and hold for 0,06 seconds and release
End Loop

Where "Find and replace" contains
Code: [Select]
using System;
using System.Text.RegularExpressions;

public class VAInline
{
public void main()
{
string text = VA.ParseTokens("{CLIP}");
string find = VA.GetText("~find");
string replace = VA.GetText("~replace");
string lastText = VA.GetText("~lastText");

if (text == lastText && text != "") //If current line is same as last, assume end of file
{
VA.WriteToLog("Text not found", "red");
VA.SetBoolean("~stopSearch", true);
}
else if (text.IndexOf(find, StringComparison.OrdinalIgnoreCase) >= 0)
{
VA.SetText("~output", Regex.Replace(text, Regex.Escape(find), replace.Replace("$","$$"), RegexOptions.IgnoreCase));
VA.SetBoolean("~stopSearch", true);
}
else
{
VA.SetText("~lastText", text);
}
}
}

I don't know what utility the latter would be, but it works, to a point; As there is no native way to detect the end of the file, it'll stop if it reads the same text twice.
There is an exception made for empty lines, which will be an issue if your file ends with one.

VirdsEyeBiew

  • Guest
This will do it in the background:
Code: [Select]
Set Text [~file] to [C:\VAtest.txt]
Set Text [~find] to 'thirdPerson=True'
Set Text [~replace] to 'thirdPerson=False'
Inline C# Function: Find and replace, wait until execution finishes
Begin Text Compare : [{TXT:~output}] Does Not Equal 'Not Set'
    Write (overwrite), '{TXT:~output}' to file 'C:\VAtest.txt'
End Condition

Where "Find and replace" contains
Code: [Select]
using System;
using System.Text.RegularExpressions;

public class VAInline
{
public void main()
{
string text = VA.GetText("~file");
string find = VA.GetText("~find");
string replace = VA.GetText("~replace");

if (text.IndexOf(find, StringComparison.OrdinalIgnoreCase) >= 0)
{
VA.SetText("~output", Regex.Replace(text, Regex.Escape(find), replace.Replace("$","$$"), RegexOptions.IgnoreCase));
VA.WriteToLog("Text found and replaced", "green");
}
else
{
VA.WriteToLog("Text not found", "red");
}
}
}


This will attempt to do it in the foreground:
Code: [Select]
Set Text [~file] to 'C:\VAtest.txt'
Run application 'notepad' -with parameters '"{TXT:~file}"'
Set Text [~windowTitle] to 'Notepad*'
Start Loop While : [{WINDOWEXISTS:~windowTitle}] Equals '0'
End Loop
Set Text [~find] to 'thirdPerson=True'
Set Text [~replace] to 'thirdPerson=False'
Start Loop While : [1] Equals [1]
    Press Home key and hold for 0,06 seconds and release
    Press Left Shift+End keys and hold for 0,06 seconds and release
    Press Left Ctrl+C keys and hold for 0,06 seconds and release
    Inline C# Function: Find and replace, wait until execution finishes
    Begin Boolean Compare : [~stopSearch] Equals True
        Begin Text Compare : [{TXT:~output}] Does Not Equal 'Not Set'
            Set Windows clipboard to '{TXT:~output}'
            Press Left Ctrl+V keys and hold for 0,06 seconds and release
            Press Left Ctrl+S keys and hold for 0,06 seconds and release
            Press Left Alt+F4 keys and hold for 0,06 seconds and release
        End Condition
        Loop Break
    End Condition
    Press Down key and hold for 0,06 seconds and release
End Loop

Where "Find and replace" contains
Code: [Select]
using System;
using System.Text.RegularExpressions;

public class VAInline
{
public void main()
{
string text = VA.ParseTokens("{CLIP}");
string find = VA.GetText("~find");
string replace = VA.GetText("~replace");
string lastText = VA.GetText("~lastText");

if (text == lastText && text != "") //If current line is same as last, assume end of file
{
VA.WriteToLog("Text not found", "red");
VA.SetBoolean("~stopSearch", true);
}
else if (text.IndexOf(find, StringComparison.OrdinalIgnoreCase) >= 0)
{
VA.SetText("~output", Regex.Replace(text, Regex.Escape(find), replace.Replace("$","$$"), RegexOptions.IgnoreCase));
VA.SetBoolean("~stopSearch", true);
}
else
{
VA.SetText("~lastText", text);
}
}
}

I don't know what utility the latter would be, but it works, to a point; As there is no native way to detect the end of the file, it'll stop if it reads the same text twice.
There is an exception made for empty lines, which will be an issue if your file ends with one.

Dude thank you so much. Now, not to sound like a noob (Probably cause I really am lol), but how would I go about applying that to mine? Is there a way you could make the file for me and then just send it so I can import it over to mine? If not, just a quick explanation of what to do? I'm sure I just copy and paste it somewhere

iceblast

  • Sr. Member
  • ****
  • Posts: 372
I hope everyone doesn't mind me saying something....

I would do this a different way, if what you're doing is only changing 1 line, or so.

Instead you could just rewrite the entire txt file everytime, and just use variables for the settings/lines that you want to change. Here's a example of what I mean. It would be easy to put a bunch of different variables in it, and change them as needed, using different commands.

Say, this is only what's in the txt file you want changed.

[thirdPerson=True
fov=90]

Replace True with the variable. Now, everytime you run the command it will just rewrite the txt file, with either True or False in the proper place.

[thirdPerson={BOOL:Setting}
fov=90]

Code: [Select]
Begin Boolean Compare : [Setting] Has Not Been Set
    Set Boolean [Setting] to True
End Condition
Set Boolean [Setting] to Toggle
Write (overwrite), '[thirdPerson={BOOL:Setting}
fov=90]' to file 'test.txt'
Write [Blue] '{BOOL:Setting}' to log

Well, I hope that helps in some way.

VirdsEyeBiew

  • Guest
I hope everyone doesn't mind me saying something....

I would do this a different way, if what you're doing is only changing 1 line, or so.

Instead you could just rewrite the entire txt file everytime, and just use variables for the settings/lines that you want to change. Here's a example of what I mean. It would be easy to put a bunch of different variables in it, and change them as needed, using different commands.

Say, this is only what's in the txt file you want changed.

[thirdPerson=True
fov=90]

Replace True with the variable. Now, everytime you run the command it will just rewrite the txt file, with either True or False in the proper place.

[thirdPerson={BOOL:Setting}
fov=90]

Code: [Select]
Begin Boolean Compare : [Setting] Has Not Been Set
    Set Boolean [Setting] to True
End Condition
Set Boolean [Setting] to Toggle
Write (overwrite), '[thirdPerson={BOOL:Setting}
fov=90]' to file 'test.txt'
Write [Blue] '{BOOL:Setting}' to log

Well, I hope that helps in some way.

Is there any way you can help? I'm an utter noob at this and have no idea how to place the coding or what to do. Only thing I've been able to do is setup the keystrokes lol

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
Click
Click "New Command"
In the "When I say" field, enter a name for the command(of your own choosing)

Click "Other >", "Advanced", "Set a Text Value"
In the "Variable Name" field, enter "~file"(see VoiceAttackHelp.pdf page 177 and 178 for info on variable prefixes)
Click the radio button next to "Value from file/URI"
Click "..."
Browse to the config file you want to change(or a test copy of it, in case something goes wrong)
Click "Open"
Click "OK"

Click "Other >", "Advanced ⯈", "Set a Text Value"
In the "Variable Name" field, enter "~find"
In the "Text" field, enter the text you want to replace within the target file
Click "OK"

Click "Other >", "Advanced ⯈", "Set a Text Value"
In the "Variable Name" field, enter "~replace"
In the "Text" field, enter the text you want to insert into the target file
Click "OK"

Click "Other >", "Advanced ⯈", "Execute an Inline Function ⯈", "C# Code"
Copy this:
Code: [Select]
using System;
using System.Text.RegularExpressions;

public class VAInline
{
public void main()
{
string text = VA.GetText("~file");
string find = VA.GetText("~find");
string replace = VA.GetText("~replace");

if (text.IndexOf(find, StringComparison.OrdinalIgnoreCase) >= 0)
{
VA.SetText("~output", Regex.Replace(text, Regex.Escape(find), replace.Replace("$","$$"), RegexOptions.IgnoreCase));
VA.WriteToLog("Text found and replaced", "green");
}
else
{
VA.WriteToLog("Text not found", "red");
}
}
}
And paste over the default 27 lines(Ctrl-A, Ctrl-V)
Click "Compile", aftert a short delay you should see "Compile succesful" if you pasted correctly
Check "Wait for the inline function to finish before continuing"
In the "Description" field, enter "Find and replace"(this is optional, but helps when editing commands later on)
Click "OK"

Click "Other >", "Advanced ⯈", "Begin a Conditional(If Statement) Block ⯈", "Single Condition"
Click the "Text" tab
In the "Variable Name / Token" field, enter "~output"(This is different from the original example; Either will work, but the token is redundant in this case, force of habit)
In the dropdown, select "Has Been Set"
Click "OK"

Click "Other >", "Advanced ⯈", "Write Text to a File"
In the "Text" field, enter "{TXT:~output}"
Click "..."
Browse to the config file you want to change(or the test copy you selected previously)
Click "Open"
Check "Overwrite file if it already exists"
Click "OK"

You should now have this in your action list(file paths may differ):
Code: [Select]
Set Text [~file] to [C:\VAtest.txt]
Set Text [~find] to 'thirdPerson=True'
Set Text [~replace] to 'thirdPerson=False'
Inline C# Function: Find and replace, wait until execution finishes
Begin Text Compare : [~output] Has Been Set
    Write (overwrite), '{TXT:~output}' to file 'C:\VAtest.txt'
End Condition

Click "OK"
Click "Apply" or "Done", then test the command.

VirdsEyeBiew

  • Guest
Click
Click "New Command"
In the "When I say" field, enter a name for the command(of your own choosing)

Click "Other >", "Advanced", "Set a Text Value"
In the "Variable Name" field, enter "~file"(see VoiceAttackHelp.pdf page 177 and 178 for info on variable prefixes)
Click the radio button next to "Value from file/URI"
Click "..."
Browse to the config file you want to change(or a test copy of it, in case something goes wrong)
Click "Open"
Click "OK"

Click "Other >", "Advanced ⯈", "Set a Text Value"
In the "Variable Name" field, enter "~find"
In the "Text" field, enter the text you want to replace within the target file
Click "OK"

Click "Other >", "Advanced ⯈", "Set a Text Value"
In the "Variable Name" field, enter "~replace"
In the "Text" field, enter the text you want to insert into the target file
Click "OK"

Click "Other >", "Advanced ⯈", "Execute an Inline Function ⯈", "C# Code"
Copy this:
Code: [Select]
using System;
using System.Text.RegularExpressions;

public class VAInline
{
public void main()
{
string text = VA.GetText("~file");
string find = VA.GetText("~find");
string replace = VA.GetText("~replace");

if (text.IndexOf(find, StringComparison.OrdinalIgnoreCase) >= 0)
{
VA.SetText("~output", Regex.Replace(text, Regex.Escape(find), replace.Replace("$","$$"), RegexOptions.IgnoreCase));
VA.WriteToLog("Text found and replaced", "green");
}
else
{
VA.WriteToLog("Text not found", "red");
}
}
}
And paste over the default 27 lines(Ctrl-A, Ctrl-V)
Click "Compile", aftert a short delay you should see "Compile succesful" if you pasted correctly
Check "Wait for the inline function to finish before continuing"
In the "Description" field, enter "Find and replace"(this is optional, but helps when editing commands later on)
Click "OK"

Click "Other >", "Advanced ⯈", "Begin a Conditional(If Statement) Block ⯈", "Single Condition"
Click the "Text" tab
In the "Variable Name / Token" field, enter "~output"(This is different from the original example; Either will work, but the token is redundant in this case, force of habit)
In the dropdown, select "Has Been Set"
Click "OK"

Click "Other >", "Advanced ⯈", "Write Text to a File"
In the "Text" field, enter "{TXT:~output}"
Click "..."
Browse to the config file you want to change(or the test copy you selected previously)
Click "Open"
Check "Overwrite file if it already exists"
Click "OK"

You should now have this in your action list(file paths may differ):
Code: [Select]
Set Text [~file] to [C:\VAtest.txt]
Set Text [~find] to 'thirdPerson=True'
Set Text [~replace] to 'thirdPerson=False'
Inline C# Function: Find and replace, wait until execution finishes
Begin Text Compare : [~output] Has Been Set
    Write (overwrite), '{TXT:~output}' to file 'C:\VAtest.txt'
End Condition

Click "OK"
Click "Apply" or "Done", then test the command.

I love you so much oh my god. It works perfectly. I did mess it up a few times while doing it but I managed to fix it. Thank you a million!!!!