我试图制作一个代码程序集来创建我自己的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发布于 2022-01-28 11:42:19
好的,编译错误,我现在知道了。试试这个:
-- 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经常令人困惑。系统事件比这更令人困惑,因为它不明显,什么是GUI脚本,什么不是。祝你好运。
--
附注:我觉得tell process块是完全多余的,并且只有在使用GUI脚本来操作应用程序的窗口时才是必要的。keystroke命令不知道或不关心哪个应用程序接收这些键,在这种情况下,重要的是在开始“按下”键之前激活Photoshop。我将由您来决定是否可以丢弃tell process。
p.p.s.AppleScriptable应用程序通常不包括cut和paste命令,因为它们已经提供了更好的获取和设置内容的方法。然而,Photoshop确实如此。因此,如果您还没有尝试过,请尝试在“tell Photoshop”块的末尾插入一个paste命令,然后注释掉“tell System Events”块,看看它是否适合您。如果是这样的话,您可以完全消除系统事件块!
P.S.这也是值得进一步研究PS的字典,看看它是否可以打开和放置您的“bg_file”直接。如果是这样的话,你可以消除“告诉预览”和其他的系统事件也可怕!!
https://stackoverflow.com/questions/70887770
复制相似问题