创建一个新的PSD文件,并使用AppleScript复制/粘贴新层



我试图制作一个代码集来创建我自己的应用程序描述,以便在Photoshop中创建图像,但不幸的是,我遇到了一个错误,找不到它的来源。

脚本的第一部分工作正常,随机图像被很好地复制到剪贴板,但其余部分有问题。

-- Select random image
tell application "Finder"
set bg_img to file (random number from 1 to (count files of folder "HD:Works:Club:LAYERS:0-BG")) of folder "HD:Works:Club:LAYERS:0-BG"
end tell
tell application "Preview"
activate
open bg_img
end tell
-- Select and copy image to clipboard
tell application "System Events"
tell process "Preview"
keystroke "a" using command down
keystroke "c" using command down
end tell
end tell
-- Quit Preview Application
tell application "Preview" to quit

tell application "Adobe Photoshop 2022"
-- Create a new document
set docRef to make new document with properties {width:1200, height:1200, resolution:72}
tell docRef
-- Unlock the background layer and fill it with gray color
set background layer of layer 1 of docRef to false
fill selection with contents {class:RGB color, red:200, green:200, blue:200}
end tell
end tell
-- Paste image to new layer 
tell application "System Events"
tell process "Adobe Photoshop 2022"
set newLayer to make art layer with properties {name:"LayerA"}
keystroke "v" using command down
end tell
end tell

好的,编译错误;我现在明白了。试试这个:

-- Paste image to new layer 
tell application "Adobe Photoshop 2022"
activate
set newLayer to make art layer with properties {name:"LayerA"}
end tell
tell application "System Events"
tell process "Adobe Photoshop 2022"
keystroke "v" using command down
end tell
end tell

请记住,您使用tell application "NAME" … end tell块为1。告诉AppleScript要向哪个应用程序发送命令,以及2。从中获取命令和对象的自定义术语(关键字(的应用程序。

因此,您的tell application "System Events"…end tell块使用SE术语和命令。其中的tell process "Photoshop"…end tell块仍然寻址到系统事件。process是SE为特定于SE的对象定义的关键字。同样地,keystroke是SE特定的命令。

要在Photoshop中make一个新的art layer,您必须将命令放入其针对PS的tell application块中。

是的,AppleScript经常令人困惑。系统事件比这更令人困惑10倍,这要归功于不清楚什么是GUI脚本,什么不是。祝你好运

--

p.s.我觉得tell process块是完全多余的,只有在使用GUI脚本操作应用程序的窗口时才有必要。IIRCkeystroke命令不知道或不关心哪个应用程序接收到这些键,在这种情况下,重要的是在开始"按下"键之前激活Photoshop。我将由您决定是否可以丢弃tell process

p.p.s.AppleScriptable应用程序通常不包括cutpaste命令,通常是因为它们已经提供了更好的获取和设置内容的方法。然而,Photoshop的确如此。所以,如果你还没有尝试过,试着在"告诉Photoshop"块的末尾插入一个paste命令,然后注释掉"告诉系统事件"块,看看这是否适合你。如果是这样的话,你就可以完全摆脱系统事件块了!

p.p.p.s.还值得进一步研究PS的字典,看看它是否可以直接打开并放置您的"bg_file"。如果是这样的话,你可以消除"告诉预览"和其他系统事件的可怕!!

最新更新