Increment folder number if exists

-- Danjuan & YK
--> New Client
--> VERSION 3

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

property forYK : true
-- true -- version used by YK for tests
-- false -- more or less your code without the git part

if forYK then
	set ClientTemplatePath to quoted form of POSIX path of ((path to desktop as string) & "Client Template:")
	set ClientsArchivedPath to (path to desktop as string) & "Clients (Archived):"
	set ClientsDestinationPath to (path to documents folder as string) & "Clients (Local):"
else
	--> Needed for Git
	set UserEmail to quoted form of "name@email.co.uk"
	set UserName to quoted form of "Full Name"
	
	--> Physical URL Path of the Client Template Folder
	set ClientTemplatePath to quoted form of POSIX path of "Volumes:GoogleDrive:My Drive:Work:Resources:Templates:OS Folders:Client Template:"
	
	--> Physical URL Path of the Project Template Folder
	set ProjectTemplatePath to quoted form of POSIX path of "Volumes:GoogleDrive:My Drive:Work:Resources:Templates:OS Folders:Project Template:"
	
	--> Physical URL Path of the Client Archived Folder
	set ClientsArchivedPath to "Google Drive:My Drive:Work:Clients (Archived):"
	
	--> Physical URL Path of the Local Client Folder
	set ClientsDestinationPath to "Macintosh HD:Users:user:Documents:Clients:"
	(*
	-- several USELESS instructions
	--> Get Current Folder Path (CurrentPath) and Name (CurrentFolder)
	tell application "Finder"
		if exists Finder window 1 then
			set currentDir to target of Finder window 1 as string
		else
			set currentDir to (desktop as string)
		end if
		--> Get Current Folder path
		set CurrentPath to POSIX path of (currentDir)
	end tell
	*)
	set ClientNamePrompt to text returned of (display dialog "Please enter your Clients Business Name:" default answer "" buttons {"Cancel", "Create"} default button "Create" cancel button "Cancel") -->  "the surname, firstname company"
end if

