everyone. I hope you are all having a great day.
I am currently trying to implement a TexStudio script that obtains the name of an image I obtained from snipping tool(I copying it automatically saves it in a "screenshots" folder), in combination with Window's CMD and TexStudio's Javascript feature(In macro settings)
- "Snip"/Capture screen using Snipping Tool
- Copy picture(now saved on screenshots folder with path of C:\Users\Dir1\Dir2\Dir3\Dir4\Pictures\Screenshots)
- Create the batch file script used to obtain the file name of screenshot. In this case, the script is stored in C:\Users\Dir5\Dir6\Dir7\scripts\actualScript.bat
- Use TexStudio's
systemfunction to use an external command(from CMD), the external command being actualScript.bat
Now, here is what the actualScript.bat looks like:
@echo off
FOR /F "eol=| delims=" %%I IN ('DIR "C:\Users\Dir1\Dir2\Dir3\Dir4\Pictures\Screenshots" /A-D /B /O-D /TW 2^>nul') DO (
SET NewestFile=%%I
GOTO FoundFile
)
ECHO No *.png file found!
GOTO :EOF
:FoundFile
echo %NewestFile%
Essentially, this code will find the first file in directory where the screenshots are and get the name of that first file. After that, the loop will stop and the name of the file will be stored in NewestFile(some error checking if no .png file is found). I have tested this code in CMD and it works perfectly in the CMD.
And here is the JavaScript Script in TexStudio:
%SCRIPT
var snipPath = "C:\Users\Dir1\Dir2\Dir3\Dir4\Pictures\Screenshots";
cmd = system("C:\Users\Dir5\Dir6\Dir7\scripts\actualScript.bat");
cmd.waitForFinished();
var output = cmd.readAllStandardOutputStr();
cmd.terminate();
editor.write(snipPath + "\\\\" + output);
This is what the output should look like after triggering the macro/script by typing //z(trigger) into the editor:
C:\Users\Dir1\Dir2\Dir3\Dir4\Pictures\Screenshots\Screenshot 2024-03-09 020741.png
Instead, the output looks like this:
C:\Users\Dir1\Dir2\Dir3\Dir4\Pictures\Screenshots\
In the messages box, I get the following error: Error Message on CMD external command when trying to execute Note: The reason I use \ instead of \ in the paths is because \ itself in the script of TexStudio is an won't be visible as output(peep the screenshot of 3 red cross out lines). That is also a problem I don't know how to solve: Use \ or \ in file paths?
As I said earlier, I the batch script works perfectly in CMD. So I believe the error is coming from how I am implementing the batch script in the JavaScript script on TexStudio. I am just not sure what the error is or where it comes from.
Here are the sources for the functions and manual used in scripting and macros of TexStudio:
- https://texstudio-org.github.io/advanced.html# "Script Macros" Section
- https://texstudio-org.github.io/advanced.html# "Command syntax in detail" Section. Note: I am not very certain as if I need to use txs/// or not(expansion or not).
I have also thought of trying to implement something equivalent from this thread but for Batch/Windows:
In summary I am aiming for capture screen(snipping tool), copy image(saved on screenshots), get file name from that image, script pastes name of image in text editor through some script/macro. The rest would be implementing that name pasting with an includegraphics but that is the much, much easier part of this process.
Another note, I have no prior knowledge to JavaScript at all and little on CMD. I just fed off various StackOverflow threads and some other youtube videos. Any help is very much appreciated.