Copy folder, rename then run a terminal command

Hi everyone.

I won’t lie, this is my first post having recently found this website. I’ve never played about with any automation but with everything thats going on I thought I would look at streamlining some tasks.

I can tell you now that this is my new favourite site.

I’ve searched through previous posts and I cannot find an answer so I thought I will post.

I have a template folder which I manually copy and paste every time I get a new client. I’ve now added version control to the mix and now requires a Terminal command to run when the folder has been duplicated.

So I am trying to do the following:

• Copy the folder 1-Client Template
(url is ~/Documents/Clients/)
• Rename the folder
(I need to have the option to type the name I want as this will change everytime I run the script)
• Run the following Terminal command

cd ~/Documents/Clients/1-Client\ Template && git init && git config --global user.email ‘name@email.co.uk && git config --global user.name 'Full Name' && git add . && git commit -m 'initial commit, added all project files' && git status

Ideally I would either like a button in the toolbar or an icon where I can double click to run it.

I’ve wasted 2 days on this and I’m no further along. I’ve been trying to do this via apple script but I am not fussed if it uses Automator or any other way as long as I can run it with ease.

Does anyone have any ideas on how best to approach this?

I’m pretty handy when it comes to basic web coding but I know nothing about apple script or Automator.

So I’ve been playing about with this all night and and this is where I am up to as still no further along so I thought I would posts what I have so far.

set sourceFolder to POSIX file "/Users/danjaun/Documents/Clients/George"
set destFolder to POSIX file "/Users/danjaun/Documents/Clients"


tell application "Finder"
	if exists sourceFolder then
		set dupeFile to duplicate sourceFolder to destFolder
		set dupeFile's name to text returned of (display dialog "Company Folder Name:" default answer "")
	else
		display dialog "Do one!:"
	end if
end tell

So far this code duplicates a folder called ‘george’, it then pastes george into the same folder, creates a popup asking for the company folder name, it then finally changes the name of the new folder to match.

A few things with the code I don’t like although minor.

• It duplicates the folder before asking for the name! This means if I cancel the popup, the folder is still created.
• I’m using the following way to get the source and destination urls

set sourceFolder to POSIX file "/Users/danjaun/Documents/Clients/George"

I would rather remove: /Users/danjuan and use ~/ but that don’t seems to brake the code. This way I can share the code and the next user doesn’t have to edit it.

You may try:

set sourceFolder to (path to documents folder as text) & "Clients:"
set templateName to "George"
set destName to text returned of (display dialog "Type the name to give to new item" default answer "W Bush")

do shell script "cp -R " & quoted form of POSIX path of (sourceFolder & templateName) & space & quoted form of POSIX path of (sourceFolder & destName)

I’m not sure that the ‘template’ is a folder. I assumed that because I never use files with no extension.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 3 avril 2020 10:58:21

Hi Yvan

Thank you for that.

That works so much better than mine and with less code.

I managed to sort out the issues with my code but I think yours works better.

Here’s my code just incase:


set sourceFolder to POSIX path of (path to home folder) & "Documents/Clients/george" as POSIX file
set destFolder to POSIX path of (path to home folder) & "Documents/Clients" as POSIX file
set destFoldername to text returned of (display dialog "Company Folder Name:" default answer "")


tell application "Finder"
	if exists sourceFolder then
		set theDupe to duplicate sourceFolder to folder destFolder
		set name of theDupe to destFoldername
	else
		display dialog "George is not found!"
	end if
end tell

I’m now trying to add my Terminal code to the mix.

Based on you code I have come up with this…


set sourceFolder to (path to documents folder as text) & "Clients:"
set templateName to "George"
set destName to text returned of (display dialog "Type your clients business name" default answer "")

do shell script "cp -R " & quoted form of POSIX path of (sourceFolder & templateName) & space & quoted form of POSIX path of (sourceFolder & destName)

do shell script "cd " & quoted form of POSIX path of (sourceFolder & templateName) & git init && git config --global user.email ‘name@email.co.uk && git config --global user.name 'Full Name' && git add . && git commit -m 'initial commit, added all project files' && git status & space & quoted form of POSIX path of (sourceFolder & destName)

But I get the following error code when saving

