Reputation: 11
I am trying to automate the execution of a .jsx (JavaScript) script in Adobe Photoshop 2025 using Python. I want to trigger the script from a Python script, but I am not sure what the best approach is.
Current Attempts: I’ve tried using the osascript command in macOS to call Adobe Photoshop and run the .jsx file, but I am facing issues with script execution, getting errors like (while trying these commands in Terminal):
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."
When I run my Javascript file directly in Photoshop through Browse>Scripts, it works perfectly without any errors.
My Python code:
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()
Upvotes: 1
Views: 114
Reputation: 46
not sure if this works but try this,
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()
Upvotes: 0