Create a folder if it doesn't already exist.

Hi all

I’ve been playing about with scripts for the last few months. I thought I completed one that worked perfectly for me but after trying it out I can find and issue.

I am trying to create a script that creates a folder. When my current script runs, it throws up a input box for you to add your folder name, it then creates your new folder in the current folder with your chosen name. Finally it grabs some folders from a template folder and add them to the new folder.

This all works as it should, the issue I found is what if I need to create a folder and there is already a folder named the same thing?

So I need to change the following part of my script.


-- CREATE THE FOLDER
	do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED


to something like


tell application "Finder"
	if exists ClientDestinationFolder then
		display dialog "Folder already exists"
	else
		do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED
	end if
end tell

Here’s my full working script without the folder exists part added


-- VARIABLES
set UserEmail to quoted form of "email@email.com" -- ADDED
set UserName to quoted form of "name" -- ADDED
set ClientTemplatepath to quoted form of ("/Desktop/Templates/OS Folders/Client Template") -- Where you've stored the Template folder

-- GET CURRENT FOLDER
tell application "Finder"
	if exists Finder window 1 then
		set CurrentDir to target of Finder window 1 as alias
	else
		set CurrentDir to desktop as alias
	end if
end tell
set CurrentPath to POSIX path of (CurrentDir)
--display dialog CurrentPath

-- GET TEMPLATE FOLDER
-- Display the current windows URL along with the client template's name
--display dialog ClientTemplatepath

-- CONFIGURE NEW FOLDER
set ClientName to text returned of (display dialog "Please enter Business Name:" default answer "" buttons {"Cancel", "Create Client"} default button "Create Client")
--display dialog ClientName
-- Set new folder name
set ClientDestinationFolder to quoted form of POSIX path of (CurrentPath & ClientName) -- ADDED
-- Display the current windows URL along with the client destinations's name
--display dialog ClientDestinationFolder
-- Duplicate the template folder and rename it

-- CREATE THE FOLDER
do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED

-- INITIALISE GIT
do shell script "cd " & ¬
	ClientDestinationFolder & ¬
	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. Files added for" & " - " & ClientName & "'"



You may try to use:


-- VARIABLES
set UserEmail to quoted form of "email@email.com" -- ADDED
set UserName to quoted form of "name" -- ADDED
set ClientTemplatepath to quoted form of ("/Desktop/Templates/OS Folders/Client Template") -- Where you've stored the Template folder

-- GET CURRENT FOLDER
tell application "Finder"
	if exists Finder window 1 then
		set CurrentDir to target of Finder window 1 as alias
	else
		set CurrentDir to desktop as alias
	end if
end tell
set CurrentPath to POSIX path of (CurrentDir)
--display dialog CurrentPath

-- GET TEMPLATE FOLDER
-- Display the current windows URL along with the client template's name
--display dialog ClientTemplatepath

-- CONFIGURE NEW FOLDER
set ClientName to text returned of (display dialog "Please enter Business Name:" default answer "" buttons {"Cancel", "Create Client"} default button "Create Client")
--display dialog ClientName
-- Set new folder name
set ClientDestinationFolder to quoted form of POSIX path of (CurrentPath & ClientName) -- ADDED
-- Display the current windows URL along with the client destinations's name
--display dialog ClientDestinationFolder
-- Duplicate the template folder and rename it

-- CREATE THE FOLDER
tell application "Finder"
	if exists ClientDestinationFolder then
		display dialog "Folder already exists"
	else
		tell me to do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED
	end if
end tell

-- INITIALISE GIT
do shell script "cd " & ¬
	ClientDestinationFolder & ¬
	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. Files added for" & " - " & ClientName & "'"



Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 15 juin 2020 14:49:39

Unfortunately that doesn’t work.

I’m taking it that you just took the ‘exists part of my code’ and added it to my original script.

The exist part I created doesn’t work in my first post doesn’t work, I don’t think it’s far off but I can’t get it over the line.

Honestly, I didn’t spent time to check that your original syntax was correct.
It wasn’t a good idea because your original 3rd instruction was wrong.

--set ClientTemplatepath to quoted form of ("/Desktop/Templates/OS Folders/Client Template")
-- it must be :
set ClientTemplatepath to quoted form of ("/Users/< here must be your Account's name>/Desktop/Templates/OS Folders/Client Template")
-- better to use:
set ClientTemplatepath to quoted form of POSIX path of ((path to desktop as string) & "Templates:OS Folders:Client Template")
 

The late one build the correct syntax by itself.

It seems that you missed that I made an important change in your original code.
I added "tell me to " in front of your do shell script command.
It’s required because do shell script which belongs to a scripting addition can’t be triggered as you tried to do in a tell application block.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 15 juin 2020 22:33:32