Friday, May 29, 2020

#1 2020-04-02 03:01:12 pm

Danjuan
Member
Registered: 2020-04-02
Posts: 14

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.

Offline

 

#2 2020-04-03 02:02:36 am

Danjuan
Member
Registered: 2020-04-02
Posts: 14

Re: Copy folder, rename then run a terminal command

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.

Last edited by Danjuan (2020-04-03 02:05:16 am)

Offline

 

#3 2020-04-03 02:58:26 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4370

Re: Copy folder, rename then run a terminal command

You may try:

Applescript:

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

Online

 

#4 2020-04-03 03:55:54 am

Danjuan
Member
Registered: 2020-04-02
Posts: 14

Re: Copy folder, rename then run a terminal command

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:

Applescript:


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

Applescript:


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.

Offline

 

#5 2020-04-03 06:02:42 am

Danjuan
Member
Registered: 2020-04-02
Posts: 14

Re: Copy folder, rename then run a terminal command

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

Applescript:


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?

Offline

 

#6 2020-04-03 06:14:18 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Copy folder, rename then run a terminal command

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

Applescript:


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

Last edited by KniazidisR (2020-04-03 06:42:00 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2020-04-03 08:49:51 am

Danjuan
Member
Registered: 2020-04-02
Posts: 14

Re: Copy folder, rename then run a terminal command

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?

Offline

 

#8 2020-04-03 09:07:06 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Copy folder, rename then run a terminal command

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

Where is its documentation?

Last edited by KniazidisR (2020-04-03 09:10:23 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#9 2020-04-03 09:09:21 am

Danjuan
Member
Registered: 2020-04-02
Posts: 14

Re: Copy folder, rename then run a terminal command

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

Applescript:


from this...

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

to this...

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

Here's the full code

Applescript:


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 '¬'?

Applescript:


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

Offline

 

#10 2020-04-03 09:17:41 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Copy folder, rename then run a terminal command

Danjuan wrote:


What are these '¬'?


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.

Last edited by KniazidisR (2020-04-03 09:22:27 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#11 2020-04-03 09:24:08 am

Danjuan
Member
Registered: 2020-04-02
Posts: 14

Re: Copy folder, rename then run a terminal command

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)