How to add text to a txt file. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hancre
Posts: 241
Joined: 02 Jul 2021, 20:51

How to add text to a txt file.

Post by hancre » 01 Feb 2023, 01:08

I tried this following script to add text to a file.

f3:: add to the bottom area
f4:: add to the top area

f3 doesn't work, showing the tooltip.
f4 works well.

How can I fix to add the text to the bottom area?
Thanks for any help in advance.

Code: Select all

GroupAdd, CutGroup, ahk_exe, word.exe
GroupAdd, CutGroup, ahk_exe, hwp.exe
GroupAdd, CutGroup, ahk_exe, notepad.exe
GroupAdd, CutGroup, ahk_exe, notepad++.exe
GroupAdd, CutGroup, ahk_exe, TedNpad.exe
GroupAdd, CutGroup, ahk_exe, sublime_text.exe
GroupAdd, CutGroup, ahk_exe, evernote.exe
GroupAdd, CutGroup, ahk_exe, memoit.exe 
GroupAdd, CutGroup, ahk_exe, onenote.exe
return

Code: Select all

f3:: ;,::
Clipboard:= ""
if WinActive("Google Docs") or WinActive("ahk_group CutGroup")
   Sendinput, {Home}{Shift Down}{End}{Shift Up}^x{Del}{End}
else
  SendInput, ^c
ClipWait, 0
Str := Clipboard
Sleep 300
FileAppend, %Str%`n, C:\공통\source\AHK\data_paste.txt, UTF-8
ToolTip, Saved to data_paste.txt, 250, 500
Sleep, 1500
ToolTip
Return

Code: Select all

f4::  ; !.:: ;최신내용을 상단에 복사하기 
   Clipboard := ""
   if WinActive("Google Docs") or WinActive("ahk_group CutGroup")
     Sendinput {Home}{Shift Down}{End}{Shift Up}^x{Del}{End} ;{BackSpace}{End} ;지우고 위로 올라가기  
    ;Send ^x
   else 
       SendInput, ^c
   ClipWait 0
       if (ErrorLevel)
           return
       oFile := FileOpen("C:\공통\source\AHK\data_paste.txt", 0x3, "UTF-8")
       prev := oFile.Read()
       oFile.Length := 3
       oFile.Write(Clipboard "`n" prev)
       oFile.Close()
       ToolTip Saved to data_paste.txt, 250, 500
       Sleep 1500
       ToolTip
   Return

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: How to add text to a txt file.

Post by garry » 01 Feb 2023, 03:07

I tried this , add to the bottom area
EDIT : small modified added errorlevel

Code: Select all

clipboard:=""
F1:="C:\공통\source\AHK\data_paste.txt"
;-----------------------------------------
;-- TEST , append to text-file F1  (bottom )
;-----------------------------------------
$F3::
clipboard:=""
;SendInput, ^a     ;- copy all text 
;sleep,700
;-- copy marked text
Send, {Control Down}c{Control Up}
ClipWait,1,text
if ErrorLevel
  {
  msgbox, 262192,NOT COPIED ,The attempt to copy text onto the clipboard failed.
  return
  }
FileAppend, %clipboard%`r`n,%f1%, UTF-8
ToolTip, Saved to data_paste.txt, 250, 500
Sleep, 1000
ToolTip
try,
 run,%f1%
clipboard:="" 
Return
;------------------------------------------
esc::exitapp


hancre
Posts: 241
Joined: 02 Jul 2021, 20:51

Re: How to add text to a txt file.

Post by hancre » 01 Feb 2023, 20:30

Thank you for your help. sorry. It shows the tooltip, but doesn't add the text to any area to the file.

[Mod edit: Removed quotes tags that made the reply look like a quote of garry.]

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: How to add text to a txt file.

Post by garry » 02 Feb 2023, 09:35

again a test example , creates first a text-file as UTF-8 with bom ( once I had a problem , the file was saved as UTF-16 LE )
alt+F9 to copy marked text to file F1
;---

