Create a folder and add row to Airtable

Hi All

I am trying to create a mini CRM for myself. I’m trying to do as much automation as possible and one thing I would like to do is add a clients information with very little manual input to my Airtable database. While doing this I would like to create a client folder where all my work will be stored for that client, this way it all stays in sync.

I have managed to do this in two separate steps. I have created an Automator script on my mac that creates a folder on my local hard drive and I’ve created a IOS Shortcut that adds data to Airtable. I would like to combine these but since Automator isn’t on the iPad and need to work out how to do the first part.

So seeing as the second step is fairly easy I’ll concentrate on the first step. How to create a client folder! I’m not fussed where the folder is created, I tend to use Google Drive but I’ll swap to iCloud if needed seeing as a local folder won’t work.

I’m quire specific on the folder name structure. For instance if I had a client called “Joe Bloggs Builders”, I would want the folder name “Joe Bloggs Builders (JOEB01)”. If I had a client called
“The Pub Group”, I would want the folder Pub Group, The(PUBG01).

Now there are a few extras in the script. It also removes any special characters, stops the script if the client already exists and finally if the 4 digit code already existed, it would increase the number by 1 for instance, if Joe Bloggs Builders (JOEB01) already existed then the script would stop. If I then added Joe Bloggs Carpets it would create the 4 digit code (JOEB02).

Here’s my AppleScript, If I could convert this then that would be the simplest option but not sure it can.

What are my options and do you have any ideas?

--> Changed from all text to upper case to ignore capitals and change the rest to titlecase

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 "Folderpath:Client Template:"

--> Physical URL Path of the Project Template Folder
set ProjectTemplatePath to quoted form of POSIX path of "Folderpath: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:daniel:Documents:Clients:"

--> Use this ClientNamePrompt for testing
--set ClientNamePrompt to "the SURname, firstNAme company"

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") --> "Result is exactly what you input"

(* Make all text capitals, no longer required in version 6
--> Change to uppercaseString to capitalizedString or lowercaseString if required
set CapitalizedText to ((current application's NSString's stringWithString:ClientNamePrompt)'s uppercaseString()) as string --> "The Surname, Firstname Company"
*)

--> Compiles input. Leaves letters that start with capitals and changes everything else to titlecase
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"


--> CLEANS OUR COMPILED NAME AND CREATES THE FIRST PART OF OUR 6 DIGIT CODE
--> Start of the cleaning process and creation of the client folder
set BaseName to "(" & my cleanTheText(CompiledName) --> "(SURN"

--> COUNT THE FOLDERS THAT CONTAIN THE BASENAME
tell application "System Events"
	--> Look for folders that have the contents of the Basename i.e. "(SURN" 
	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

--> CREATES THE SECOND PART OF OUR 6 DIGIT CODE
--> If i.e. i.e. "(SURN" exists, increase the number
--> Take the compiled name, add a space then the basename
set fullNameIncCode to CompiledName & space & BaseName --> "Surname, Firstname Company (SURN"
--> Add text to the end
set fullNameIncCode to CompiledName & space & BaseName & text --> "Surname, Firstname Company (SURNtext"
--> Replace 'text with the number 101' and add the final bracket
set fullNameIncCode to CompiledName & space & BaseName & text of ((101) as string) & ")" --> "Surname, Firstname Company (SURN101)"
--> Remove a digit from the number from 101 to 01
set fullNameIncCode to CompiledName & space & BaseName & text -2 thru -1 of ((101) as string) & ")" --> "Surname, Firstname Company, The (SURN01)"
--> Finally count how many folders contain 'maybe' and replace the number
set fullNameIncCode to CompiledName & space & BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ")" --> "Surname, Firstname Company, The (SURN02)"

--> THIS WILL BE THE PATH TO THE NEW CLIENTS FOLDER
set NewClientPath to quoted form of POSIX path of (ClientsDestinationPath & fullNameIncCode)
--> "'/Users/daniel/Documents/Clients/Surname, Firstname Company (SURN02)'"

tell application "System Events"
	--> Checks if any folders within the main clients (Local) folder containes our new clients name (Once it's been compiled)
	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
		--> Checks if any folders within the clients (Archive) folder containes our new clients name (Once it's been compiled)
	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"
	else
		--> If the compiled name doesn't match the two client folder then continue creating the client
		tell me -- required for two functions belongonging to a scripting addition
			do shell script "cp -R " & ClientTemplatePath & space & NewClientPath
			display notification "Client created in Clients (Local)"
		end tell
	end if
end tell

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

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

#=====