RyeStick Posted June 6, 2021 Share Posted June 6, 2021 Hello would someone be able to help me with my code. I am trying to write the clipboards contents to a text file. I have read the documentation but can't seem to egt it to work. $file1 = "C:\Users\Home\Desktop\Documents\test.txt" Local $Savetext = FileOpen($file1, 1) FileWrite($Savetext, ClipGet() & @CRLF) FileClose($Savetext) Link to comment Share on other sites More sharing options...
Subz Posted June 6, 2021 Share Posted June 6, 2021 Have you tried FileOpen($file1, 9) ;~ Creating the file path if it doesn't exist? Otherwise it should work fine, as long as you've saved data to the clipboard. Link to comment Share on other sites More sharing options...
Developers Jos Posted June 6, 2021 Developers Share Posted June 6, 2021 7 minutes ago, Subz said: Otherwise it should work fine, as long as you've saved data to the clipboard. As long as it is text! FrancescoDiMuro and Subz 2 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
RyeStick Posted June 6, 2021 Author Share Posted June 6, 2021 3 minutes ago, Jos said: As long as it is text! Why must it be text? Wouldn't anything in the clipboard get copied to the text file? I tried changing the FileOpen($file1, 8) but does not seem to create the filepath. Link to comment Share on other sites More sharing options...
JockoDundee Posted June 6, 2021 Share Posted June 6, 2021 17 minutes ago, RyeStick said: but can't seem to egt it to work. what do you mean? 1) no file is created 2) file created with unexpected data? Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Subz Posted June 6, 2021 Share Posted June 6, 2021 Quote Why must it be text? Wouldn't anything in the clipboard get copied to the text file? Not all data copied to clipboard is in text format, example copying an image to clipboard. Quote I tried changing the FileOpen($file1, 8) Did you try using flag 9? Flag 8 would try to open an existing file (read-only mode). Link to comment Share on other sites More sharing options...
Developers Jos Posted June 6, 2021 Developers Share Posted June 6, 2021 23 minutes ago, RyeStick said: Why must it be text? Wouldn't anything in the clipboard get copied to the text file? Yes,No! SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
RyeStick Posted June 6, 2021 Author Share Posted June 6, 2021 16 minutes ago, Subz said: Not all data copied to clipboard is in text format, example copying an image to clipboard. Did you try using flag 9? Flag 8 would try to open an existing file (read-only mode). Yes the I tried 9 also, The documentation says it is number 8. I have added the following $file1 = "C:\Users\Home\Desktop\Documents\test.txt" Local $Savetext = FileOpen($file1, 8) ; Prove it exists If FileExists($Savetext) Then MsgBox($MB_SYSTEMMODAL, "File", "Exists") Else MsgBox($MB_SYSTEMMODAL, "File", "Does not exist") EndIf FileWrite($Savetext, ClipGet() & @CRLF) FileClose($Savetext) MsgBox says the file does not exist. 30 minutes ago, JockoDundee said: what do you mean? 1) no file is created 2) file created with unexpected data? Both. I can't create the file and if I create the file manually and then attempt to amend/write over it with the clipboards contents then does it not work. Link to comment Share on other sites More sharing options...
Solution Subz Posted June 6, 2021 Solution Share Posted June 6, 2021 Flag 8 creates the folder path, you need to be combine it with either: Append text to file Overwrite existing file This could also be a result of not having rights to create the folder, in which case you could add #RequireAdmin to the top of your script, here is a basic example which works for me: ;~ Place some text into clipboard ClipPut("Clipboard example text") ;~ Path to file Local $sFileName = @ScriptDir & "\Documents\test123.txt" ;~ Create folder path + open in write mode, appending to file Local $hFileName = FileOpen($sFileName, 9) ;~ Write clipboard to the file FileWrite($hFileName, ClipGet() & @CRLF) FileClose($hFileName) ;~ Open the file in your preferred text editor. ShellExecute($sFileName) RyeStick 1 Link to comment Share on other sites More sharing options...
RyeStick Posted June 6, 2021 Author Share Posted June 6, 2021 25 minutes ago, Subz said: Flag 8 creates the folder path, you need to be combine it with either: Append text to file Overwrite existing file This could also be a result of not having rights to create the folder, in which case you could add #RequireAdmin to the top of your script, here is a basic example which works for me: ;~ Place some text into clipboard ClipPut("Clipboard example text") ;~ Path to file Local $sFileName = @ScriptDir & "\Documents\test123.txt" ;~ Create folder path + open in write mode, appending to file Local $hFileName = FileOpen($sFileName, 9) ;~ Write clipboard to the file FileWrite($hFileName, ClipGet() & @CRLF) FileClose($hFileName) ;~ Open the file in your preferred text editor. ShellExecute($sFileName) Your example works in creating a filepath, does it copy ClipGet() contents for you? as it opens an emtpy black notepad for me. Link to comment Share on other sites More sharing options...
Subz Posted June 6, 2021 Share Posted June 6, 2021 Yes, I tested the code before posting it. Did you use the code above or did you modify it? Link to comment Share on other sites More sharing options...
RyeStick Posted June 6, 2021 Author Share Posted June 6, 2021 26 minutes ago, Subz said: Yes, I tested the code before posting it. Did you use the code above or did you modify it? Wow, I just tried this on a different computer and it works. I have spent hours trying to get this to work. What do you think the problem may be?I am going to try a reinstall and get back to you. Link to comment Share on other sites More sharing options...
RyeStick Posted June 6, 2021 Author Share Posted June 6, 2021 47 minutes ago, RyeStick said: Wow, I just tried this on a different computer and it works. I have spent hours trying to get this to work. What do you think the problem may be?I am going to try a reinstall and get back to you. The Reinstall has not fixed the problem? do you think my register could be corrupted or something, how do you think I can fix the problem? Link to comment Share on other sites More sharing options...
JockoDundee Posted June 6, 2021 Share Posted June 6, 2021 @RyeStick, am I missing something or have you not specified what type of data is in the clipboard during your testing, and how did it get there? Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
pseakins Posted June 7, 2021 Share Posted June 7, 2021 8 hours ago, RyeStick said: The documentation says it is number 8 No it doesn't. It says "$FO_CREATEPATH (8) = Create directory structure if it doesn't exist (See Remarks)." You actually need, $FO_APPEND (1) = Write mode (append to end of file) OR $FO_OVERWRITE (2) = Write mode (erase previous contents) So, your arg should be 8 + 1 = 9 OR 8 + 2 = 10 depending on whether you want to extend the file or clear it every time. 6 hours ago, RyeStick said: how do you think I can fix the problem? Add some debug statements to your test code. Initially just try writing the file without messing around with extracting the clipboard. Once you have the filewrite working then implement the rest of your code. One step at a time. Phil Seakins Link to comment Share on other sites More sharing options...
Professor_Bernd Posted June 7, 2021 Share Posted June 7, 2021 7 hours ago, RyeStick said: Wow, I just tried this on a different computer and it works. I have spent hours trying to get this to work. What do you think the problem may be? It depends on whether you use clipboard tools, or clipboard cleaning tools (CCleaner), or password tools that clear the clipboard, or ... and maybe also on the Windows version (e.g. Win 10 can synchronize your clipboard between devices). Link to comment Share on other sites More sharing options...
Confuzzled Posted June 15, 2021 Share Posted June 15, 2021 (edited) Obtuse comment on this thread: A lot of effort would have been saved if return codes for each function were tested, or even just written to the console for debugging purposes. ConsoleWrite, @error, variable values, etc, clauses will help with debugging immensely. Example, return code for unable to create file, unable to open file in output mode, unable to write from clipboard, etc. You will then be able to see clearly where your code is failing, or even pick up on unanticipated conditions that you can test for and take appropriate action to make your code flexible and robust. This is just good coding practise. Get used to embedding it, early, is my strong suggestion. Go on, add the error code results, and see where your code broke, and why. It will be a great learning exercise for all. Edited June 15, 2021 by Confuzzled Link to comment Share on other sites More sharing options...
pseakins Posted June 15, 2021 Share Posted June 15, 2021 (edited) It's so simple. Just hover your mouse over a variable and type ALT-D. A debug statement will be added to your code automatically. Where there is no variable eg @error, just type @error on the following line, hover and type ALT-D. Then delete @error. If you want to know the runtime value of a variable or macro, eg $somestring or @HOUR , that is not nearby, type the variable name or macro on a blank line, hover over it and type ALT-D. Then delete the line containing $somestring or the macro that you just typed. Edited June 15, 2021 by pseakins @error was a bad example for debug display of a variable Exit 1 Phil Seakins Link to comment Share on other sites More sharing options...
Confuzzled Posted June 15, 2021 Share Posted June 15, 2021 Just to add further to the discussion, in your code FileWrite($hFileName, ClipGet() & @CRLF) don't forget you are actually doing three things (FileWrite, ClipGet and string concatenation), so the error code that is returned may apply to only one of the three functions when you test for it. In this situation, if this is where your code is inconsistent, you perform each operation separately, using intermediate variables, and test each @ERROR that is returned individually. Link to comment Share on other sites More sharing options...
JockoDundee Posted June 15, 2021 Share Posted June 15, 2021 13 minutes ago, Confuzzled said: ...so the error code that is returned may apply to only one of the three functions when you test for it... Actually it would always and only apply to the FileWrite() the way it is written, since ClipGet() is called first. IMO, and FWIW, inline “string concatenation” never alters @Error. Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now