Increment folder number if exists

I never used networks and refuse to own a smartphone so I guess that what you ask is outside my area of expertise.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 22 juin 2020 18:53:09

Sorry one last thing.

If for instance I had a client called: ‘The Bluechip company’

At the moment the folder will be created as ‘(BLUE01) The Bluechip Company’.

Is it possible that IF the name contains ‘The’ move it to the back i.e. ‘(BLUE01) Bluechip Company, The’.

Other than that, your script is perfect.

I realise Shane’s intention was simply to illustrate the transform, but this version of the handler’s possibly a bit more robust in practice:

use AppleScript version "2.5" -- macOS 10.11 or later only
use framework "Foundation"
use scripting additions

cleanTheText("The Éclairs R Us")
on cleanTheText(theText)
	if ((theText begins with "The ") and ((count theText) > 4)) then set theText to text 5 thru -1 of theText
	set aString to current application's NSString's stringWithString:theText
	set theText to (aString's stringByApplyingTransform:"Any-Latin; ASCII; Upper; [^A-Z0-9] Remove" |reverse|:false) as text
	if ((count theText) < 4) then return false -- Or some other error token.
	return text 1 thru 4 of theText
end cleanTheText

Thank you

I’ll give that a go.

In the meantime, due to the change in code part of my old code that worked no longer fits.

I am trying to check whether the customer exists and display an error if it does. Then copy a folder from a template location and rename it, here’s my the code:


tell application "Finder"
	if exists folder ClientDestinationFolderString then
		display dialog "That client already exists!"
	else
		do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED
	end if
end tell

Here’s the new code


tell application "System Events"
	set maybe to name of every folder of folder CurrentDir whose name contains BaseName
	set fullName to combined & space & BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ")"
	make new folder at end of folder CurrentDir with properties {name:fullName}
	move ClientTemplatePath to ClientsDestinationPath
	
end tell

would be better to code:

tell me to do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED

because you are triggering a function belonging to a scripting addition in a tell application block.

As I am not a sooth sayer, I can’t guess what are the variables
combined
CurrentDir
ClientTemplatePath
ClientsDestinationPath
supposed to contain.
I just have a vague idea about CurrentDir.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 23 juin 2020 16:39:32

Apologies, I didn’t think.

Here is the full (WORKING) script.

Issues I have found are:

• I don’t want two companies of the same name
Currently if you add a company and add the company again, it will simply increase the number of the code. I need this to check for the business name exists first. This need to be checked against two locations which are: ClientsArchivePath & ClientsDestinationPath

• I need to copy a the contents of a template folder
When the client folder is created, I need to copy the contents over from: ClientTemplatePath
Or simply copy the folder to the new location and then rename it.

I was doing this using the following code:

The old code I used to determine if a folder existed and copy the template folder


tell application "Finder"
	if exists folder ClientDestinationFolderString then
		display dialog "That client already exists!"
	else
		do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED
	end if
end tell

But I would like to do it without using Finder if possible as mentioned on here due to the speed.

Here is the full code


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

--> Needed for Git
set UserEmail to quoted form of "name@email.com"
set UserName to quoted form of "Full Name"

--> Physical URL Path of the Client Template Folder
set ClientTemplatePath to "Volumes:GoogleDrive:My Drive:Work:Resources:Templates:OS Folders:Client Template:"

--> Physical URL Path of the Project Template Folder
set ProjectTemplatePath to "Volumes:GoogleDrive:My Drive:Work:Resources:Templates:OS Folders:Project Template:"

--> Physical URL Path of the Client Archived Folder
set ClientsArchivePath to "Google Drive:My Drive:Work:Clients (Archived):"

--> Physical URL Path of the Local Client Folder
set ClientsDestinationPath to "Macintosh HD:Users:daniel:Documents:Clients:"

--> Get Current Folder Path (CurrentPath) and Name (ClientsFolder)
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 Name
	set ClientsFolder to name of folder CurrentDir
	--> Get Current Folder path
	set CurrentPath to CurrentDir
end tell

--> Prompt to Choose a Client Name.
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")

set CapitalizedText to ((current application's NSString's stringWithString:ClientNamePrompt)'s capitalizedString()) as string

--> Word Exraction
set allWords to words of CapitalizedText
set someWords to {}
set otherWords to {}
repeat with aWord in allWords
	set aWord to aWord as string
	if not aWord is "The" then set end of someWords to aWord
	if aWord is "The" then set end of otherWords to aWord
end repeat
someWords --> {"theorem", "of", "ƒPythagoras", "€"}
set someText to my recolle(someWords, space)
set otherText to my recolle(otherWords, space)
set other to someText
set thetext to otherText
set combined to other & ", " & thetext

--> Start of the cleaning process and creation of the client folder
set BaseName to "(" & my cleanTheText(combined)
tell application "System Events"
	set maybe to name of every folder of folder CurrentDir whose name contains BaseName
	set fullName to combined & space & BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ")"
	if CurrentDir contains combined then
		display dialog "That client already exists!"
	else
		display dialog "That client dpoes not exists!"
		make new folder at end of folder CurrentDir with properties {name:fullName}
	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

--> 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)
	end try
end cleanTheText

I apologizes but I didn’t became a sooth sayer since my late message.

What is the variable ClientDestinationFolderString supposed to contain ?
Is it the path to the folder created by the instruction:

make new folder at end of folder CurrentDir with properties {name:fullName}

I wish to add some comments about the block of instructions:

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 theorem of : ƒPythagoras # / the €"

set CapitalizedText to ((current application's NSString's stringWithString:ClientNamePrompt)'s capitalizedString()) as string--> "THE THEOREM OF : ƒPYTHAGORAS # / THE €"

--> Word Exraction
set allWords to words of CapitalizedText --> {"THE", "THEOREM", "OF", "ƒPYTHAGORAS", "THE", "€"}
set someWords to {}
set otherWords to {}
repeat with aWord in allWords
	set aWord to aWord as string
	if not aWord is "The" then set end of someWords to aWord
	if aWord is "The" then set end of otherWords to aWord
end repeat
someWords --> {"THEOREM", "OF", "ƒPYTHAGORAS", "€"}
otherWords --> {"THE", "THE"}
set someText to my recolle(someWords, space) --> "THEOREM OF ƒPYTHAGORAS €"
set otherText to my recolle(otherWords, space) --> "THE THE"
set other to someText --> "THEOREM OF ƒPYTHAGORAS €"
set theText to otherText --> "THE THE"
set combined to other & ", " & thetext --> "THEOREM OF ƒPYTHAGORAS €, THE THE"

At this time, if ClientNamePrompt contain several times the word “The”,
combined will contain several times the word “THE”.
Is it really useful ?
If it’s not the block may be replaced by:

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 theorem of : ƒPythagoras # / the €"

set CapitalizedText to ((current application's NSString's stringWithString:ClientNamePrompt)'s capitalizedString()) as string --> "THE THEOREM OF : ƒPYTHAGORAS # / THE €"

--> Word Exraction
set allWords to words of CapitalizedText --> {"THE", "THEOREM", "OF", "ƒPYTHAGORAS", "THE", "€"}
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 --> {"THEOREM", "OF", "ƒPYTHAGORAS", "€"}
set combined to my recolle(someWords, space) --> "THEOREM OF ƒPYTHAGORAS €"
if theText ≠ "" then set combined to combined & ", " & theText --> "THEOREM OF ƒPYTHAGORAS €, THE" 

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 23 juin 2020 19:33:33

Thank you.

I’m almost ready to post the full code but I need to add another folder location to the following line so it checks two folders.


set maybe to name of every folder of folder ClientsDestinationPath whose name contains BaseName

The following code works:


tell application "System Events"
set maybe to name of every folder of folder ClientsDestinationPath whose name contains BaseName
end tell

And so does this:


tell application "System Events"
set maybe to name of every folder of folder ClientsArchivedPath whose name contains BaseName
end tell

I have tried the following but it won’t work


tell application "System Events"
set maybe to name of every folder of folder ClientsDestinationPath & ClientsArchivedPath whose name contains BaseName
end tell

I have also tried:


tell application "System Events"
set maybe to name of every folder of folder ClientsDestinationPath & name of every folder of folder ClientsArchivedPath whose name contains BaseName
end tell

We aren’t allowed to grab some content of two folders as you tried to do.
A correct syntax would be:

set p2d to path to desktop as string
set ClientsDestinationPath to p2d & "ClientsDestinationPath"
set ClientsArchivedPath to p2d & "ClientsArchivedPath"
set BaseName to "Tagada"
tell application "System Events"
	set maybe to name of every folder of folder ClientsDestinationPath whose name contains BaseName
	set maybe to maybe & (name of every folder of folder ClientsArchivedPath whose name contains BaseName)
end tell
log maybe (*third column Tagada, fourth column Tagada, third column Tagada, fourth kolumn Tagada*)

-- or the shorter code which will do the same actions at execution time.
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)
end tell
log maybe (*third column Tagada, fourth column Tagada, third column Tagada, fourth kolumn Tagada*)

-- as you see, you can't make the difference with names belonging to each folder.
-- I don't know what is your real goal. It may be better to use:
tell application "System Events"
	set maybe to (path of every folder of folder ClientsDestinationPath whose name contains BaseName) & (path of every folder of folder ClientsArchivedPath whose name contains BaseName)
end tell
maybe --> {"SSD 1000:Users:**********:Desktop:ClientsDestinationPath:third column Tagada:", "SSD 1000:Users:**********:Desktop:ClientsDestinationPath:fourth column Tagada:", "SSD 1000:Users:**********:Desktop:ClientsArchivedPath:third column Tagada:", "SSD 1000:Users:**********:Desktop:ClientsArchivedPath:fourth kolumn Tagada:"}

You didn’t answered the question:
What is the variable ClientDestinationFolderString supposed to contain ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 24 juin 2020 15:06:27

ClientDestinationFolderString was an old variable which I forgot to take out.

Is was originally the url path for the client destination folder.

The script now works perfect thank you so much for your help. I’m just testing it a little now to make sure it does everything I need and I’ll post it up shortly.

I’m now working on a script for a child folder and I would like to name that folder using the 4digit code and number that was created in the previous script.

Basically the url is: Clients/CLIENT-NAME/Projects/PROJECT-NAME

I have managed to get the name from the parent container using the following code:


set ParentPathName to my ParentPathExtraction(CurrentPath)

on ParentPathExtraction(ParentObject)
	tell application "System Events"
		return name of container of disk item (path of disk item (ParentObject as text))
	end tell
end ParentPathExtraction

Which gives me the parents filename which in this case is: New Wave (NEWW01)

I want to take (NEWW01) and name my project folder with it.

Looking at the code you’re helped me with, I know how to extract certain text I know i.e. “The” but I can’t work out how to extract text that I don’t. The only thing I do know is that I want everything between ( and )

-- version 1
set fileName to "New Wave (NEWW01)"
set baseName to item 2 of my decoupe(fileName, {"(", ")"}) --> "NEWW01"
-- but it may return a wrong result
set fileName to "New (blahblah) Wave (NEWW01)"
set baseName to item 2 of my decoupe(fileName, {"(", ")"}) --> "blahblah"
-- so need more steps
set splitted to my decoupe(fileName, {"(", ")"}) --> {"New ", "blahblah", " Wave ", "NEWW01", ""}
set baseName to item (((count splitted) div 2) * 2) of splitted
-- and it's not perfect
set fileName to "New (z(blahblah) Wave (NEWW01)"
set splitted to my decoupe(fileName, {"(", ")"}) --> {"New ", "blahblah", " Wave ", "NEWW01", ""}
set baseName to item (((count splitted) div 2) * 2) of splitted --> ""

-- version 2
set backwards to reverse of text items of fileName --> {")", "1", "0", "W", "W", "E", "N", "(", " ", "e", "v", "a", "W", " ", ")", "h", "a", "l", "b", "h", "a", "l", "b", "(", "z", "(", " ", "w", "e", "N"}
set myChars to {}
repeat with i from 2 to count backwards
	set aChar to item i of backwards as string
	if aChar is "(" then
		exit repeat
	else
		set end of myChars to aChar
	end if
end repeat
myChars --> {"1", "0", "W", "W", "E", "N"}
-- you wrote : I want everything between ( and )
set baseName to my recolle(reverse of myChars, {""}) -->"NEWW01"
-- if you want to get the paretheses, add one instruction
set baseName to "(" & baseName & ")" -- ADDED

#=====

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

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 24 juin 2020 21:29:57

@Yvan

Thank you.

Version 1 looks so much cleaner but I understand why I should use version 2.

What does the following mean just so I understand the code?


set backwards to reverse of text items of fileName --> {")", "1", "0", "W", "W", "E", "N", "(", " ", "e", "v", "a", "W", " ", ")", "h", "a", "l", "b", "h", "a", "l", "b", "(", " ", "w", "e", "N"}

and

myChars --> {"1", "0", "W", "W", "E", "N"}

Also, I would like to keep the () if that is possible?

The updated version 1 feels like the best way to go for me at least.

From my point of view, the version 2 is the better because it can’t return a wrong value.

I edited my script so that it offers the way to return the parentheses, which is not what you asked for.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 24 juin 2020 22:31:02

Even better code:


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



set theString to "blah( az (AZER01) tagada (QWER01) (truc)"

set pattern to "\\(([A-Z]{4}[0-9]{2})\\)" -- was "\\(([^(]{6})\\)"
set regex to (current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|:(missing value))
set cocoaString to (current application's NSString's stringWithString:theString)
set matches to (regex's matchesInString:theString options:0 range:{location:0, |length|:(count theString)})

-- version 3
set partialResults to {}
set fullResults to {}
repeat with aMatch in matches
	set end of partialResults to (cocoaString's substringWithRange:(aMatch's range())) as string
end repeat
set baseName to item -1 of partialResults --> "(QWER01)"

--version 4
repeat with aMatch in matches
	set baseName to (cocoaString's substringWithRange:(aMatch's range())) as string
end repeat
baseName --> "(QWER01)"

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 25 juin 2020 02:07:40

I love it and works perfect.

I’ve tried adding it to my code and it breaks. I believe it’s down to the following line of code:


set matches to (regex's matchesInString:theString options:0 range:{location:0, |length|:(count theString)})

It won’t work within system events i.e. (I’ve taken out the rest of the code to make it easier to see what I mean)


tell application "System Events"
if CurrentPathName = "Projects" then
set matches to (regex's matchesInString:theString options:0 range:{location:0, |length|:(count theString)})
else
display dialog "Open your Clients 'Projects' folder first" buttons {"OK"} default button "OK" with icon caution --or use icon stop
end if
end tell

When I post a script, every instruction is useful but you removed most of them and you added useless others.
Don’t be surprised by error messages.
Below is a correct code:

use AppleScript version "2.4" -- required
use framework "Foundation" -- to use
use scripting additions -- ASObjc


set theString to "blah( az (AZER01) tagada (QWER01) (truc)"

set CurrentPathName to "Projects"
--tell application "System Events" -- USELESS
if CurrentPathName = "Projects" then
	-- define the pattern used by the regular expression
	set pattern to "\\(([A-Z]{4}[0-9]{2})\\)" -- was "\\(([^(]{6})\\)"
	-- build the regular expression
	set regex to (current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|:(missing value))
	-- call the regex to search the substring mathching the given pattern
	set matches to (regex's matchesInString:theString options:0 range:{location:0, |length|:(count theString)})
	-- matches is an object which can't be used directly by AppleScript
	-- convert the AppleScript string object into an NSString which is the official class used by ASObjC
	set cocoaString to (current application's NSString's stringWithString:theString)
	repeat with aMatch in matches
		-- extract the Applescript's substrings
		set baseName to (cocoaString's substringWithRange:(aMatch's range())) as string
		log baseName
		(*(AZER01)*)
		(*(QWER01)*)
	end repeat
	baseName --> "(QWER01)"
else
	display dialog "Open your Clients 'Projects' folder first" buttons {"OK"} default button "OK" with icon caution --or use icon stop
end if
--end tell -- USELESS

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 25 juin 2020 18:32:43

Fair enough. I removed the notes to make it less compact so I could read it easier, I didn’t realise they were there to help.

I’m still learning the code so I am constantly breaking down the code and trying to build it back up again so I understand it.

All in all, thank you for your help. The scripts work perfect and they allow me to create client folders with ease so they all look and feel the same as each other. The second script then does the same for my projects folders.

It might be overkill but I like everything looking the same and if I can create folders with a single touch then it’s a step in the right direction.

Here are the full working scripts

Both these scripts are added to my toolbar in Finder.
Script 1: This creates a new client folder in: Users/User/Documents/Clients and add the 6 digit code.
Script 2: This creates a new project but you must be in the project file for it to work


--> New Client
--> VERSION 3

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

--> 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:"

--> 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 theorem of : ƒPythagoras # / the €"

set CapitalizedText to ((current application's NSString's stringWithString:ClientNamePrompt)'s capitalizedString()) as string --> "THE THEOREM OF : ƒPYTHAGORAS # / THE €"

--> Word Exraction
set allWords to words of CapitalizedText --> {"THE", "THEOREM", "OF", "ƒPYTHAGORAS", "THE", "€"}
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 --> {"THEOREM", "OF", "ƒPYTHAGORAS", "€"}
set CompiledName to my recolle(someWords, space) --> "THEOREM OF ƒPYTHAGORAS €"
if theText ≠ "" then set CompiledName to CompiledName & ", " & theText --> "THEOREM OF ƒPYTHAGORAS €, THE"

--> Start of the cleaning process and creation of the client folder
set BaseName to "(" & my cleanTheText(CompiledName)
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)
	
	set fullNameIncCode to CompiledName & space & BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ")"
	tell application "Finder"
		set dnnn to quoted form of POSIX path of (ClientsDestinationPath & fullNameIncCode)
	end tell
	
	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 to do shell script "cp -R " & ClientTemplatePath & space & dnnn
		display notification "Client created in Clients (Local)"
	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

--> 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


--> New Project
--> VERSION 5

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

--> 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:"

--The name of the folder where you're keeping your projects
set ProjectsFolderName to "Projects"

--The name of the folder where you're keeping your Website Code
set CodeFolderName to "Code"

--> 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

--> Get Current Paths folder name
set CurrentPathName to my CurrentPathExtraction(CurrentPath)

on CurrentPathExtraction(CurrentObject)
	tell application "System Events"
		return name of disk item (path of disk item (CurrentObject as text))
	end tell
end CurrentPathExtraction


if CurrentPathName = "Projects" then
	set ProjectNamePrompt to text returned of (display dialog "Please enter your Project Name:" default answer "" buttons {"Cancel", "Create"} default button "Create" cancel button "Cancel") --> "Project name"
	
	set CapitalizedText to ((current application's NSString's stringWithString:ProjectNamePrompt)'s uppercaseString()) as string --> "THE THEOREM OF : ƒPYTHAGORAS # / THE €"
	
	--> Word Exraction
	set ProjectallWords to words of CapitalizedText --> {"THE", "THEOREM", "OF", "ƒPYTHAGORAS", "THE", "€"}
	set ProjectsomeWords to {}
	set ProjecttheText to ""
	repeat with ProjectaWord in ProjectallWords
		set ProjectaWord to ProjectaWord as string
		if ProjectaWord is "THE" then
			set ProjecttheText to ProjectaWord
		else
			set end of ProjectsomeWords to ProjectaWord
		end if
	end repeat
	ProjectsomeWords --> {"THEOREM", "OF", "ƒPYTHAGORAS", "€"}
	set ProjectCompiledName to my recolle(ProjectsomeWords, space) --> "THEOREM OF ƒPYTHAGORAS €"
	if ProjecttheText ≠ "" then set ProjectCompiledName to ProjectCompiledName & ", " & ProjecttheText --> "THEOREM OF ƒPYTHAGORAS €, THE"
	
	--> Get 4 digit code from parent folder
	set ParentPathName to my ParentPathExtraction(CurrentPath)
	
	--> Word Exraction
	set ParentallWords to words of ParentPathName --> {"THE", "THEOREM", "OF", "ƒPYTHAGORAS", "THE", "€"}
	set ParentsomeWords to {}
	set ParenttheText to ""
	repeat with ParentaWord in ParentallWords
		set ParentaWord to ParentaWord as string
		if ParentaWord is "THE" then
			set ParenttheText to ParentaWord
		else
			set end of ParentsomeWords to ParentaWord
		end if
	end repeat
	ParentsomeWords --> {"THEOREM", "OF", "ƒPYTHAGORAS", "€"}
	set ParentCompiledName to my recolle(ParentsomeWords, space) --> "THEOREM OF ƒPYTHAGORAS €"
	if ParenttheText ≠ "" then set ParentCompiledName to ParentCompiledName & ", " & ParenttheText --> "THEOREM OF ƒPYTHAGORAS €, THE"
	
	set ParentPathName to "blah( az (AZER01) tagada (QWER01) (truc)"
	
	-- define the pattern used by the regular expression
	set pattern to "\\(([A-Z]{4}[0-9]{2})\\)" -- was "\\(([^(]{6})\\)"
	-- build the regular expression
	set regex to (current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|:(missing value))
	-- call the regex to search the substring mathching the given pattern
	set matches to (regex's matchesInString:ParentPathName options:0 range:{location:0, |length|:(count ParentPathName)})
	-- matches is an object which can't be used directly by AppleScript
	-- convert the AppleScript string object into an NSString which is the official class used by ASObjC
	set cocoaString to (current application's NSString's stringWithString:ParentPathName)
	repeat with aMatch in matches
		-- extract the Applescript's substrings
		set baseName to (cocoaString's substringWithRange:(aMatch's range())) as string
		log baseName
		(*(AZER01)*)
		(*(QWER01)*)
	end repeat
	baseName --> "(QWER01)"
	
	tell application "System Events"
		set maybe to name of every folder of folder CurrentPath
		
		set FolderCount to text -4 thru -1 of ((10001 + (count maybe)) as string)
		
		tell application "Finder"
			set ProjectFolderPath to quoted form of POSIX path of (CurrentPath & baseName & "-" & FolderCount & ", " & ProjectCompiledName)
			set ProjectFolderName to quoted form of (baseName & "-" & FolderCount & ", " & ProjectCompiledName)
			
		end tell
		tell me to do shell script "cp -R " & ProjectTemplatePath & space & ProjectFolderPath
		display notification "Project Created in " & ParentPathName
		
	end tell
	-- GET CODE FOLDER PATH
	set CodeDestinationPath to quoted form of (CurrentPath & baseName & "-" & FolderCount & ", " & ProjectCompiledName & "/" & CodeFolderName)
	
	--display dialog CodeDestinationPath
	ProjectCompiledName
	-- INITIALISE GIT
	do shell script "cd " & ¬
		CodeDestinationPath & ¬
		space & ¬
		"&& git init" & ¬
		space & ¬
		"&& git config --global user.email " & ¬
		UserEmail & ¬
		space & ¬
		"&& git config --global user.name " & ¬
		UserName & ¬
		space & ¬
		"&& git add ." & ¬
		space & ¬
		"&& git commit -m 'Initial commit. Added the code file for" & " " & ¬
		ParentPathName & " and the " & ¬
		ProjectCompiledName & "project." & "'"
	
	
else
	display dialog "Open your Clients 'Projects' folder first" buttons {"OK"} default button "OK" with icon caution --or use icon stop
end if


on ParentPathExtraction(ParentObject)
	tell application "System Events"
		return name of container of disk item (path of disk item (ParentObject as text))
	end tell
end ParentPathExtraction

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(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

Hi

I was adding new clients yesterday using script 1 and I came up with a couple of changes I would like to make.

#1
If the business name’s first word starts with 4 random characters or less, I would like to make them uppercase. I don’t think it’s possible to work out what are ‘actual’ words and what are random so I thought what if I manually type in part of the client name I want in uppercase?

At the moment, script 1 will take the input and convert the words to start with a capital letter and everything else will be lower case. Here’s some example of what I mean.

Company Name 1 example:
Actual Name: the kite company
Required result: Kite Company, The (KITE01)

Company Name 2 example:
Actual Name: abc company
Required result: ABC Company (ABCC01)

Company Name 3 example:
Actual Name: ynwa company
Required result: YNWA Company (YNWA01)

Company Name 4 example:
Actual Name: the ynwa company
Required result: YNWA Company (YNWA01)

#2
I found when adding clients, clients name worked well but when I added an individual then I came into a problem. When adding individuals I like to start with the surname comma then the first name i.e. Surname, Firstname

Currently script one removes the commas.

As I say the scripts work perfectly well but just a couple changes I want to make

I can’t guess what may allow us to decide that in example 1 the article “the” must be kept but that it must be dropped in example 4.

Below is an edited version:

-- Danjuan
--> New Project
--> VERSION 5

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 ProjectTemplatePath to quoted form of POSIX path of ((path to desktop as string) & "Project Template:")
	set currentDir to (path to desktop as string) & "Client Surname, Firstname, The (CLIE01):Projects:"
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:"
	
	--The name of the folder where you're keeping your projects
	set ProjectsFolderName to "Projects"
	
	--The name of the folder where you're keeping your Website Code
	set CodeFolderName to "Code"
	
	--> 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) -- no need for this coercion here
		set CurrentPathName to my CurrentPathExtraction(currentDir)
		-- Get parent folder name
		set ParentPathName to my ParentPathExtraction(currentDir) -- instruction moved here
	end tell
end if


-- Get Current Paths folder name
set CurrentPathName to my CurrentPathExtraction(currentDir) --> "Projects"
-- Get parent folder name
set ParentPathName to my ParentPathExtraction(currentDir) -- instruction moved here
--> "Client Surname, Firstname, The (CLIE01)"

if CurrentPathName = "Projects" then
	--set ProjectNamePrompt to text returned of (display dialog "Please enter your Project Name:" default answer "" buttons {"Cancel", "Create"} default button "Create" cancel button "Cancel") --> "Project name"
	set ProjectNamePrompt to "the surname, firstname company"
	set CapitalizedText to ((current application's NSString's stringWithString:ProjectNamePrompt)'s capitalizedString()) as string -- EDITED
	--> "The Surname, Firstname Company"
	
	--> Word Extraction
	(*
	-- old version
	set ProjectallWords to words of CapitalizedText -->{"The", "Surname", "Firstname", "Company"}
	*)
	-- new version
	set ProjectallWords to my decoupe(CapitalizedText, space) --> {"The", "Surname,",  "Firstname", "Company"}
	
	set ProjectsomeWords to {}
	set ProjecttheText to ""
	repeat with ProjectaWord in ProjectallWords
		set ProjectaWord to ProjectaWord as string
		if ProjectaWord is "The" then
			set ProjecttheText to ProjectaWord
		else
			set end of ProjectsomeWords to ProjectaWord
		end if
	end repeat
	ProjectsomeWords --> {"Surname,", "Firstname", "Company"}
	set ProjectCompiledName to my recolle(ProjectsomeWords, space) --> "Surname, Firstname Company"
	if ProjecttheText ≠ "" then set ProjectCompiledName to ProjectCompiledName & ", " & ProjecttheText --> "Surname, Firstname Company, The"
	
	--> Get 4 digit code from parent folder
	-- set ParentPathName to my ParentPathExtraction(CurrentDir) -- moved above
	--> Word Extraction
	
	-- Here, ParentPathName is supposed to be correctly capitalized.
	
	(*
	-- old version
	-- set ParentAllWords to words of ParentPathName --> {"Client", "Surname", "Firstname,", "The", "(CLIE01)"}
	*)
	-- new version
	set ParentAllWords to my decoupe(ParentPathName, space) --> {"Client", "Surname,", "Firstname,", "The", "(CLIE01)"}
	set ParentsomeWords to {}
	set ParentTheText to ""
	repeat with ParentaWord in ParentAllWords
		set ParentaWord to ParentaWord as string
		if ParentaWord is "The" then
			set ParentTheText to ParentaWord
		else
			set end of ParentsomeWords to ParentaWord
		end if
	end repeat
	ParentsomeWords --> {"Client", "Surname,", "Firstname,", "(CLIE01)"}
	
	set ParentCompiledName to my recolle(ParentsomeWords, space) --> "Client Surname, Firstname, (CLIE01)"
	if ParentTheText ≠ "" then set ParentCompiledName to ParentCompiledName & ", " & ParentTheText --> "Client Surname, Firstname, (CLIE01), The"
	
	-- What need to build ParentCompiledName which is not used below ?
	
	-- define the pattern used by the regular expression
	set pattern to "\\(([A-Z]{4}[0-9]{2})\\)" -- was "\\(([^(]{6})\\)"
	-- build the regular expression
	set regex to (current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|:(missing value))
	-- call the regex to search the substring mathching the given pattern
	set matches to (regex's matchesInString:ParentPathName options:0 range:{location:0, |length|:(count ParentPathName)})
	-- matches is an object which can't be used directly by AppleScript
	-- convert the AppleScript string object into an NSString which is the official class used by ASObjC
	set cocoaString to (current application's NSString's stringWithString:ParentPathName)
	repeat with aMatch in matches
		-- extract the Applescript's substrings
		set baseName to (cocoaString's substringWithRange:(aMatch's range())) as string
	end repeat
	baseName --> "(CLIE01)"
	
	tell application "System Events"
		set maybe to name of every folder of folder currentDir
	end tell --  "System Events"
	-- Now we have no reason to speak to System Events
	set FolderCount to text -4 thru -1 of ((10001 + (count maybe)) as string)
	
	-- tell application "Finder"
	-- there is no reason to speak, to Finder for these two instructions.
	-- worse they are using features belonging to a script addition
	set ProjectFolderPath to quoted form of POSIX path of (currentDir & baseName & "-" & FolderCount & ", " & ProjectCompiledName)
	set ProjectFolderName to quoted form of (baseName & "-" & FolderCount & ", " & ProjectCompiledName)
	
	-- end tell
	-- Now that we are no longer speaking to System Events there is no need for the "tell me to' part
	-- tell me to do shell script "cp -R " & ProjectTemplatePath & space & ProjectFolderPath
	do shell script "cp -R " & ProjectTemplatePath & space & ProjectFolderPath
	display notification "Project Created in " & ParentPathName
	
	--end tell
	
	
	-- I can't test the git part
	(*
	-- GET CODE FOLDER PATH
	set CodeDestinationPath to quoted form of POSIX path of (currentDir & baseName & "-" & FolderCount & ", " & ProjectCompiledName & ":" & CodeFolderName) -- EDITED
	
	--display dialog CodeDestinationPath
	ProjectCompiledName
	-- INITIALISE GIT
	do shell script "cd " & ¬
		CodeDestinationPath & ¬
		space & ¬
		"&& git init" & ¬
		space & ¬
		"&& git config --global user.email " & ¬
		UserEmail & ¬
		space & ¬
		"&& git config --global user.name " & ¬
		UserName & ¬
		space & ¬
		"&& git add ." & ¬
		space & ¬
		"&& git commit -m 'Initial commit. Added the code file for" & " " & ¬
		ParentPathName & " and the " & ¬
		ProjectCompiledName & "project." & "'"
	*)
else
	display dialog "Open your Clients 'Projects' folder first" buttons {"OK"} default button "OK" with icon caution --or use icon stop
end if

#===== Now we aren't in the main code, we may put the handlers

on CurrentPathExtraction(CurrentObject) -- CurrentObject is an Hfs string
	tell application "System Events"
		--return name of disk item (path of disk item (CurrentObject as text))
		return name of disk item CurrentObject
	end tell
end CurrentPathExtraction

#=====

on ParentPathExtraction(ParentObject) -- ParentObject is an Hfs string
	tell application "System Events"
		--return name of container of disk item (path of disk item (ParentObject as text))
		return name of container of disk item ParentObject
	end tell
end ParentPathExtraction

#=====

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) vendredi 26 juin 2020 18:18:02