skip to Main Content

I have two scripts I would like to combine, but the second script can’t begin until a program (Photoshop) is closed. Script one ends by starting a photoshop script with Invoke-Item. Once the Photoshop script is complete PhotoShop closes. The second code archives the raw files with a simple Move-Item. With PowerShell, how can I know when PhotoShop is closed and begin my Move-Item?

I have spent some time researching this to see what documentation there is, but either I am asking my questions poorly or it is an obscure enough I can’t find any leads to begin off of.

# Script One
ii "E:resizerScript.jsx"

#Something to determine when PhotoShop is closed and begin the next bit of code.

# Script Two
Move-Item -path "E:Staged*" -Destination "E:Archived"

I’m very new to coding and what I have is cobbled together from other articles. If anything is too unclear I would be happy to elaborate. Thanks in advance for any help or direction.

2

Answers


    • First, you need to find photoshop’s process name. Open powershell and run

    Get-Process | Select-Object -Property ProcessName

    • Then use the following (you can customize it according to your needs of course, I’ve tested using Outlook)
    param(
        [string]$procName = "Outlook",
        [int]$timeout = 90, ## seconds
        [int]$retryInterval = 1 ## seconds
    )
    
    $isProcActive = $true
    
    $timer = [Diagnostics.Stopwatch]::StartNew()
    
    # to check the process' name:
    # Get-Process | Select-Object -Property ProcessName
    
    while (($timer.Elapsed.TotalSeconds -lt $timeout) -and ($isProcActive)) {
    
        $procId = (Get-Process | Where-Object -Property ProcessName -EQ $procName).Id
    
        if ([string]::IsNullOrEmpty($procId))
        {
            Write-Host "$procName is finished"
            $isProcActive = $false
        }
    }
    
    $timer.Stop()
    
    if ($isProcActive)
    {
        Write-Host "$procName did not finish on time, aborting operation..."
        # maybe you want to kill it?
        # Stop-Process -Name $procName
        exit
    }
    
    # do whatever
    

    [UPDATE] if you need to put this inside another script, you need to omit the param since this must be the 1st thing in a script. So it would look like:

    # start of script
    $procName = "Outlook"
    $timeout = 90 ## seconds
    $retryInterval = 1 ## seconds
    
    $isProcActive = $true
    # etc etc
    
    

    Hope this helps,

    Jim

    Login or Signup to reply.
  1. You can use Wait-Process,

    Invoke-Item "E:resizerScript.jsx"
    Wait-Process photoshop
    Move-Item -Path "E:Staged*" -Destination "E:Archived"
    

    but I recommend using Start-Process -Wait to start Photoshop.

    $photoshopPath = "C:...Photoshop.exe"
    Start-Process $photoshopPath "E:resizerScript.jsx" -Wait
    Move-Item -Path "E:Staged*" -Destination "E:Archived"
    

    If you want to set the timeout:

    Start-Process $photoshopPath "E:resizerScript.jsx" -PassThru |
        Wait-Process -Timeout (15 * 60) -ErrorAction Stop
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search