Here is what I am trying to do:
I need to perform a photoshop action on all images with the same name within a folder system. I then need to save the image as a .png file with a new name in the same location as the original image. The following code work correctly for the most part except instead of saving a .png file, it saves a .psd instead.
I am not allowed to use any 3rd party modules such as PowerShell-Photoshop-Api-Tools.
Here is the code that I have so far:
# Specify the root folder where your images are located
$rootFolderPath = "C:\PhotoshopTest"
# Specify the common image name (excluding file extension)
$imageName = "TestImage"
# Specify the name of the action you want to apply
$actionSetName = "AutomationTest"
$actionName = "Test1"
# Connect to Photoshop
$app = New-Object -ComObject "Photoshop.Application"
# Get all image files with the same name (excluding file extensions) in the folder and subfolders
$imageFiles = Get-ChildItem -Path $rootFolderPath -Recurse -File | Where-Object { $_.BaseName -eq $imageName }
# Process each image
foreach ($imageFile in $imageFiles) {
# Open the image
$doc = $app.Open($imageFile.FullName)
# Execute any necessary edits or adjustments
$app.DoAction($actionName, $actionSetName)
# Save the modified image with a new name
$newFileName = "Test1.png"
$newImagePath = Join-Path $imageFile.DirectoryName $newFileName
$pngSaveOptions = [System.Type]::GetType("Adobe.Photoshop.PNGSaveOptions")
$doc.SaveAs($newImagePath, $pngSaveOptions)
# Close the image
$doc.Close()
}
# Quit Photoshop
$app.Quit()
Write-Host "Images processed and saved with new names."
I have tried the following variations of the SaveAs functions:
Saved a .psd file:
$doc.SaveAs($newImagePath)
$pngSaveOptions = [System.Type]::GetType("Adobe.Photoshop.PsSaveOptions")
$doc.SaveAs($newImagePath, $pngSaveOptions)
$pngSaveOptions = [System.Type]::GetType("Adobe.Photoshop.TiffSaveOptions")
$doc.SaveAs($newImagePath, $pngSaveOptions)
$app.ActiveDocument.SaveAs($newImagePath, $pngSaveOptions)
Broke and did not save anything:
$doc.SaveAs($newImagePath, 16)
$doc.SaveAs($newImagePath, [System.Drawing.Imaging.ImageFormat]::Png)
Solution found by @fatalerrer and @mclayton:
$pngSaveOptions = new-object -ComObject "Photoshop.PngSaveOptions
Hopefully someone more knowledgeable than me will be able to provide a better answer, but until then:
In each of the cases you mentioned it sounds like the application just saved with the default options. Based on that you're obviously not pulling any of the settings, or at least not in a format that can be used.
Going off of this documentation: https://github.com/Adobe-CEP/CEP-Resources/blob/master/Documentation/Product%20specific%20Documentation/Photoshop%20Scripting/photoshop-cc-scripting-guide-2019.pdf (Page 32)
I realize it's not PowerShell, but based off of the general format it might be worth trying to mirror some of the examples. For instance, try and drop the Adobe in "Adobe.Photoshop.PNGSaveOptions" since the example code in the document only uses "Photoshop.JPEGSaveOptions".
Also worth noting is that in the example code they create an object for the file-format settings rather than using a GET command. So you may be find better results by experimenting with New-Object for the save options. I also noticed that both the VBS and JS examples include a True value in the save command, which could be important.
Sorry I don't have more to offer, but hopefully this at least gives you some more options to explore.