Jump to content

Recommended Posts

Posted

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)

Posted

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.

  • Developers
Posted
  On 6/6/2021 at 4:17 PM, Subz said:

Otherwise it should work fine, as long as you've saved data to the clipboard.

Expand  

As long as it is text!

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.
  :)

Posted
  On 6/6/2021 at 4:24 PM, Jos said:

As long as it is text!

Expand  

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.

Posted
  Quote

Why must it be text? Wouldn't anything in the clipboard get copied to the text file?

Expand  

Not all data copied to clipboard is in text format, example copying an image to clipboard.

  Quote

I tried changing the FileOpen($file1, 8)

Expand  

Did you try using flag 9?  Flag 8 would try to open an existing file (read-only mode).

 

Posted
  On 6/6/2021 at 4:42 PM, 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).

 

Expand  

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.

  On 6/6/2021 at 4:30 PM, JockoDundee said:

what do you mean?

1) no file is created

2) file created with unexpected data?

 

Expand  

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.

  • Solution
Posted

Flag 8 creates the folder path, you need to be combine it with either:

  1. Append text to file
  2. 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)

 

Posted
  On 6/6/2021 at 5:13 PM, Subz said:

Flag 8 creates the folder path, you need to be combine it with either:

  1. Append text to file
  2. 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)

 

Expand  

Your example works in creating a filepath, does it copy ClipGet() contents for you? as it opens an emtpy black notepad for me.

Posted
  On 6/6/2021 at 5:48 PM, Subz said:

Yes, I tested the code before posting it.  Did you use the code above or did you modify it?

Expand  

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.

Posted
  On 6/6/2021 at 6:17 PM, 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.

Expand  

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?

Posted

@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...

Posted
  On 6/6/2021 at 5:02 PM, RyeStick said:

The documentation says it is number 8

Expand  

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.

  On 6/6/2021 at 7:05 PM, RyeStick said:

how do you think I can fix the problem?

Expand  

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

Posted
  On 6/6/2021 at 6:17 PM, 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?

Expand  

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).

  • 2 weeks later...
Posted (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 by Confuzzled
Posted (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 by pseakins
@error was a bad example for debug display of a variable

Phil Seakins

Posted

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.

Posted
  On 6/15/2021 at 2:27 PM, Confuzzled said:

...so the error code that is returned may apply to only one of the three functions when you test for it...

Expand  

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...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...