No internet connection
  1. Home
  2. How to

Use a single Streamdeck button for different commands in separate applications

By Andrew Sherman @Andrew_Sherman8
    2021-02-12 15:58:07.084Z

    I'm trying to set up a single Streamdeck button that triggers one of two different commands, based on which application is currently active in the foreground (even if both applications are currently open).

    What the command does, is centre the selected layers in either Adobe Photoshop or Adobe Illustrator.

    I'm getting an error on line 1 "error invoking app.activate, could not find running app with bundle id: ' '

    // Center in Illustrator
    if (sf.ui.app('com.adobe.Illustrator').isActive) {
        sf.ui.app('com.adobe.Illustrator').menuClick({
        menuPath: ["Object","Align","Horizontal Align Center"],
        });
        sf.ui.app('com.adobe.Illustrator').menuClick({
        menuPath: ["Object","Align","Vertical Align Center"],
        });
    
    // Center in Photoshop
    } else {
        sf.ui.app('com.adobe.Photoshop').menuClick({
        menuPath: ["Object","Select","All"],
        });
        sf.ui.app('com.adobe.Photoshop').menuClick({
        menuPath: ["Layer","Align Layers to Selection","Vertical Centers"],
        });
        sf.ui.app('com.adobe.Photoshop').menuClick({
        menuPath: ["Layer","Align Layers to Selection","Horizontal Centers"],
        });
    }
    
    Solved in post #3, click to view
    • 7 replies
    1. Dustin Harris @Dustin_Harris
        2021-02-13 04:03:02.262Z

        Try changing the first line to:

        if (sf.ui.frontmostApp.activeBundleID === "com.adobe.Illustrator") {
        

        Maybe that will do the trick?

        1. AAndrew Sherman @Andrew_Sherman8
            2021-02-14 13:28:03.059Z

            Thank for your reply Dustin, much appreciated. That helped a lot. After some further tweaks, it's working properly. For anyone interested, the capitilisation in the app id is important (com.adobe.illustrator and com.adobe.Photoshop are correct in this case). For some reason the Photoshop section needed a wait as well.

            Here's the script:

            // Center in Illustrator
            if (sf.ui.frontmostApp.activeBundleID === "com.adobe.illustrator") {
                
                sf.ui.app('com.adobe.illustrator').menuClick({
                menuPath: ["Object","Align","Horizontal Align Center"],
                });
            
                sf.ui.app('com.adobe.illustrator').menuClick({
                menuPath: ["Object","Align","Vertical Align Center"],
                });
            
            // Center in Photoshop
            } else {
                
                sf.ui.app('com.adobe.Photoshop').menuClick({
                menuPath: ["Select","All"],
                });
            
                sf.wait({
                intervalMs: 1000,
                });
            
                sf.ui.app('com.adobe.Photoshop').menuClick({
                menuPath: ["Layer","Align Layers to Selection","Vertical Centers"],
                });
            
                sf.ui.app('com.adobe.Photoshop').menuClick({
                menuPath: ["Layer","Align Layers to Selection","Horizontal Centers"],
                });
            
                sf.ui.app('com.adobe.Photoshop').menuClick({
                menuPath: ["Select","Deselect"],
                });
            }
            
            ReplySolution
            1. Dustin Harris @Dustin_Harris
                2021-02-15 15:13:57.925Z

                Just for my own practice benefit I refactored this script, adding an else if for the photoshop condition, and an else so that if you hit the button and you're in neither photoshop or illustrator, it won't throw an error (nothing will happen). Use it if you'd like :)

                const illustratorID = "com.adobe.illustrator";
                const photoshopID = "com.adobe.Photoshop";
                
                // Center in Illustrator
                if (sf.ui.frontmostApp.activeBundleID === illustratorID) {
                
                    const illustrator = sf.ui.app(illustratorID);
                
                    illustrator.menuClick({
                        menuPath: ["Object", "Align", "Horizontal Align Center"],
                    });
                
                    illustrator.menuClick({
                        menuPath: ["Object", "Align", "Vertical Align Center"],
                    });
                
                // Center in Photoshop
                } else if (sf.ui.frontmostApp.activeBundleID === photoshopID) {
                
                    const photoshop = sf.ui.app(photoshopID);
                
                    photoshop.menuClick({
                        menuPath: ["Select", "All"],
                    });
                
                    sf.wait({
                        intervalMs: 1000,
                    });
                
                    photoshop.menuClick({
                        menuPath: ["Layer", "Align Layers to Selection", "Vertical Centers"],
                    });
                
                    photoshop.menuClick({
                        menuPath: ["Layer", "Align Layers to Selection", "Horizontal Centers"],
                    });
                
                    photoshop.menuClick({
                        menuPath: ["Select", "Deselect"],
                    });
                
                } else {
                
                    //do nothing
                }
                
                1. AAndrew Sherman @Andrew_Sherman8
                    2021-02-15 15:24:19.077Z

                    Good idea Dustin, that's even better. Also nice to keep it tidy with defining the constants.

                    I'm thinking of re-looking at this at some point to give it an option where you use a modifier key (eg press command and then Streamdeck key), which would then just align it horizontally instead of both.

                    1. Dustin Harris @Dustin_Harris
                        2021-02-15 15:26:39.640Z

                        Ooohhh... modifier key + streamdeck button isn't something I've tried yet!

                        1. modified SD macros are great.
                          saves a lot of space and redundant keys

                        2. In reply toAndrew_Sherman8: