1

I'm trying to write a script that will open an application and then send the save command every 30 minutes to that application, not the active window in case I'm switching to some other task.

Here is what I have so far that works aside from only sending the command to the program which for this test is excel. I have tried switching the active window but that doesn't work and it just continues to save the active window.

$programPath = "C:\Users\cflora\Desktop\test.xlsx"
$saveShortcut = "^s"

# start the program
Start-Process $programPath
$programRunning = $true

Start-Sleep -Seconds 10

# loop until the program is exited
while ($programRunning) {

    # send the "Save" command
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.SendKeys]::SendWait($saveShortcut)

    # wait 30 minutes
    Start-Sleep -Seconds 10

    # check if the program is still running
    $programRunning = Get-Process | Where-Object {$_.ProcessName -eq "Excel"}

    # if the program is not running, stop the loop
    if (!$programRunning) {
        break
    }
}
Chase
  • 11
  • 1
  • Programmatic activation of windows and sending keystrokes is brittle and disruptive. Consider alternatives; e.g., [Microsoft 365 now has an auto-save feature](https://support.microsoft.com/en-us/office/what-is-autosave-6d6bd723-ebfd-4e40-b5f6-ae6e8088f7a5). Also, you can use COM automation to launch Excel (`New-Object -ComObject Excel.Application`), which allows you to control it programmatically. If you really need to automate window activation, see [this answer](https://stackoverflow.com/a/73675531/45375). – mklement0 Feb 16 '23 at 21:31
  • For clarification, I know that excel already has an auto save, this is for a program that doesn't have it. I was just testing with excel because it's faster. – Chase Feb 16 '23 at 22:59
  • Understood. I've closed your question as a duplicate of the previously mentioned answer, which I hope you'll be able to adapt to your needs. If not, please ask a _new_ question based on where, specifically, you got stuck in the adaptation process. – mklement0 Feb 17 '23 at 02:39

0 Answers0