Copying a folder structure that lives elsewhere

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

I'm trying to automatically "populate" new job folders for the design firm I work for, and I'm having a bit of trouble finding answers.

We currently have an automated system that generates new (empty) job folders on our server whenever a new job is assigned. What I want to do is automatically fill each new folder with the entire contents of a "Job Folder Template" that we all use. This template folder also lives on the server (but not in the same location as the job folders).

The job folder template contains folders like, "Presentations," "Stock Imagery," "Reference Documents," "Copy," "Artwork," etc. This is how we organize just about every job we work on – So rather than manually copying/pasting these empty folders every time, I thought it'd be smart to automate it. I also like the idea that if we ever decide to change our template folder's structure, any newly created job folders would automatically pull the latest structure -- that is, as long as we keep it in the same location.

Anyway, I've built quite a few custom actions with Hazel and I have a pretty good understanding of how it works. In this case, the issue is that it's not just a straight "move A to B" copy operation... New job folders all have unique names. However, they ALL contain certain strings of text that identify them as new job folders. For example, when a new one is created, it's name ends with three consecutive hyphens (---). Therefore I was hoping to do something like this:

IF ALL THESE CONDITIONS ARE MET:
Kind is Folder
Name Contains "---"

THEN
Copy Contents of [file path of job folder template on server] to [the matched folder]


I'm thinking Hazel must be capable of doing this somehow, but I'm sure I'll need to get into some custom scripting to make it happen. Anybody have any ideas / tips that'll at least point me in the right direction? I know this probably seems like a really inane task, but if I'm able to pull it off, it'll be huge. :shock:

Anyway, I hope I've been clear in describing the situation. Any help would be greatly appreciated!
exlxaxd
 
Posts: 4
Joined: Tue Aug 20, 2013 9:43 pm

The action part is where you need the script. The current actions operate on the matched file/folder as the source of the operation, not the destination so you'd need a script to do the copy itself. A shell script would work well for this. A starting point:
Code: Select all
cp -r "/path/to/template" "$1"


You should google around so you understand the above enough to fix/modify it to fit your needs. Also, you might want to add a condition to match folders specifically.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Thanks for the help Mr_Noodle. So, it seems like the "$1" argument needs to be some sort of query that looks for "---." Is that correct? Because it's not just a variable folder name - it's a variable folder name that must contain "---" There are other folders that get created in the same parent directory that are NOT job folders (they don't contain "---" in their names) and I wouldn't want the template copied into those.

Anyway, I found this intro article which does help a little: https://developer.apple.com/library/mac ... ripts.html However, it explains things in technical terms which at this particular point are a little over my head. I'm going to spend some more time with it, but ultimately it doesn't seem like it'll help me to understand how to set the "$1" variable to be a search query.

Thanks again for your help. If you know of any other resources that might be able to dumb shell scripting down for me a little more, that'd be great. Otherwise, I'll just keep on digging. :D

Mr_Noodle wrote:
Code: Select all
cp -r "/path/to/template" "$1"


You should google around so you understand the above enough to fix/modify it to fit your needs. Also, you might want to add a condition to match folders specifically.
exlxaxd
 
Posts: 4
Joined: Tue Aug 20, 2013 9:43 pm

You still need to use the condition to match the folder with "---". The script will use that folder as the destination of the copy, putting the template stuff underneath it.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Just a minor correction, you will need a trailing / on the template argument or else it will copy the template directory itself into the new job directory. So the script you will need it
Code: Select all
cp -r "/path/to/template/" "$1"


So, as an example, on your server is a shared folder containing the following
Code: Select all
/Assigned Jobs/
/Assigned Jobs/Existing Job A/...
/Job Template/
/Job Template/Artwork/
/Job Template/Copy/
/Job Template/Presentations/


and that file share is mounted on your Mac at /Volumes/fileshare/, you would configure Hazel to monitor /Volumes/fileshare/Assigned Jobs for

If ALL of the conditions are met
  • Kind is Folder
  • Name Ends With '---'
  • Sub-file/folder Count is 0
Do the following
  • Passes Shell Script
    • embedded script containing
      Code: Select all
      cp -r "/Volumes/fileshare/Job Template/" "$1"

So when your automated system creates /Assigned Jobs/Bob the Plumber---/, Hazel will copy the contents of the Job Template folder into the new Bob the Plumber--- folder, thus giving you
Code: Select all
/Assigned Jobs/Bob the Plumber---/Artwork/
/Assigned Jobs/Bob the Plumber---/Copy/
/Assigned Jobs/Bob the Plumber---/Presentations/


and to explain the shell script a bit.
The full path of the item that was matched by Hazel is passed to the shell script in the $1 variable. In this example, $1 would contain the value /Volumes/fileshare/Assigned Jobs/Bob the Plumber---

Since you don't know if the path could, and in this example does, contain spaces then you need to put quotes around the path and the variable. If you didn't, the shell would see the script as multiple command arguments of
Code: Select all
cp -r /Volumes/fileshare/Job
Template/
/Volumes/fileshare/Assigned
Jobs/Bob
the
Plumber---

instead of
Code: Select all
cp -r "/Volumes/fileshare/Job Template/" "/Volumes/fileshare/Assigned Jobs/Bob the Plumber---"



and if you want to learn more, forget about that Apple Developer document and try this instead. :D

Hope that helps!
Smudge
 
Posts: 34
Joined: Mon Jul 15, 2013 10:44 pm

This the post I was looking for :-)

I did a setup to pass a shell script :
Code: Select all
mv -f "$1" /Users/sa/Documents/HAZEL_Rules/Moved_From_Previous_Location


But .DS_Store seems to be busy while mv is ruuning :

Code: Select all
2015-01-27 18:26:42.471 hazelworker[17205] _ADOBE_: Rule Suppression_PatternClient_ matched.
2015-01-27 18:26:42.472 hazelworker[17205] [File Event] File renamed: /Users/sa/Documents/HAZEL_Rules/Renaming_Folders/_ADOBE_ renamed to /Users/sa/Documents/HAZEL_Rules/Renaming_Folders/ADOBE.
2015-01-27 18:26:42.585 hazelworker[17205] Hazel Alert: Rule : Suppression_PatternClient_
2015-01-27 18:26:42.585 hazelworker[17205] [Custom Message] Hazel Alert: Rule : Suppression_PatternClient_
2015-01-27 18:26:42.604 hazelworker[17205] [Error] Shell script failed: Error processing shell script on file /Users/sa/Documents/HAZEL_Rules/Renaming_Folders/ADOBE.
2015-01-27 18:26:42.604 hazelworker[17205] Shellscript exited with non-successful status code: 1
2015-01-27 18:26:44.614 hazelworker[17205] File .DS_Store is busy. Skipping for now.
2015-01-27 18:26:50.826 hazelworker[17205] Done processing folder Renaming_Folders


How I could exclude this .DS_Store ????
trouspinette
 
Posts: 11
Joined: Tue Jan 27, 2015 9:43 am

OK, I can now copy files/folder if structure lives elsewhere :

Image

But still issues while shell script is running :

Code: Select all
/bin/cp -r "$1" /Users/sa/Documents/HAZEL_Rules/Moved_From_Previous_Location && /bin/rm -f "$1"


Console errors :

Code: Select all
2015-01-27 19:09:42.540 hazelworker[17759] _ADOBE3_: Rule Suppression_PatternClient_ matched.
2015-01-27 19:09:42.541 hazelworker[17759] [File Event] File renamed: /Users/sa/Documents/HAZEL_Rules/Renaming_Folders/_ADOBE3_ renamed to /Users/sa/Documents/HAZEL_Rules/Renaming_Folders/ADOBE3.
2015-01-27 19:09:42.659 hazelworker[17759] Hazel Alert: Rule : Suppression_PatternClient_
2015-01-27 19:09:42.659 hazelworker[17759] [Custom Message] Hazel Alert: Rule : Suppression_PatternClient_
2015-01-27 19:09:42.707 hazelworker[17759] [Error] Shell script failed: Error processing shell script on file /Users/sa/Documents/HAZEL_Rules/Renaming_Folders/ADOBE3.
2015-01-27 19:09:42.707 hazelworker[17759] Shellscript exited with non-successful status code: 1
2015-01-27 19:09:46.868 hazelworker[17759] Done processing folder Renaming_Folders
trouspinette
 
Posts: 11
Joined: Tue Jan 27, 2015 9:43 am


Return to Support