No internet connection
  1. Home
  2. Macro and Script Help

Launching and hiding a specific file folder from a Stream Deck

By Justin Krol @Justin_Krol
    2024-11-11 17:07:59.162Z

    Title

    Launching and hiding a specific file folder from a Stream Deck

    What do you expect to happen when you run the script/macro?

    I'd like this script to open and close a specific file on my computer and then duplicate and edit it for essential project folders.

    Are you seeing an error?

    What happens when you run this script?

    Nothing seems to be happening when I run the script..finder windows do no open or close and I don't get an error.

    How were you running this script?

    I used a Stream Deck button

    How important is this issue to you?

    4

    Details

    {
        "inputExpected": "I'd like this script to open and close a specific file on my computer and then duplicate and edit it for essential project folders. ",
        "inputIsError": false,
        "inputWhatHappens": "Nothing seems to be happening when I run the script..finder windows do no open or close and I don't get an error. ",
        "inputHowRun": {
            "key": "-MpfwmPg-2Sb-HxHQAff",
            "title": "I used a Stream Deck button"
        },
        "inputImportance": 4,
        "inputTitle": "Launching and hiding a specific  file folder from a Stream Deck"
    }

    Source

    const appName = 'Finder';
    const appRef = sf.ui.app('com.apple.finder');
    const appPath = "/Users/Shared";
    
    const isAppRunning = appRef.isRunning;
    const isAppFrontmost = sf.ui.frontmostApp.uiElement['Title'] === appName;
    const isAppWindowOpen = appRef.windows.length > 1;
    
    switch (true) {
        case (!isAppRunning):
            sf.app.launch({ path: appPath });
            break
        case (!isAppFrontmost && isAppWindowOpen):
            sf.app.launch({ path: appPath });
            break
        case ((!isAppFrontmost || isAppFrontmost) && !isAppWindowOpen):
            appRef.appActivate();
            appRef.menuClick({ menuPath: ['File', 'New Window'] });
            break
        case (isAppFrontmost && isAppWindowOpen):
            appRef.menuClick({
                menuPath: [appName, `Hide ${appName}`],
            });
            break
    }
    
    

    Links

    User UID: XH03HyxIkaZm7CXik1lfHYOCNoF2

    Feedback Key: sffeedback:XH03HyxIkaZm7CXik1lfHYOCNoF2:-OBRBZrY_-IFODQFW_7w

    Feedback ZIP: FwvhMqyp6BA5isXaDtFVojNAL14FZvjCqDaDxm02IJC48J7mpXfszD1M2nnGWK8my7FscXVr0iOuOiQUdzoKlpYR2Nls8X7Zgc/G17WyNfn1Xytmpc4ERuJvfJKjL1NiDPDNayeiA9hL1XqusVcn1wgMq1fGeGO6Mjb6X4tTwXGYSSwyTBsqc2EgrrdgFCGHLFL4cW8OmdWkKwlF3ymIO8JV0u8DcgnPo6b7GONE9RXyixx3ccpse5A3RfGWx7zQSfatxvKRIhHGyH5EziW0P70VaJYfFdPjdC4V+neMbOFngpMD0w911Va6vpgSKCEncce+wDAtZRw0+eI4pT6i2jAUJm0UMmLDOEy0Iwl8XLY=

    Solved in post #5, click to view
    • 12 replies
    1. J
      Justin Krol @Justin_Krol
        2024-11-13 22:55:25.394Z

        I made a couple tweaks and still nothing. @Kitch, do you have any ideas? This code mostly works for opening and closing other apps, however, it does sometimes fail at line 18 (can't find file, new window). I sometimes have to manually open an app for it to recognize again... let me know what you think and thanks!

        const appName = 'Finder';
        const appRef = sf.ui.app('com.apple.finder');
        const appPath = "/Applications/Finder/Users/Shared.app";
        
        const isAppRunning = appRef.isRunning;
        const isAppFrontmost = sf.ui.frontmostApp.uiElement['Title'] === appName;
        const isAppWindowOpen = appRef.windows.length > 0;
        
        switch (true) {
            case (!isAppRunning):
                sf.app.launch({ path: appPath });
                break
            case (!isAppFrontmost && isAppWindowOpen):
                sf.app.launch({ path: appPath });
                break
            case ((!isAppFrontmost || isAppFrontmost) && !isAppWindowOpen):
                appRef.appActivate();
                appRef.menuClick({ menuPath: ['File', 'New Window'] });
                break
            case (isAppFrontmost && isAppWindowOpen):
                appRef.menuClick({
                    menuPath: [appName, `Hide ${appName}`],
                });
                break
        }
        
        1. Kitch Membery @Kitch2024-11-13 23:32:01.860Z

          Hi @Justin_Krol,

          To get a bit more context, can you write in point form what should happen in each of the four scenarios/cases?

          1. JJustin Krol @Justin_Krol
              2024-11-13 23:37:17.914Z

              Totally! I’m just trying to achieve basic open/show to hide action with the same Stream Deck button.

              1. Click on Stream Deck button to open/activate/bring to forefront said app (in this case, looking at a specific folder in finder)

              2. Click on the same Stream Deck button to hide said application

              Does that help? Thanks!

              1. Kitch Membery @Kitch2024-11-14 00:42:42.040Z

                Thanks @Justin_Krol,

                This looks like two scenarios that could be accomplished like this.

                const appName = 'Finder';
                const app = sf.ui.app('com.apple.finder');
                const folderPath = "~/Desktop/FOLDER NAME";
                
                const isAppRunning = app.isRunning;
                const isAppFrontmost = sf.ui.frontmostApp.title.value === appName;
                
                if (!isAppFrontmost) {
                    // Activate the app
                    app.appActivate();
                
                    // Open the Folder
                    sf.file.open({ path: folderPath });
                } else {
                    // Hide app
                    app.appHide();
                }
                
                Reply1 LikeSolution
                1. JJustin Krol @Justin_Krol
                    2024-11-14 16:43:21.092Z

                    You did it again! Thanks man!

                    1. JJustin Krol @Justin_Krol
                        2024-11-14 16:57:26.597Z

                        Not to be greedy with your time, but one more tweak that I'm curious about for this specific one. What would the code look like for this if I wanted to instead CLOSE the finder window instead of hiding it? Just to avoid having like 10 finder windows pop up when I trigger a specific one to keep it clean (if that makes sense). Thank you!

                        1. Kitch Membery @Kitch2024-11-14 17:57:56.070Z

                          Hi @Justin_Krol,

                          I'm not sure if you've tested the functionality but I don't think the script opens multiple instances of the window. If however you want to close the window before hiding Finder you could use this.

                          const appName = 'Finder';
                          const app = sf.ui.app('com.apple.finder');
                          const folderPath = "~/Desktop/FOLDER NAME";
                          
                          const isAppRunning = app.isRunning;
                          const isAppFrontmost = sf.ui.frontmostApp.title.value === appName;
                          
                          if (!isAppFrontmost) {
                              // Activate the app
                              app.appActivate();
                          
                              // Open the Folder
                              sf.file.open({ path: folderPath });
                          } else {
                              // Close Folder Window
                              const folderName = folderPath.split("/").pop();
                              const folderWindow = app.windows.getByTitle(folderName);
                          
                              if (folderWindow.exists) {
                                  folderWindow.windowClose()
                              }
                          
                              // Hide app
                              app.appHide();
                          }
                          
                          1. JJustin Krol @Justin_Krol
                              2024-12-06 15:39:07.887Z

                              Hey @Kitch!

                              Ok, one more tweak with this one that I'm trying to solve as a general blanket script. Finder is always running, so it doesn't really apply to this one. But take this script below for "Calculator" -

                              const appName = 'Calculator';
                              const app = sf.ui.app('com.apple.calculator');
                              const folderPath = "/System/Applications/Calculator.app";
                              
                              const isAppRunning = app.isRunning;
                              const isAppFrontmost = sf.ui.frontmostApp.title.value === appName;
                              
                              if (!isAppFrontmost) {
                                  // Activate the app
                                  app.appActivate();
                              
                                  // Open the Folder
                                  sf.file.open({ path: folderPath });
                              } else {
                                  // Hide app
                                  app.appHide();
                              }
                              

                              Now, this works fine if Calculator is already running, of course, but I'm trying to figure out how to add the launch aspect as well if calculator isn't already running. I've got this for a basic blanket script to open and hide a lot of general applications, but of course if fails if the programs aren't already running.

                              Thanks!!

                              1. Kitch Membery @Kitch2024-12-06 18:34:37.608Z

                                Hi @Justin_Krol,

                                Try this. :-)

                                const app = sf.ui.app('com.apple.calculator');
                                
                                const isAppRunning = app.isRunning;
                                const isAppFrontmost = app.isFrontmost;
                                
                                if (!isAppRunning || !isAppFrontmost) {
                                    app.appEnsureIsRunningAndActive();
                                } else {
                                    app.appHide();
                                }
                                
                                1. Kitch Membery @Kitch2024-12-06 19:06:54.128Z

                                  Hi @Justin_Krol,

                                  This should also work with the finder...

                                  const app = sf.ui.finder;
                                  
                                  const isAppRunning = app.isRunning;
                                  const isAppFrontmost = app.isFrontmost;
                                  
                                  if (!isAppRunning || !isAppFrontmost) {
                                      app.appEnsureIsRunningAndActive();
                                  } else {
                                      app.appHide();
                                  }
                                  
                2. J
                  In reply toJustin_Krol:
                  Justin Krol @Justin_Krol
                    2024-12-09 16:38:59.314Z

                    This all works beautifully, by the way...sorry for delay. Thank you, as always.

                    1. Kitch Membery @Kitch2024-12-09 18:35:48.019Z

                      Fantastic! :-)