Scriptable Script Alerts and issue with deleting folders

Hi

I have a script that creates client folders based on input, the script runs fine but i have a couple of issues.

Firstly, when a customer already exists the script stops as it should but how do I add an alert to say the client already exists? Same again when the script is finished just in case there’s an issue adding it.

Secondly, the scripts creates unique usernames based on the client name followed by a number. The number is determined by counting how many folders have the unique 4 digit codes then adding +1. This works but if i delete a folder then we have an issue where for instance, If i had ABCD01, ABCD02, ABCD03 the next one would be ABCD04, but if i deleted the folder ABCD01, the next one would again be ABCD03 because it would count 3 instances.

Don’t count the instances, determine the highest number and add 1.

If the client equates to a folder you could use an isDirectory() call on the path. If it exists then it should return true, otherwise false. Then base your processing logic and notifications around that result.

Ok, i like that.

How do you determine that? I’m new to this and the first script took me ages.

Also, shortcuts don’t allow alerts from scriptable but shortcuts has it’s own alerts action. Can you go back to that just to show the alert then go back to the script?

Here’s my script, forgot to post it.

One way would be to read in all folder names, strip off the leading characters and then sort the numbers. Unfortunately I don’t think Scriptable has a way to let you list and sort the results chronologically, otherwise you could just pick the most recently created off the sorted list.

If you’re able to count the files with a similar name, you already have some way to find all the folders and filter out only those starting with, say, “ABCD”. You could use something like

let list = [];
folders.forEach(f => list.push(f.name));
let last = list.sort()[list.length-1];
let lastNumber = last.match("ABCD(\d+)")[1];
let nextNumber = parseInt(lastNumber,10)+1;

BTW: If you want to post your script here, it is better to copy its source code (or the relevant parts of it) into your post, enclosed in ``` (three backqoutes). That way, one can easily peruse your code here. If you post a link to your shortcurt, that would require me to install it on my device only to look at the source. Which I can’t be bothered with :wink:

Fair enough, I wasn’t sure which way to do it so posted the whole thing.

I’ll have a look at your posted code.

Thanks

It’s been a while since I did this code, I really should leave more comments.


let fmCloud = FileManager.iCloud() // {}
let strPath = fmCloud.bookmarkedPath("Clients") // /private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/Work/Client
let InputName ="Company Name";



//Create Basename - START
function CreateBasename(InputName) {
    // Change Case - START
    const toCapitaliseCase = (phrase) => {
        return phrase
        .split(' ')
        .map(word => word.charAt(0).toUpperCase() + word.slice(1))
        .join(' ');
    };
    let Capitalise = toCapitaliseCase(InputName);
    // Change Case - END
    // return capitalise; // The ABC Company
    
    // Format Client Name if starts with 'The' - START
    if (Capitalise.startsWith('The ')) { //The ABC Company
        let Words = Capitalise.split(' '); //["The","ABC","Company"]
        let The = Words[0]; // The
        let TheSlice = Capitalise.slice(4); //ABC Company
        let Comma = ', '; // ,
        let BaseName = TheSlice.concat('', Comma, The); // ABC Company, The
        return BaseName //ABC Company, The
    }
    // Format Client Name if it DOESN'T start with 'The' - START
    else { //The ABC Company
        return Capitalise
    }
    
}
var BaseName = CreateBasename(InputName);
//console.log (BaseName) //ABC Company, The
//Create Basename - END

// Says client exists i.e brown group, the and brown group


//Check if a folder with BaseName exists - START
function DoesFolderExist(InputName) {
    let ListContents = fmCloud.listContents(strPath).filter(x => x.includes(BaseName));
    var x = ListContents.toString();
    let hello = fmCloud.isDirectory(strPath);

    if (x.includes(BaseName)) {
        Script.setShortcutOutput('Client already exists')
        console.log('Client already exists')

    }
    else {
        let RemoveSpecials = BaseName.replace(/[^0-9a-zA-Z]/g, ""); // ABCCompanyThe
        let FourDigitCode = RemoveSpecials.slice(0,4); // ABCC
        let UpperCaseCode = FourDigitCode.toUpperCase(); // ABCC
        let code = (' (')
        let newcode = code.concat(UpperCaseCode); // ABC Company, The (ABCC
        let CompiledName = BaseName.concat(newcode); // ABC Company, The (ABCC
        
        let AlreadyExist = fmCloud.listContents(strPath).filter(hhh => hhh.includes(newcode)).length;
        this.number = this.number || AlreadyExist;
        this.number++;
        let Num = this.number;
        if(('' + Num).length == 1) {
            Num = '0' + Num;
        }
        let CompiledNamhghge = BaseName.concat(newcode + Num + ')'); // ABC Company, The (ABCC


        //console.log(CompiledNamhghge)
        
        


        var path = (strPath + "/" + CompiledNamhghge);
        fmCloud.createDirectory(path) //ABC Company, The (ABCC
        Script.setShortcutOutput(CompiledNamhghge)
        console.log(CompiledNamhghge)
    }
};
var FindName = DoesFolderExist(InputName);
//Check if a folder with BaseName exists - END

I just tried adding your code and editing the script but I can’t work it out, if you have any input that would be great.

I still cannot figure this out using your code. Below is the part of code that creates the number and increases it but I can’t work out how to do it using your code and last.match.


let RemoveSpecials = BaseName.replace(/[^0-9a-zA-Z]/g, ""); // ABCCompanyThe
        let FourDigitCode = RemoveSpecials.slice(0,4); // ABCC
        let UpperCaseCode = FourDigitCode.toUpperCase(); // ABCC
        let code = (' (')
        let newcode = code.concat(UpperCaseCode); // (ABCC
        let CompiledName = BaseName.concat(newcode); // ABC Company, The (ABCC
        
        let AlreadyExist = fmCloud.listContents(strPath).filter(hhh => hhh.includes(newcode)).length;
        this.number = this.number || AlreadyExist;
        this.number++;
        let Num = this.number;
        if(('' + Num).length == 1) {
            Num = '0' + Num;
        }
        let CompiledNamhghge = BaseName.concat(newcode + Num + ')');

Any idea how i would edit this code to use yours?