Save notepad file thats open in background

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
samt
Posts: 61
Joined: 09 Sep 2022, 13:29

Save notepad file thats open in background

Post by samt » 04 Feb 2023, 00:47

I've copied and modified the 2nd last script from
viewtopic.php?f=76&t=106071
and have so far made it so that the notepad file will open in the background when the script runs, and then paste my clipboard into the notepad. But I'm having trouble getting control to save the file while its in the background, cus when the script goes to close the notepad window, I get a pop-up window asking me if I want to save it or not first before closing it, so clearly the ctrl+s command is not being sent.

edit: I need the latest content at the top, and oldest at the bottom. This is all needs to be done in the background or thru directly writing to the file.

Code: Select all

SetKeyDelay 0
SetTitleMatchMode, 2
FileEncoding, UTF-8
FileAppend,, found.txt
IFWinNotExist, % BackGround := "found.txt ahk_exe NotePad.exe"
	Run, "NotePad.exe" "found.txt",, Min
sleep 500
ControlSend, Edit1, %clipboard%`n`n`n`n, %BackGround%
sleep 100
; ControlSend, {Ctrl Down}s{Ctrl Up}, %BackGround% ; <---cant get control to save the file while it's in background/minimized
ControlSend, Edit1, {Ctrl Down}s{Ctrl Up}, %BackGround% ; <--tried this too
sleep 200
if WinExist("found.txt - Notepad")
WinClose ; at this point it asks me if I want to save the file first before closing
ExitApp
Last edited by samt on 04 Feb 2023, 14:16, edited 1 time in total.

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: Save notepad file thats open in background

Post by boiler » 04 Feb 2023, 04:50

I don’t understand the purpose of this. Why use Notepad instead of writing directly to the file?

User avatar
mikeyww
Posts: 26435
Joined: 09 Sep 2014, 18:38

Re: Save notepad file thats open in background

Post by mikeyww » 04 Feb 2023, 07:09

An example is below.

Code: Select all

#Requires AutoHotkey v1.1.33
filePath := A_ScriptDir "\found.txt"
FileEncoding UTF-8
FileRead txt, % filePath
FileRecycle % filePath
FileAppend % Clipboard "`n`n`n`n" txt, % filePath
Run % filePath
ExitApp
Also:

https://www.autohotkey.com/board/topic/80417-fileprependw-insert-text-at-begin-of-file-utf-16/

viewtopic.php?t=61744

samt
Posts: 61
Joined: 09 Sep 2022, 13:29

Re: Save notepad file thats open in background

Post by samt » 04 Feb 2023, 13:04

boiler wrote:
04 Feb 2023, 04:50
I don’t understand the purpose of this. Why use Notepad instead of writing directly to the file?
I need the content to be entered into the top of the file which FileAppend doesn't do, but it looks like the link above should do that. I'll give it a go soon and report back
Last edited by samt on 04 Feb 2023, 14:13, edited 1 time in total.

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

Re: Save notepad file thats open in background

Post by garry » 04 Feb 2023, 14:08

also an example

Code: Select all

F1:= a_scriptdir . "\test.txt"
;--------------------------------------------- 
;- copy marked text to ( file top )
;---------------------------------------------
$F2::
clipboard:=""	   
Send, {Control Down}c{Control Up}
ClipWait,2,1
if (ErrorLevel)
    return
fx        := FileOpen(F1,"rw","UTF-8")
prev      := fx.Read()
fx.Length := 3
fx.Write(Clipboard "`r`n------------`r`n" . prev)
fx.Close()
ToolTip Saved to %f1%`n%clipboard%, 250, 500
Sleep 1500
ToolTip
clipboard=
Return
;-------------------------------------------
$F4::
try,
 run,%f1%
Return
;-------------------------------------------

samt
Posts: 61
Joined: 09 Sep 2022, 13:29

Re: Save notepad file thats open in background

Post by samt » 04 Feb 2023, 16:32

I got it, though using a different method. I did not explain things well enough in my first post, so the suggestions here haven't been to my purposes, my fault.
I found and used the top script from viewtopic.php?t=28230

Here is my slightly modified version that works perfectly for me after testing a bunch- it adds text from my clipboard to the top of the .txt file and saves it. So its content is most recent at the top of the file and oldest at the bottom. I added the date and time for each time this script runs which gets attached to the copied content.
The stuff in curly braces and the line just before that I did not modify.
Thanks for everyone else's suggestions.

Code: Select all

            
FormatTime, CurrentDateTime,, HH:mm:ss  MM-dd-yyyy

            FilePrepend("C:\Program Files\AutoHotkey\AHK scripts\test.txt", "`t`t`t`t" . CurrentDateTime . "`n" . Clipboard . "`n`n`n`n")

            FilePrepend(filename, atext)
            {
               FileRead fileContent, % filename
               FileDelete % filename
               FileAppend % atext . "`n" . filecontent, % filename
            }

Post Reply

Return to “Ask for Help (v1)”