No internet connection
  1. Home
  2. How to

Open TextEdit, create new document and minimize on next press

By Jan Kuhn @Jan_Kuhn
    2024-12-02 17:28:38.492Z2024-12-02 17:42:54.656Z

    Hey,
    is there a way to do the following via stream deck buttons:

    1. Button click
    • open TextEdit
    • Create new Document
      2.Button click
    • minimize TextEdit
    • resume to last highlighted application

    Preferably it would also just create another new document when TextEdit is already open but minimized.

    I'm still new and trying to figure out basic things.
    Thank you!

    • 5 replies
    1. Kitch Membery @Kitch2024-12-02 21:38:19.203Z

      Hi @Jan_Kuhn,

      Try this...

      // Get Frontmost App
      const frontmostApp = sf.ui.frontmostApp;
      
      // Open Text Edit
      sf.app.launch({ path: "TextEdit" });
      
      // Wait for TextEdit
      sf.ui.app("com.apple.TextEdit").elementWaitFor();
      
      // Create New TextEdit Document
      sf.ui.app("com.apple.TextEdit").menuClick({
          menuPath: ["File", "New"],
      });
      
      // Hide TextEdit
      sf.ui.app("com.apple.TextEdit").menuClick({
          menuPath: ["TextEdit", "Hide TextEdit"],
      });
      
      // Activate previous app
      frontmostApp.appActivate();
      
      1. JJan Kuhn @Jan_Kuhn
          2024-12-05 15:49:27.488Z

          Thank you so much for the quick reply. Unfortunately it's not working either.

          Log:
          !! Command Error: Text Edit NEW Script WAL [user:cm479g3dh000f6k10dtj8sq2j:cm4bhqevj0002541039h0jr07]:
          Couldn't get count of attribute 'AXChildren' of element with title '', role '': kAXErrorCannotComplete (Text Edit NEW Script WAL: Line 11)
          SoundFlow.Shortcuts.AXUIElementException: Couldn't get count of attribute 'AXChildren' of element with title '', role '': kAXErrorCannotComplete
          at SoundFlow.Shortcuts.AXUIElement.GetSubElementsImpl(String attribute, Int32 maxCount) + 0x3fc
          at SoundFlow.Shortcuts.AXUIElement.GetSubElements(String attribute, Int32 maxCount, Boolean ignoreEmptyShells) + 0x18
          at SoundFlow.Shortcuts.AXUIElement.GetMenuItems() + 0xe4
          at SoundFlow.Shortcuts.AXUIElement.GetMenuItem(String title) + 0x50
          at SoundFlow.Shortcuts.Automation.Actions.Menu.GetMenuItem(AXUIElement appElement, String[] menuNames) + 0x38
          at SoundFlow.Shortcuts.Automation.Actions.ClickMenuAction.d__20.MoveNext() + 0x9c
          --- End of stack trace from previous location ---
          at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x24
          at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0x100
          at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x68
          at SoundFlow.Shortcuts.Automation.AutoAction`1.d__25.MoveNext() + 0x368

          05.12.2024 16:48:44.80 [Backend]: << Command: Text Edit NEW Script WAL [user:cm479g3dh000f6k10dtj8sq2j:cm4bhqevj0002541039h0jr07]

          05.12.2024 16:48:47.91 [EditorWindow:Renderer]: Active Focus Container: code Line 33963 file:///Applications/SoundFlow.app/Contents/Helpers/SoundFlow.app/Contents/Resources/app.asar/dist/editor.js

          1. Opening Text Edit and creating a new document can be a bit tricky depending on your system's settings and iCloud settings.
            The code above gave me an error as well so I opted to start TextEdit via an AppleScript command.
            Give this a try:

            // Get Frontmost App
            const frontmostApp = sf.ui.frontmostApp;
            
            const textEdit = sf.ui.app("com.apple.TextEdit");
            
            // Open Text Edit
            sf.system.execAppleScript({
                script: ` tell application "TextEdit"
             activate
            
            end tell`
            })
            
            // Create New TextEdit Document
            textEdit.menuClick({
                menuPath: ["File", "New"],
            });
            
            // Hide TextEdit
            sf.ui.app("com.apple.TextEdit").menuClick({
                menuPath: ["TextEdit", "Hide TextEdit"],
            });
            
            // Activate previous app
            frontmostApp.appActivate();
            
            1. JJan Kuhn @Jan_Kuhn
                2024-12-05 19:24:30.303Z

                Thanks for working on this again. I've tried it and it opens TextEdit, creates a new document, but minimizes it immediately without a chance of doing anything to it.

                1. Chris Shaw @Chris_Shaw2024-12-05 20:25:53.013Z2024-12-05 21:22:38.245Z

                  I had to change the workflow here to account for a couple of different scenarios.
                  I'm assuming that you'll be running this from a stream deck (you can use a keyboard trigger but you'll need to set up two triggers for this command).

                  • On the first button press if Textedit isn't open it will be launched. If, at launch, there are no documents open it will create a new document.
                  • On the second button press, textedit windows will be minimized and the app will be hidden (which should focus the previous app).
                  • On a third press, since text edit is already running, all windows will be un-minimized and text edit will be focused.
                  • If you want to want a new document to be created hold the option key while pressing the stream deck button. This is to prevent a new document being created every time you switch to textedit via the stream deck button.

                  If you want to trigger this script via a keyboard trigger you need to creat two triggers for the script - one with the option key and one without:
                  (Be careful and make sure that no other app is using any of these keyboard combinations since they would interfere with the triggers you set up for this script)

                  const modifiers = event.keyboardState.asString
                  const textEdit = sf.ui.app("com.apple.TextEdit");
                  const textEditWindows = textEdit.windows;
                  
                  // Get Frontmost App
                  const frontmostApp = sf.ui.frontmostApp;
                  
                  // If TextEdit is focused, minimize windows and hide
                  if (frontmostApp.title.value == "TextEdit") {
                      textEditWindows.forEach(win => win.windowMinimize())
                  
                      textEdit.menuClick({
                          menuPath: ["TextEdit", "Hide TextEdit"],
                      });
                      throw 0
                  };
                  
                  // If TextEdit isn't running open it
                  if (!textEdit.isRunning) {
                      // Open TextEdit
                      sf.system.execAppleScript({
                          script: ` 
                          tell application "TextEdit"
                          activate
                          end tell`
                      })
                  }
                  
                  // otherwise, focus TextEdit and un-minimize all winodws
                  textEdit.appActivate()
                  
                  textEditWindows.forEach(win => win.windowRestore());
                  
                  // If there are no text edit windows open or the cmd key is held create new document
                  if (textEditWindows.length == 0 || modifiers.includes("alt")) {
                      // Create New TextEdit Document
                      textEdit.menuClick({
                          menuPath: ["File", "New"],
                      });
                      textEdit.appActivate()
                  }
                  

                  Personally I think minimizing takes too long (unless you have minimize animations turned off in system prefs).
                  Hiding seems more efficient.
                  If you'd rather just hide TextEdit then delete or comment lines 10 and 32