0

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.

1
  • You could use following "hack" but the better solution would be to start Photoshop and wait for the handle to close. That would require a change in how you start it too though. while ((gps photoshop -ea ignore).Count -ne 0) {start-sleep -Seconds 10} Commented Apr 10, 2019 at 13:51

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

I like this Start-Process -Wait method. I think tying that in with IsProcActive to set the end time is a good final solution. I really appreciate your help.
Note though that with Start-Process -Wait you can’t have a timeout, it will wait forever. IMO the best solution is either Wait-Process -Timeout or the loop; with the loop you have more options (you can e.g. do logging or do other stuff while waiting like copying large files) but Wait-Process -Timeout is clearer and therefore easier to maintain.
I added a way to set the timeout to the answer. If you want logs while waiting, you have to use a loop, as Jim says.
0
  • 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

7 Comments

This script checks if the process is running and if a timeout is reached. In production code having a timeout is highly recommended, e.g. photoshop could hang.
Upon testing I received 2 errors: At line:175 char:25 + [string]$procName = "Photoshop", + ~~~~~~~~~~~ The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
At line:176 char:21 + [int]$timeout = 90, ## seconds + ~~ The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : InvalidLeftHandSide I will troubleshoot a bit and let you know what I come up with.
From the error, I guess you tried to put this script inside another one, right? In this case, you should omit the param since this can only be the 1st thing in your script. So instead of param([string]$procName = "Outlook" etc etc, just use $procName = "Outlook" etc
Thank you so much, that update worked perfectly in line with the other code. A clarifying question. If my PhotoShop script takes 15 minutes to run (Resizing a large batch of images), does that $timeout of 90 seconds force the next bit of code to begin?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.