Hello all,
I have had no luck in my search for a scripting method to tell Photoshop CC to open a new file using the clipboard. In my situation that option will always exist. In other words, File > New would reveal Clipboard as being an opiton. Unfortunately, I cannot figure out how to script this simple command.
Per the photoshop scripting guide (2018, p55), the following will create a new document and use the clipboard for its content. You may have to tweak it slightly.
tell application "Adobe Photoshop CC 2018"
activate
select all of current document
copy
set current layer of current document to layer "Background" of current document
set newDocRef to make new document paste newDocRef
end tell
Thank you Mockman,
What you have is not exactly what I was shooting for. The snippet you provided will paste, but it is not replicating the action of creating a New Document that is retaining the dimesnsions found in the clipboard. I was able to do some workarounds by using System Events:
For example, assumming I copied an image from a web page and it is now in my clipboard:
-This script below will work. The repeat and checking for a color mode is there just in case Photoshop wasn’t launched. This way, it will not execute keystrokes until the new document is present if Photoshop. There are probably better ways to achieve this “document is ready” check.
tell application id "com.adobe.Photoshop"
activate
set colorMode to "None"
repeat while colorMode is "None"
tell application "System Events" to keystroke "n" using command down
delay 0.5
tell application "System Events" to keystroke return
delay 0.5
try
set colorMode to mode of current document as text
if colorMode contains "constant" then
set colorMode to "None"
end if
if colorMode is not "None" then
exit repeat
end if
end try
if colorMode is not "None" then
delay 0.5
end if
end repeat
--Pressing Escape just in case any Photoshop fields were highlighted for whatever reason.
tell application "System Events" to key code 53
delay 0.5
tell application "System Events" to keystroke "v" using command down
end tell
I see your point. Preview has the same feature and it is handy. Also, I didn’t see anything in the scripting docs on the matter of waiting so maybe your confirm mode loop is the best solution for that.
FWIW, I have seen a reference to there being an art layer created upon pasting the clipboard into a new document, and that you can get the bounds of that layer and crop accordingly. Dunno if it’s doable though. Also, regardless of anything else, you should be able to use the standard photoshop paste rather than UI scripting that last part.
I definitely would rather use Photoshop’s paste - so thank you for pointing that out. I also decided to shorten the delays.
For what it is worth, I am using this script to open (avif) files. Part of my script (not included below) will detect a file type and if (avif) will open the image in Chrome and execute a copy. Since (avif) supports transparency, I would like to retain the file’s transparency. Therefore, I decided to check for the count of layers and delete the “Background” layer if the count was greater than 1. This was necessary because Photoshop’s File>New will retain the last used “Background Contents”. So if the last File>New instance happened to use “White” as its “Background Contents” then the image would paste in as a new “Layer 1”, and beneath it would be the white filled “Background” layer. Sure, an operator could then manually delete the “Backround” layout - But I would prefer the script deletes this unnecessary layer when applicable.
tell application id "com.adobe.Photoshop"
activate
--Wait until the current/new document has a color mode
set colorMode to "None"
repeat while colorMode is "None"
tell application "System Events" to keystroke "n" using command down
delay 0.1
tell application "System Events" to keystroke return
delay 0.1
try
set colorMode to mode of current document as text
if colorMode contains "constant" then
set colorMode to "None"
end if
if colorMode is not "None" then
exit repeat
end if
end try
if colorMode is not "None" then
delay 0.1
end if
end repeat
--Pressing Escape key, just in case any Photoshop fields are active
tell application "System Events" to key code 53
delay 0.1
paste
tell current document
try
if (count of layers) is greater than 1 then
delete layer "Background"
end if
end try
end tell
end tell