No internet connection
  1. Home
  2. How to

Import Audio Code Trouble

By Tiernan Cranny @Tiernan_Cranny
    2023-07-14 17:20:35.909Z

    I am having trouble with my code. I am new to coding so I use very simple language. My goal is to import audio, use the find command, paste command, move down to second folder, highlight all the stems, and then press copy/convert (i used cmd+c) and then enter enter. Again I am new so this is probably really simple code. Thank you for the help.

    sf.wait({
    intervalMs: 400,
    });

    sf.keyboard.press({
    keys: "shift+cmd+I",
    fast: true,
    });

    sf.wait({
    intervalMs: 400,
    });

    sf.keyboard.press({
    keys: "cmd+f",
    fast: true,
    });

    sf.wait({
    intervalMs: 400,
    });

    sf.keyboard.type({
    text: "/Users/tiernan/Documents/IN PROGRESS SESSION/T112-G001-F001-K001-ARTIST-SONG"
    })
    });

    sf.wait({
    intervalMs: 400,
    });

    sf.keyboard.press({
    keys: "right",
    fast: true,
    });

    sf.wait({
    intervalMs: 400,
    });

    sf.keyboard.press({
    keys: "down",
    fast: true,
    });

    sf.wait({
    intervalMs: 400,
    });

    sf.keyboard.({
    keys: "shift+down*(256)",
    fast: true,
    });

    sf.wait({
    intervalMs: 400,
    });

    • 11 replies

    There are 11 replies. Estimated reading time: 13 minutes

    1. Nathan Salefski @nathansalefski
        2023-07-14 20:26:05.261Z

        If you quickly edit this post, click </> above where you type and copy the code where it says "preformatted text", it'll appear like the text below and be much easier to read.

        preformatted text
        
        1. Nathan Salefski @nathansalefski
            2023-07-14 21:14:27.808Z

            Here is what I believe you would want. The issue would be this would only open this exact file path and import that audio.

            //Activate Pro Tools
            sf.ui.proTools.appActivate();
            //File, Import, Audio
            sf.ui.proTools.menuClick({
                menuPath: ["File","Import","Audio..."],
            });
            //Define Import Audio Window
            const openWin = sf.ui.proTools.windows.whoseTitle.is('Open').first;
            //Wait for 'Open' Window
            openWin.elementWaitFor();
            //Open 'Go' Sheet
            sf.keyboard.type({ text: '/' });
            //Wait for Open Window
            openWin.sheets.first.elementWaitFor();
            //Set Destination
            openWin.sheets.first.textFields.first.elementSetTextAreaValue({
                value: "/Users/tiernan/Documents/IN PROGRESS SESSION/T112-G001-F001-K001-ARTIST-SONG",
            });
            //Press Return
            sf.keyboard.press({
                keys: "return",
            });
            //Select All
            sf.keyboard.press({
                keys: "cmd+a",
            });
            //Click Open
            openWin.buttons.whoseTitle.is('Open').first.elementClick();
            //Wait
            sf.wait();
            //Click Open on Second Window
            sf.ui.proTools.windows.whoseTitle.is("Open").first.buttons.whoseTitle.is("Open").first.elementClick();
            
            1. @Nathan_Salefski how about a sf.interaction to define the value instead? Put this at the top of the code:

              const importDirectory = sf.interaction.selectFolder({
                  prompt : `Select Folder to Import All Audio`,
              }).path;
              

              And then change your code line 16 to read :

              openWin.sheets.first.textFields.first.elementSetTextAreaValue({
                  value: importDirectory,
              });
              

              Untested.... but should in theory prompt you for the folder and then run the script as is...

              1. Nathan Salefski @nathansalefski
                  2023-07-14 23:00:07.158Z2023-07-15 01:21:08.032Z

                  I had no idea you could do that, that's awesome. This should work for both Copying and Converting the imported audio

                  //Activate Pro Tools
                  sf.ui.proTools.appActivate();
                  // Prompt for Folder with Option To Cancel
                  let importDirectory = sf.interaction.selectFolder({
                      prompt : `Select Folder to Import All Audio`,
                  }).path;
                  //File, Import, Audio
                  sf.ui.proTools.menuClick({
                      menuPath: ["File","Import","Audio..."],
                  });
                  //Define Import Audio Window
                  const openWin = sf.ui.proTools.windows.whoseTitle.is('Open').first;
                  //Wait for 'Open' Window
                  openWin.elementWaitFor();
                  //Open 'Go' Sheet
                  sf.keyboard.type({ text: '/' });
                  //Wait for Sheet Window
                  openWin.sheets.first.elementWaitFor();
                  //Set Destination
                  openWin.sheets.first.textFields.first.elementSetTextAreaValue({
                      value: `${importDirectory}`,
                  });
                  //Press Return
                  sf.keyboard.press({
                      keys: "return",
                  });
                  //Wait for Window to Disappear
                  openWin.sheets.first.elementWaitFor({ waitType: "Disappear" })
                  //Select All
                  sf.keyboard.press({
                      keys: "cmd+a",
                  });
                  //Click Open
                  openWin.buttons.whoseTitle.is('Open').first.elementClick();
                  //Click Open on Second Window if Necessary
                  if (sf.ui.proTools.windows.whoseTitle.is("Open").exists) {
                      //Click Open on Second Window
                      sf.ui.proTools.windows.whoseTitle.is("Open").first.buttons.whoseTitle.is("Open").first.elementClick();
                  };
                  
                  1. TTiernan Cranny @Tiernan_Cranny
                      2023-07-15 08:59:09.830Z

                      thnak you so much

                      1. Nathan Salefski @nathansalefski
                          2023-07-15 14:41:53.913Z

                          Absolutely. Let me know if it works out for ya

                  2. O
                    In reply toTiernan_Cranny:

                    Can you post a video of you doing your workflow manually? Helps people understand the end goal. There are a number of things you can do to improve this script for sure. Just want to make sure we understand the final result you're looking for.

                    1. TTiernan Cranny @Tiernan_Cranny
                        2023-07-15 08:59:38.506Z

                        will do

                      • In reply toTiernan_Cranny:
                        Eric Huergo @Eric_Huergo
                          2023-07-14 21:13:48.342Z2023-07-14 21:24:47.340Z

                          Sup' Tiernan - quick tip for inputting code on the forums. When you're typing in the text there's a button above the textbox that looks like a "</>". Click that and replace the line 'preformatted text' with your code. After that, your code will look nice and organized like so:

                          sf.wait({
                          intervalMs: 400,
                          });
                          sf.keyboard.press({
                          keys: "shift+cmd+I",
                          fast: true,
                          });
                          sf.wait({
                          intervalMs: 400,
                          });
                          sf.keyboard.press({
                          keys: "cmd+f",
                          fast: true,
                          });
                          sf.wait({
                          intervalMs: 400,
                          });
                          sf.keyboard.type({
                          text: "/Users/tiernan/Documents/IN PROGRESS SESSION/T112-G001-F001-K001-ARTIST-SONG"
                          })
                          });
                          sf.wait({
                          intervalMs: 400,
                          });
                          sf.keyboard.press({
                          keys: "right",
                          fast: true,
                          });
                          sf.wait({
                          intervalMs: 400,
                          });
                          sf.keyboard.press({
                          keys: "down",
                          fast: true,
                          });
                          sf.wait({
                          intervalMs: 400,
                          });
                          sf.keyboard.({
                          keys: "shift+down*(256)",
                          fast: true,
                          });
                          sf.wait({
                          intervalMs: 400,
                          });
                          

                          What I gather you're trying to do from the outline of your script is the following:

                          1. Wait 400ms
                          2. Shift+Cmd+I (Import Audio)
                          3. Wait 400ms
                          4. Cmd+F (Find)
                          5. Wait 400ms
                          6. Type the string "/Users/tiernan/Documents/IN PROGRESS SESSION/T112-G001-F001-K001-ARTIST-SONG"
                          7. Wait 400ms
                          8. Press the "right" arrow key
                          9. Wait 400ms
                          10. Press the "down" arrow key
                          11. Wait 400ms
                          12. Press Shift+Down 256 times
                          13. Wait 400ms

                          Generally speaking, what you're trying to do is simple but there are a few errors in your process. These are the errors I see.

                          1. By simply pasting the text in the forum without it being inside a preformatted text box things can get awry and it's harder to get the help you're looking for (It's much easier to read the code too if it's preformatted).
                          2. Lots of waits that don't necessarily need to be there. I would start to consider thinking with the logic of waiting for 'x' to appear rather than x amount of ms (That's wasted time!).
                          3. Cmd+F - If your goal was to find a file, sure but then you'd be at the mercy of waiting for your computer to find the file. Given that you provided a filepath in your example, my guess is that you were referring to the "Go To Folder" Command (Shift+Cmd+G)
                          4. Typing the path - this works but it is slower and will keep whatever you have in your clipboard. The solution I provided below is faster but won't keep your clipboard.
                          5. Shift+DownAsterisk256 - I'm guessing you wanted to do this 256 times but you put the 'Asterisk(256)' within the quotation marks so technically you're saying something along the lines of whatever '*' means the "(256)" key. It's just not gonna work. I substituted this for a 'Cmd+A' so it selects everything within that directory.

                          Anywho, in general the forums are a great place to get help/support (and even ideas) but I've personally found that being specific and seeking knowledge rather than a solution will always get you better results. Anywho, ignoring what I just said, here's a solution that I believe should accomplish what you were trying to get done.

                          /// 0a. State the filepath to the folder containing your files.
                          var searchText = '/Users/tiernan/Documents/IN PROGRESS SESSION/T112-G001-F001-K001-ARTIST-SONG'
                          
                          /// 0b. Set your computer's clipboard to the filepath specified above.
                          sf.clipboard.setText({ text: searchText })
                          
                          /// 1. Import Audio.
                          sf.ui.proTools.menuClick({
                              menuPath: ["File","Import","Audio..."],
                          });
                          
                          /// 2. Wait for the "Import Audio" Dialog Box to appear.
                          sf.ui.proTools.windows.whoseTitle.is("Open").first.children.whoseRole.is("AXStaticText").whoseValue.is("Import Audio").first.elementWaitFor();
                          
                          /// 3. Go To Folder.
                          sf.keyboard.press({
                              keys: "cmd+shift+g",
                          });
                          
                          /// 4. Wait 400ms.
                          sf.wait({
                              intervalMs: 400,
                          });
                          
                          /// 5. Paste the filepath.
                          sf.keyboard.press({
                              keys: "cmd+v",
                          });
                          
                          /// 6. Press Return (Enter).
                          sf.keyboard.press({
                              keys: "return",
                          });
                          
                          /// 7. Wait 400ms.
                          sf.wait({
                              intervalMs: 400,
                          });
                          
                          /// 8. Press the Right Key.
                          sf.keyboard.press({
                              keys: "right",
                          });
                          
                          /// 9. Select all files in the specified directory.
                          sf.keyboard.press({
                              keys: "cmd+a",
                          });
                          
                          /// #### From this point forward I'm only assuming what you're trying to do.
                          
                          /// 10. Click "Add All".
                          sf.ui.proTools.windows.whoseTitle.is("Open").first.buttons.whoseTitle.is("Add All ->").first.elementClick();
                          
                          /// 11. Wait 400ms.
                          sf.wait({
                              intervalMs: 400,
                          });
                          
                          /// 12. Click "Open".
                          sf.ui.proTools.windows.whoseTitle.is("Open").first.buttons.whoseTitle.is("Open").first.elementClick();
                          

                          .
                          For future reference, if you're trying to learn more about soundflow these resources are invaluable:

                          https://www.youtube.com/playlist?list=PLKWpZOwx5Z3jxnpNo_dQPhDQNRwp7DCNj

                          https://soundflow.org/docs/how-to/custom-commands/javascript-resources

                          https://www.youtube.com/playlist?list=PLyuRouwmQCjkYdv4VjuIbvcMZVWSdOm58

                          Best of luck sir! ;)

                          1. Nathan Salefski @nathansalefski
                              2023-07-14 21:15:07.487Z

                              same exact timing lol

                            • T
                              In reply toTiernan_Cranny:
                              Tiernan Cranny @Tiernan_Cranny
                                2023-07-15 08:56:28.332Z

                                Thank you all for your help. I am very new to this so please forgive me for being so green. i will try these out and hopefully learn from these new codes