No internet connection
  1. Home
  2. How to

Adding prefixes etc to file names in the finder with soundflow?

By Kris Crunk @Kris_Crunk
    2023-03-09 20:18:14.333Z

    I'm using Soundflow and automator to add prefix etc to file names - can I bypass automator and just use soundflow to do this?

    • 4 replies
    1. Kitch Membery @Kitch2023-03-09 22:09:02.260Z

      Yes, you can... Stand by :-)

      1. Chad Wahlbrink @Chad2023-03-09 22:27:51.047Z2023-03-09 22:44:41.279Z

        @Kitch probably has a more refined method for this, but I just whipped up this renamer

        You can set a prefix or postfix at the top of the script. A "template" script probably makes more sense, but this works for simple pre- and post-fix!

        // Set Prefix or Postfix
        let prefix = ''
        let postfix = ''
        
        // Set finder paths
        let filesSelected = sf.ui.finder.selectedPaths;
        
        // Only have a prefix or postfix
        if (prefix.length > 0 && postfix.length > 0) {
            throw 'Choose prefix or postfix at top of script'
        }
        
        // Activate Finder
        sf.ui.finder.appActivate();
        
        // Function - Rename Files
        function renameFiles(callback) {
            // Click Finder Rename
            sf.ui.finder.menuClick({ menuPath: ['File', 'Rename'], looseMatch: true });
        
            try {
                // Try to rename the files
                callback();
        
            } finally {
                // Make Sure Rename Dialog Disappears
                if (sf.ui.finder.mainWindow.sheets.first.getElement("AXCancelButton").exists) {
                    sf.ui.finder.mainWindow.sheets.first.getElement("AXCancelButton").elementClick();
                }
            }
        }
        
        // Function - Main
        function main() {
            // If more than one file is selected.
            if (filesSelected.length > 1) {
                // Make Sure Finder Rename is set Up to Add Text
                if (!sf.ui.finder.mainWindow.sheets.first.popupButtons.whoseValue.is("Add Text").first.exists) {
                    sf.ui.finder.mainWindow.sheets.first.popupButtons.first.popupMenuSelect({ menuPath: ["Add Text"] });
                }
                // If prefix, set before name
                if (prefix.length > 0) {
                    if (!sf.ui.finder.mainWindow.sheets.first.popupButtons.whoseValue.is("before name").first.exists) {
                        sf.ui.finder.mainWindow.sheets.first.popupButtons.allItems[1].popupMenuSelect({ menuPath: ["before name"] });
                    }
                // If postfix, set after name
                } else if (postfix.length > 0) {
                    if (!sf.ui.finder.mainWindow.sheets.first.popupButtons.whoseValue.is("after name").first.exists) {
                        sf.ui.finder.mainWindow.sheets.first.popupButtons.allItems[1].popupMenuSelect({ menuPath: ["after name"] });
                    }
                }
                // Set text
                sf.ui.finder.mainWindow.sheets.first.textFields.first.elementSetTextFieldWithAreaValue({
                    value: prefix ? prefix : postfix,
                })
                // Click Rename
                sf.ui.finder.mainWindow.sheets.first.buttons.whoseTitle.is("Rename").first.elementClick();
                sf.ui.finder.mainWindow.sheets.first.buttons.whoseTitle.is("Rename").first.elementWaitFor({ waitType: "Disappear" });
            } // If only one file is selected
            else if (filesSelected.length = 1) {
                // Get File Name
                let filename = filesSelected[0].split('/').slice(-1)[0].split('.').slice(0, -1).join(".");
        
                // Set New file name
                let newfilename = prefix ? prefix + filename : filename + postfix;
                
                // Set Clipboard
                sf.clipboard.setText({ text: newfilename });
        
                // Paste new file name
                sf.ui.finder.menuClick({ menuPath: ['Edit', 'Paste'], looseMatch: true });
                
                // press enter
                sf.keyboard.press({keys:'enter'});
            }
        }
        
        // rename files
        renameFiles(main);
        
      2. In reply toKris_Crunk:
        Kitch Membery @Kitch2023-03-09 22:27:58.055Z

        This will allow you to add a prefix and a suffix to selected finder files.

        const prefix = "Prefix_";
        const suffix = "_Suffix";
        
        function updateFinderFileName({ path, prefix = "", suffix = "" }) {
            //Extract File Extension
            const fileExtensionRegex = /\.[^/.]+$/;
            const fileExtension = path.match(fileExtensionRegex).toString();
        
            //Extract filename from path
            const fileName = path.replace(fileExtension, "").split('/').slice(-1).join('/').toString();
        
            //Extract Directory from path
            const directoryPath = path.split('/').slice(0, -1).join('/').toString();
        
            //Create new file path
            const newFilePath = `${directoryPath}/${prefix}${fileName}${suffix}${fileExtension}`;
        
            //Change file path
            sf.file.move({
                sourcePath: path,
                destinationPath: newFilePath,
            });
        }
        
        function main() {
            const confirmation = confirm('Do you want to proceed?');
            if (!confirmation) throw 0
        
            //Get the selected finder file paths.
            let finderFilePaths = sf.ui.finder.selectedPaths.map(path => path);
        
            //For each path add prefix and Suffix to the file name
            finderFilePaths.forEach(path => {
                updateFinderFileName({
                    path,
                    prefix,
                    suffix,
                })
            });
        }
        
        main();
        
        1. In reply toKris_Crunk:
          Kris Crunk @Kris_Crunk
            2023-03-09 22:31:49.442Z

            Sweet thanks - I'll give it a go!