Search code examples
javascriptapplescriptphotoshopphotoshop-script

Open All Smart Objects inside a Photoshop file using AppleScript


The pictures I am working with have a lot of Smart Object Layers. Since there is no official way to open the content of all Smart Objects at once, I am thinking of doing this using AppleScript and JavaScript.

However I am facing a problem when my first Smart Object is opened, the focus of Photoshop moves to the newly opened picture (the content of the already opened Smart Object). So the focus should be changed again to the original file (the one with the many smart objects) so the next Smart Object to be opened.

It might be something simple but I am not very experienced and for the last couple of days I couldn't find a way to do this in the context of Photoshop.

Here is my code:

on run
    tell application "Adobe Photoshop CS6"
        activate
        set Doc_Ref to the current document
        set Doc_Name to name of Doc_Ref
        tell Doc_Ref
            set layerList to name of every layer in Doc_Ref whose kind is smart object layer
            repeat with currentName in layerList
                set current layer to layer currentName
                my Edit_Smart_Layer()
            end repeat
        end tell
    end tell
end run

on Edit_Smart_Layer()
    tell application "Adobe Photoshop CS6"
        do javascript "editSmartLayer(); function editSmartLayer() { function cTID(s) { return app.charIDToTypeID(s); }; function sTID(s) { return app.stringIDToTypeID(s); }; var desc01 = new ActionDescriptor(); executeAction( sTID('placedLayerEditContents'), desc01, DialogModes.NO );};" show debugger on runtime error 
    end tell
end Edit_Smart_Layer

P.S. I think the code also should have a check if the Smart Object Layer is visible.


Solution

  • The Solution is:

    on run {input}
        try
            tell application "Adobe Photoshop CS6"
                activate
                
                set Doc_Ref to the current document
                tell Doc_Ref
                    try
                        set layerList to name of every layer in Doc_Ref whose kind is smart object layer
                    on error
                        display dialog "The active picture does not have Smart Object Layers !"
                        return input
                    end try
                    
                    repeat with currentName in layerList
                        set current layer to layer currentName
                        my Edit_Smart_Layer()
                        tell application "Adobe Photoshop CS6"
                            set current document to Doc_Ref
                        end tell
                    end repeat
                    display dialog "Opened Smart Objects: " & length of layerList
                end tell
            end tell
        on error
            display dialog "Please run Photoshop and open a picture !"
            return input
        end try
    end run
    
    
    on Edit_Smart_Layer()
        
        tell application "Adobe Photoshop CS6"
            
            do javascript "editSmartLayer(); function editSmartLayer() { function cTID(s) { return app.charIDToTypeID(s); }; function sTID(s) { return app.stringIDToTypeID(s); }; var desc01 = new ActionDescriptor(); executeAction( sTID('placedLayerEditContents'), desc01, DialogModes.NO );};" show debugger on runtime error
            
        end tell
        
    end Edit_Smart_Layer