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