Jump to content

How to Autosave a file in a temp path


RedAck
 Share

Recommended Posts

Hi Everyone,

I am developing a script to autoclose a program if the application is idle for say 10 sec. I also want to save the file before closing the application in a different path and name from parent. Please find the below sample code. 

---------------------------------------------------------------------------------------------------------------------------------

Opt("WinTitleMatchMode", 2) ;2 = any substring
Opt("TrayIconDebug", 1)

$S_running = "check-4-app" ;name the script
If WinExists($S_running) Then Exit
AutoItWinSetTitle($S_running)

$title = "- Notepad"
$count = 0
While 1
    $state = WinGetState($title)
    $count += 1

    If $state = 15 Or $state = 47 Or $state = 0 Then $count = 0

    If $count > 10 Then
       WinClose($title)
      Send("!s")
      $count = 0
      EndIf

    ToolTip("count = " & $count, 0, 0, "state = " & $state)
    Sleep(1000)

WEnd

--------------------------------------------------------------------------------------------

 

Thanks in advance.

Link to comment
Share on other sites

so what isn't working? you need to help up help you. you are supposed to learn. tell us what doesn't work and what you have tried. otherwise it sounds like you want US to develop your script. did you write that code?

anyway, look at how to read the TEMP environment variable.

 

from help file, modified for TEMP variable

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Retrieve the value of the environment variable %TEMP%.
    ; When you assign or retrieve an envorinment variable you do so minus the percentage signs (%).
    Local $sEnvVar = EnvGet("TEMP")

    ; Display the value of the environment variable %TEMP%.
    MsgBox($MB_SYSTEMMODAL, "", "The environment variable %TEMP% has the value of: " & @CRLF & @CRLF & $sEnvVar) ; 
EndFunc   ;==>Example

 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

Rather than looking at the combined states of the window, you can you bitand to see if a specific state is present.  that way you don't have to check for every possible combination that may include that state.    if you want to check if the window includes the state 'active'...bitand (your window state, 8).

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

The previous script i posted will update the file once it get closed. I'm trying to save the file in different location with different name. I was trying the following code.

----------------------------------------------------------------------------------------------------

If $count > 3 Then
       WinActivate($title)
       Send("!F")
       Send("!A")
       WinWaitActive("Save As","File name")
       Send("temp file save")
       Send("!s")
-----------------------------------------------------------------------------------------

 

this changes the application window to notepad but it is not showing the saveas window.

Link to comment
Share on other sites

you can just do a filecopy after you do the save.

Or ctrlgettext and filewrite...the ctrl id is 15.

 

Whenever you can get away from interacting with windows, you should.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

 

4 hours ago, RedAck said:

this changes the application window to notepad but it is not showing the saveas window.

you are probably doing the first send too fast.  do some sleep after winactivate of notepad...or use winwaitactive 

and after that first send, the next send alt keys wont work, just send the letter

Edited by Nine
Link to comment
Share on other sites

 The following below code is working fine. Any suggestions on shorting this code and this file is getting saved in its default location. Could you please suggest on how to change the path in "save as" dialogue.

---------------------------------------------------------------------------------------------------------------------------------

  If $count > 3 Then
       WinActivate($title)
       WinWaitActive($title)
       Send("!F")
       Send("A")
       WinWaitActive("[TITLE:Save As]")
       Send("temp file save")
       Sleep(2000)
       Send("!S")
       Sleep(3000)
       ProcessClose("Notepad.exe")
       $count = 0
    EndIf

--------------------------------------------------------------------------------------------------------------------------------------------

Link to comment
Share on other sites

in any case, stack overflow has an answer for you ready to go.

https://stackoverflow.com/questions/22461209/autoit-and-notepad

Example()