Code: Select all

;--- at first run creates a folder and file F1  UTF-8 with BOM if not exists ------------
clipboard:=""
FormatTime,Korea,%a_nowutc% L0x0412, dddd MMMM yyyy-MM-dd  HH:mm 
fd:="C:\공통\source\AHK"
ifnotexist,%fd%
  filecreatedir,%fd%
F1:=fd . "\data_paste.txt"           ;- filename
;--- at first run ----------- 
ifnotexist,%f1%
  {
  FileOpen(F1, "w", "UTF-8").Write(korea . "`r`n")  ;- writes once the date as example
  try,
    run,notepad "%f1%"                              ;- see it's saved as UTF-8 with BOM
  }	
return  

;-----------------------------------------
;-- TEST , append to text-file F1  (bottom )
;-----------------------------------------
$!F9::                                               ;- alt + F9 copies marked text and write to F1
clipboard:=""
Send, {Control Down}c{Control Up}                    ;- copy selected 
ClipWait,2,1
if ErrorLevel
  {
  msgbox, 262192,NOT COPIED ,The attempt to copy text onto the clipboard failed.
  return
  }
;FileAppend, %clipboard%`r`n,%f1%, UTF-8
FileOpen(F1, "a", "UTF-8").Write(clipboard . "`r`n")   ;- append to file ( UTF-8 with BOM )
ToolTip, Saved to data_paste.txt, 250, 500
Sleep, 1000
ToolTip
try,
 run,notepad "%f1%"     ;- open with notepad for test
clipboard:="" 
Return
;------------------------------------------
esc::exitapp

hancre
Posts: 241
Joined: 02 Jul 2021, 20:51

Re: How to add text to a txt file.

Post by hancre » 03 Feb 2023, 07:53

Thank you for your help. I tried your new code, I got an error.
< .txt can't be created >

I fixed my original code to add Clipboard to the bottom area.

Code: Select all

!.:: 
Clipboard:=""
   if (WinActive("ahk_group CutGroup"))
      Send {Home}{Shift Down}{End}{Shift Up}^x{BackSpace 2}
   Else 
      Send ^c
 Clipwait, 0
  Str := Clipboard
 if (ErrorLevel) 
        MsgBox 0x40010, Error, Couldn't grab text.
        ;  FormatTime, T, %A_Now%, yyyyMMdd-hhmmss
  FileAppend, %str%`r`n, C:\공통\source\AHK\data_paste.txt, UTF-8
    ToolTip Saved to data_paste.txt, 250, 500
    Sleep 1500
    ToolTip
Return

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: How to add text to a txt file.  Topic is solved

Post by Smile_ » 03 Feb 2023, 11:27

When you create your group such as GroupAdd, CutGroup, ahk_exe, word.exe, ahk_exe, word.exe is not supposed to be splited.
Try your first script with this instead:

Code: Select all

GroupAdd, CutGroup, ahk_exe word.exe
GroupAdd, CutGroup, ahk_exe hwp.exe
GroupAdd, CutGroup, ahk_exe notepad.exe
GroupAdd, CutGroup, ahk_exe notepad++.exe
GroupAdd, CutGroup, ahk_exe TedNpad.exe
GroupAdd, CutGroup, ahk_exe sublime_text.exe
GroupAdd, CutGroup, ahk_exe evernote.exe
GroupAdd, CutGroup, ahk_exe memoit.exe 
GroupAdd, CutGroup, ahk_exe onenote.exe

hancre
Posts: 241
Joined: 02 Jul 2021, 20:51

Re: How to add text to a txt file.

Post by hancre » 16 Feb 2023, 20:47

Smile_ wrote:
03 Feb 2023, 11:27
Sorry for my late reply. I fixed the issue thanks to your tip. ^^;;
Thank you for your help.

Post Reply

Return to “Ask for Help (v1)”