Expected end of line, etc. but found identifier.

Ok, so a bit of playing around and wit the help of Yvan I have come up with this…


set sourceFolder to (path to documents folder as text) & "Clients:"
set destFolder to POSIX file "~/Documents/Clients"
set templateName to "george"
set destName to text returned of (display dialog "Type your clients business name" default answer "")

do shell script "cp -R " & quoted form of POSIX path of (sourceFolder & templateName) & space & quoted form of POSIX path of (sourceFolder & destName)

do shell script "cd " & POSIX path of (destFolder) & ("/") & (destName) & "&& git init && git config --global user.email ‘name@email.co.uk && git config --global user.name 'Full Name' && git add . && git commit -m 'test commit, added all project files' && git status"

It appears to work but is it the best way to do it?

To avoid a syntax error, let’s write the code nicely:


set theEmail to quoted form of "name@email.co.uk" -- ADDED
set userName to quoted form of "Full name" -- ADDED

set sourceFolder to (path to documents folder as text) & "Clients:"
set templateName to "George"
set destName to text returned of (display dialog "Type your clients business name" default answer "")

set TemplateQuoted to quoted form of POSIX path of (sourceFolder & templateName) -- ADDED
set destinationQuoted to quoted form of POSIX path of (sourceFolder & destName) -- ADDED

do shell script "cp -R " & TemplateQuoted & space & destinationQuoted -- EDITED

do shell script "cd " & ¬
	TemplateQuoted & ¬
	" git init" & ¬
	" && git config --global user.email " & ¬
	theEmail & ¬
	" && git config --global user.name " & ¬
	userName & ¬
	" && git add ." & ¬
	" && git commit -m 'initial commit, added all project files'" & ¬
	" && git status " & ¬
	destinationQuoted -- EDITED

Hi KniazidisR

That code seems a lot nicer and although I don’t understand it all just yet, there is an error when I run it.

fatal: not a git repository (or any of the parent directories): .git

Any idea what this is pointing to?

I don’t know how it works. Maybe, git in do shell script should be replaced with some certain repository?

Where is its documentation?

Hi KniazidisR

I had an error using your code so I have edited it slightly and it now appears to work.

I edited these 3 lines


from this...

do shell script "cd " & ¬
	destinationQuoted & ¬
	" && git init" & ¬

to this...

do shell script "cd " & ¬
	TemplateQuoted & ¬
	" git init" & ¬

Here’s the full code


set theEmail to quoted form of "name@email.co.uk" -- ADDED
set userName to quoted form of "Full name" -- ADDED

set sourceFolder to (path to documents folder as text) & "Clients:"
set templateName to "george"
set destName to text returned of (display dialog "Type your clients business name" default answer "")

set TemplateQuoted to quoted form of POSIX path of (sourceFolder & templateName) -- ADDED
set destinationQuoted to quoted form of POSIX path of (sourceFolder & destName) -- ADDED

do shell script "cp -R " & TemplateQuoted & space & destinationQuoted -- EDITED

do shell script "cd " & ¬
	destinationQuoted & ¬
	" && git init" & ¬
	" && git config --global user.email " & ¬
	theEmail & ¬
	" && git config --global user.name " & ¬
	userName & ¬
	" && git add ." & ¬
	" && git commit -m 'initial commit, added all project files'" & ¬
	" && git status " & ¬
	destinationQuoted -- EDITED

Thank you so much for your help.

Can you explain in a bit more detail the following section so I can can understand it more.

What are these ‘¬’?


do shell script "cd " & ¬
	destinationQuoted & ¬
	" && git init" & ¬
	" && git config --global user.email " & ¬
	theEmail & ¬
	" && git config --global user.name " & ¬
	userName & ¬
	" && git add ." & ¬
	" && git commit -m 'initial commit, added all project files'" & ¬
	" && git status " & ¬
	destinationQuoted -- EDITED

This is a sign of “bonding”. Using this character AppleScript provides convenience for writing text in a column, the text which is actually a single continuous line. That is, the text is not break actually as with return or linefeed signs.

Thank you.

It now works perfect although now i think I want to take it one step further and ideally get Google Assistant to run it if thats at al possible.

I’ll ask my question in another post