我正在尝试使用 Python 在 Adobe Photoshop 2025 中自动执行 .jsx (JavaScript) 脚本。我想从 Python 脚本触发脚本,但我不确定最好的方法是什么。
当前尝试:我尝试在 macOS 中使用 osascript 命令来调用 Adobe Photoshop 并运行 .jsx 文件,但我遇到了脚本执行问题,出现类似错误(在终端中尝试这些命令时):
Command: osascript -e 'tell application "Adobe Photoshop 2025" to do javascript ("path/to/my/jsx/file")'
Error: "Adobe Photoshop 2025 got an error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop."-> (8800) Expected: ;.
Command: osascript "path/to/my/jsx/file"
Error: Expected end of line, etc. but found “/”. (-2741)
Command: osascript -e 'tell application "Adobe Photoshop 2025" to do javascript (POSIX file "path/to/my/jsx/file")'
Error: "Can’t get POSIX file."
当我通过“浏览”>“脚本”直接在 Photoshop 中运行 Javascript 文件时,它运行完美,没有任何错误。
我的Python代码:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
import subprocess
class PhotoHandler(FileSystemEventHandler):
def __init__(self, target_folder, script_path):
self.target_folder = target_folder
self.script_path = script_path
self.photo_count = 0
self.photos = []
def on_created(self, event):
if event.is_directory:
return
if event.src_path.endswith(('.jpg', '.jpeg', '.png', '.ARW')):
self.photos.append(event.src_path)
self.photo_count += 1
if self.photo_count == 3:
self.process_photos()
self.photo_count = 0
self.photos = []
def process_photos(self):
applescript_path = "/Users/ishikagurnani/Documents/runjavascript.scpt"
print(f"Photos detected: {self.photos}")
try:
# Call Photoshop script
subprocess.run(
["osascript", applescript_path],
check=True
)
print("Photoshop script executed successfully!")
except subprocess.CalledProcessError as e:
print(f"Error running Photoshop script: {e}")
# Configuration
folder_to_watch = r"/Users/ishikagurnani/Pictures/Test/Auto Imported Photos"
script_path = r"/Users/ishikagurnani/Documents/full_automate_photobooth.jsx"
event_handler = PhotoHandler(folder_to_watch, script_path)
observer = Observer()
observer.schedule(event_handler, folder_to_watch, recursive=False)
observer.start()
try:
print("Monitoring folder for new photos...")
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
不确定这是否有效,但尝试一下,
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
import subprocess
class PhotoHandler(FileSystemEventHandler):
def __init__(self, target_folder, script_path):
self.target_folder = target_folder
self.script_path = script_path
self.photo_count = 0
self.photos = []
def on_created(self, event):
if event.is_directory:
return
if event.src_path.endswith(('.jpg', '.jpeg', '.png', '.ARW')):
self.photos.append(event.src_path)
self.photo_count += 1
if self.photo_count == 3:
self.process_photos()
self.photo_count = 0
self.photos = []
def process_photos(self):
print(f"Photos detected: {self.photos}")
try:
run_photoshop_script(self.script_path)
except Exception as e:
print(f"Error running Photoshop script: {e}")
def run_photoshop_script(script_path):
applescript_command = f'tell application "Adobe Photoshop 2025" to do javascript file "{script_path}"'
try:
subprocess.run(["osascript", "-e", applescript_command], check=True)
print("Photoshop script executed successfully!")
except subprocess.CalledProcessError as e:
print(f"Error running Photoshop script: {e}")
folder_to_watch = r"/Users/ishikagurnani/Pictures/Test/Auto Imported Photos"
script_path = r"/Users/ishikagurnani/Documents/full_automate_photobooth.jsx"
event_handler = PhotoHandler(folder_to_watch, script_path)
observer = Observer()
observer.schedule(event_handler, folder_to_watch, recursive=False)
observer.start()
try:
print("Monitoring folder for new photos...")
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()