1
votes

I would like to create an applescript that will create multiple folders with the same root name but the numbers change? or at least a repeating folder creation script until the person has enough folders. So something that makes folders like this: JOYR-15-0035-00, JOYR-15-0036-00, JOYR-15-0037-00 and so on. Is that at all possible? I am just learning this. I am normally a graphic designer but I feel like I can get a lot from applescript.

Currently I just have this basic script:

tell application "Finder"
    set KDID to text returned of (display dialog "Enter the KDID ID:" default answer "JOYR-")
    set loc to choose folder "Choose Parent Folder Location"
    set newfoldername to {name:KDID}
    set newfo to make new folder at loc with properties {name:KDID}
    reveal newfo
end tell
1

1 Answers

0
votes

Try this, it assumes that the KDID is just the number 15 in the example, the syntax is always JOYR-<KDID>-<consecutive number>-00 and the leading JOYR as well as the trailing double zero don't change.

The script asks for the parent folder, the KDID and the number of sequential folders. Then it checks the parent folder for the greatest existing number (the 0035 part) and creates folders starting with the greatest number plus 1 or – if no existing folders are found – with 1. The number has always four digits.

property letterPrefix : "JOYR"
property KDID : "15"
property parentFolder : missing value

set parentFolder to choose folder "Choose Parent Folder Location"
tell application "Finder"
    activate
    set KDID to text returned of (display dialog "Enter the KDID ID:" default answer "15")
    repeat
        set howManyFolders to text returned of (display dialog "Enter the Number of Folders to create:" default answer "1")
        try
            set howManyFolders to howManyFolders as integer
            if howManyFolders < 1 then error
            exit repeat
        on error
            display dialog "Please enter an integer value greater than 0" default answer "1"
        end try
    end repeat
    set currentNumber to my getGreatestFolderNumber()
    repeat howManyFolders times
        set folderName to letterPrefix & "-" & KDID & "-" & my pad(currentNumber) & "-00"
        make new folder at parentFolder with properties {name:folderName}
        set currentNumber to currentNumber + 1
    end repeat
    open parentFolder
end tell

on getGreatestFolderNumber()
    tell application "Finder"
        set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "-"}
        try
            set folderNames to name of folders of parentFolder whose name starts with (letterPrefix & "-" & KDID & "-")
            set maxNumber to 0
            repeat with aName in folderNames
                set curNumber to (text item 3 of aName) as integer
                if curNumber > maxNumber then set maxNumber to curNumber
            end repeat
            set AppleScript's text item delimiters to ASTID
            return maxNumber + 1
        on error
            set AppleScript's text item delimiters to ASTID
            return 1
        end try
    end tell
end getGreatestFolderNumber

on pad(v)
    return text -4 thru -1 of ("000" & v)
end pad