Func Example()
    ; Run Notepad
    Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)

    ; Keep the Notepad window active when using the Send function.
    SendKeepActive("[CLASS:Notepad]")

    ; Simulate entering a string of text. If you try to change to a different window other than Notepad, then the Notepad window will
    ; be brought to focus again.
    For $i = 1 To 10
        Sleep(5)
        Send("notepad - ")
    Next

    ; Disable the Notepad window being active when using the Send function.
    SendKeepActive("")

    ; Close the Notepad window using the handle returned by WinWait.
    WinClose($hWnd)

    Send("{ENTER}") ; <<< SAVE

    ; Now a screen will pop up and ask to save the changes, the classname of the window is called
    ; "#32770" and simulating the "TAB" key to move to the second button in which the "ENTER" is simulated to not "save the file"
    WinWaitActive("[CLASS:#32770]", '', 2)
    Sleep(50)
    Send('newName.txt')
    Sleep(1000)
    Send("{TAB}{ENTER}")
EndFunc   ;==>Example

it works. tested. notice in the code at the bottom, waiting for CLASS:#32770, that is the windows Dialog box, and also what you need to wait to interact with.

This is good for you to study, run and learn and play with as learning goes. But in reality, it's WAY easier/faster/better to close the file and copy it somewhere to where you want it at any name you choose. KISS, Keep It Simple St... you know the rest.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

"Could you please suggest on how to change the path in "save as" dialogue."

You can simply put the folder before the file name like ".\Temp\temp file save" or "C:\somefolder\temp file save" or use macro files @... 

Edited by Nine
Link to comment
Share on other sites

Not using 'send' (more reliable):

$h = WinGetHandle("Save As")
WinActivate($h)
$c = ControlGetHandle($h,"",1001)
ControlSetText($h,"",$c,"c:\dev\test.txt")
$c = ControlGetHandle($h,"",1)
ControlFocus($h,"",$c)
ControlClick($h,"",$c)

I hate enabling the use of not using filecopy()...but the functions I'm using are good practice for automating applications more reliably.  this is still very minimalistic.   if I wanted my script to be successful 99.99999 percent of the time I would verify all window and control states.


I'd also use winmenuselectitem instead

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

the example i linked to is pretty reliable:

WinWaitActive("[CLASS:#32770]", '', 2)

 

you could include the title of said dialog in that call. I also don't like it that they choose to not do it the best way but we can't control them, just help them learn. I do also prefer to use ControlSend and such. Very much agree.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

Last one...here is something more in line with how I would automate it...again, it's very minimalistic, there is no error handling, and I'm not validating win states or control states...

Run("notepad.exe")
$h = WinWait("[CLASS:Notepad]")
$c = ControlGetHandle($h,"",15)
ControlSetText($h,"",$c,"New Text")
WinActivate($h)
WinMenuSelectItem($h,"","&File",'Save &As...')
$h2 = WinWait("Save As")
WinActivate($h)
$c = ControlGetHandle($h2,"",1001)
ControlSetText($h2,"",$c,"c:\dev\test.txt")
$c = ControlGetHandle($h2,"",1)
ControlFocus($h2,"",$c)
ControlClick($h2,"",$c)
WinWaitClose($h2,"",1)
If WinExists("Confirm Save As") Then
    $h3 = WinGetHandle("Confirm Save As")
    $c = ControlGetHandle($h3,"","Button1")
    WinActivate($h3)
    ControlFocus($h3,"",$c)
    ConsoleWrite($h3 & " " & $c)
    ControlClick($h3,"",$c)
EndIf
WinClose($h)

 

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • 2 months later...

I have asked the following question in other discussions aswell.

Please let me know if you can help!

The above script particularly works fine for single application. What if i want to monitor idle time for morethan 2 application. How  to modify the above script.

Link to comment
Share on other sites

2nd instance of same app?...use winlist instead of winwait (the first one), and loop through the array.  make everything below winwait a function with one parameter = $h, and call that in the loop.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

12 hours ago, jdelaney said:

2nd instance of same app?...use winlist instead of winwait (the first one), and loop through the array.  make everything below winwait a function with one parameter = $h, and call that in the loop.

Not the same application. The above script works for Notepad. What if i want to monitor word and excel in the same script. or more alication

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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