set ClientNamePrompt to "the surname, firstname company"
set CapitalizedText to ((current application's NSString's stringWithString:ClientNamePrompt)'s capitalizedString()) as string -- EDITED
--> "The Surname, Firstname Company"
-- Word Exraction
(*
-- old version
set allWords to words of CapitalizedText --> {"The", "Surname", "Firstname", "Company"}
*)
-- new version
set allWords to my decoupe(CapitalizedText, space) --> {"The", "Surname,", "Firstname", "Company"}
set someWords to {}

set theText to ""
repeat with aWord in allWords
	set aWord to aWord as string
	if aWord is "The" then
		set theText to aWord
	else
		set end of someWords to aWord
	end if
end repeat
someWords --> {"Surname,", "Firstname", "Company"}
set CompiledName to my recolle(someWords, space) --> "Surname, Firstname Company"
if theText ≠ "" then set CompiledName to CompiledName & ", " & theText --> "Surname, Firstname Company, The"

--> Start of the cleaning process and creation of the client folder
set BaseName to "(" & my cleanTheText(CompiledName) --> "(SURN"

tell application "System Events"
	set maybe to (name of every folder of folder ClientsDestinationPath whose name contains BaseName) & (name of every folder of folder ClientsArchivedPath whose name contains BaseName)
	--> {"Surname, Firstname Company, The (SURN01)"}
end tell -- no longer speak to System Events
set fullNameIncCode to CompiledName & space & BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ")" --> "Surname, Firstname Company, The (SURN02)"

--tell application "Finder"
set dnnn to quoted form of POSIX path of (ClientsDestinationPath & fullNameIncCode)
--> "'/Users/yvankoenig/Documents/Clients/Surname, Firstname Company, The (SURN02)'"
-- end tell
tell application "System Events"
	if exists (name of every folder of folder ClientsDestinationPath whose name contains CompiledName) then
		display dialog "That client already exists in Clients (Local)" buttons {"OK"} default button "OK" with icon caution --or use icon stop
	else if exists (name of every folder of folder ClientsArchivedPath whose name contains CompiledName) then
		display dialog "That client already exists in Clients (Archived)" buttons {"OK"} default button "OK" with icon caution --or use icon stop
	else
		tell me -- required for two functions belongonging to a scripting addition
			do shell script "cp -R " & ClientTemplatePath & space & dnnn
			display notification "Client created in Clients (Local)"
		end tell
	end if
end tell

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

--> Removes the special characters from ClientNamePrompt
on cleanTheText(theText)
	-- handler designed by Shane Stanley
	set aString to current application's NSString's stringWithString:theText
	if aString's hasPrefix:"The " then set aString to aString's substringFromIndex:4
	set aString to aString's stringByApplyingTransform:"Any-Latin; ASCII; Upper; [^A-Z0-9] Remove" |reverse|:false
	try -- errors if string too short
		return text 1 thru 4 of (aString as text)
	on error
		display dialog "The Client Name does not contain four characters." buttons {"OK"} cancel button 1 default button 1 with icon stop
	end try
end cleanTheText

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 27 juin 2020 11:59:34

Thank you so much for doing that, I hadn’t got round to looking at the code so that saves me a lot of time.

The final adjustment I would like to make is ‘If the input name is a capital letter, don’t make any adjustments to those letters’

i.e.
Input name: the CNC company:
Result: CNC Company, The

or
Input name: the NEW WAVE company:
Result: NEW WAVE Company, The

Sometime I have clients that just use initials and I find it hard to spot when looking for i.e. Cnc Company, The when CNC Company, The is just easier for me to read.

Really boring kind of asker’s behavior.
Was it difficult to describe your needs in a single question ?

Happily, we can’t use colors in file names.
Otherwise a next step would be : oh I forgot to ask that this component appear in blue.
And two days later, oops, I forgot to ask that this oter component appear in red,

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set ClientNamePrompt to "the SURNAME, FIRstname company"
set allWords to my decoupe(ClientNamePrompt, space) --> {"the", "SURNAME,", "FIRstname", "company"}

set someWords to {}

set theText to ""
repeat with aWord in allWords
	set aWord to aWord as string
	if aWord is "the" then
		set theText to aWord
	else
		set end of someWords to aWord
	end if
end repeat
someWords --> {"SURNAME,", "FIRSTname", "company"}

set CompiledName to my recolle(someWords, space) --> "SURNAME, FIRSTname company"
if theText ≠ "" then set CompiledName to CompiledName & ", " & theText --> "SURNAME, FIRSTname company, the"
CompiledName --> "SURNAME, FIRSTname company, the"

set newWords to my decoupe(CompiledName, space) --> {"SURNAME,", "FIRSTname", "company,", "the"}

set someWords to {}
repeat with aWord in newWords
	set aWord to aWord as string -- required
	considering case
		if aWord = ((current application's NSString's stringWithString:aWord)'s uppercaseString()) as string then -- are they all uppercase ?
			set end of someWords to aWord as string
		else
			set end of someWords to ((current application's NSString's stringWithString:aWord)'s capitalizedString()) as string
		end if
	end considering
end repeat
someWords --> {"SURNAME,", "Firstname", "Company,", "The"}

set CompiledName to my recolle(someWords, space) --> "SURname, Firstname Company, The"


#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

--> Removes the special characters from ClientNamePrompt
on cleanTheText(ProjecttheText)
	-- handler designed by Shane Stanley
	set aString to current application's NSString's stringWithString:ProjecttheText
	if aString's hasPrefix:"The " then set aString to aString's substringFromIndex:4
	set aString to aString's stringByApplyingTransform:"Any-Latin; ASCII; Upper; [^A-Z0-9] Remove" |reverse|:false
	try -- errors if string too short
		return text 1 thru 4 of (aString as text)
	on error
		display dialog "The Client Name does not contain four characters." buttons {"OK"} cancel button 1 default button 1 with icon stop
	end try
end cleanTheText

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 27 juin 2020 16:21:34

I really do appreciate your help and in the future I will try and list everything I need in one question.