Copy folder structure and merge sub directories - bash

enter image description hereI'm not sure where best to post this, hopefully someone can advise if this isn't right, I've been a long time browser but this is my first post. Anyway..
I need to automate copying a load of folders to a new location and merging sub directories.
I will use ABC as an example text string which represents the company name and is a constant value.
The current structure is like:
2012 Jobs
ABC12 001-099
ABC12-001
jpgs
raw
web
tiffs
ABC12-002
ABC12-003
...
ABC12 100-199
ABC12 200-299
2013 Jobs
...
2014 Jobs
...
What I need to do is copy this structure to a new location, but merge the jpgs and web folders in to one named jpgs for example and the raw and tiffs in to another folder named tiffs.
I created an AppleScript to create the folder structure but these are obviously all just blank folders, would be much easier to just copy what is there already and start again, open to any suggestions, you're the experts! Thanks
Image shows structure before and how I want it after:
Before and After

The script bellow does partially what you need (I think !).
It looks for every "raw" in the top folder you selected (i.e. the folder containing all the "2012 jobs", 2013 Jobs",...)
For each "raw" folder found, it looks if, at same level a "tiffs" folder already exists (if not it creates it).
Then it moves all the files from "raw" to the "tiffs". Finally, it deletes the empty "raw".
set TopFolder to choose folder with prompt "Select parent folder to process"
tell application "Finder"
set RawsFolder to every folder of entire contents of TopFolder whose (name is "raw")
repeat with aFolder in RawsFolder
set ParentFolder to container of aFolder -- get upper folder
set TiffFolder to (ParentFolder as string) & "tiffs"
if not (exists TiffFolder) then make new folder in ParentFolder with properties {name:"tiffs"}
set FilesRaw to every file of aFolder --get all files from raw folder
move FilesRaw to folder TiffFolder -- move them to tiffs folder
delete aFolder -- delete the raw folder
end repeat
end tell
To complete, you must do same thing again, but with "web" folders to be copied into "jpgs". You have 2 ways to do that :
1) move the "tell Finder" block into sub routine with 2 strings parameters (raw, tiffs), adjusting the script to replace each occurrence of raw/tiffs by the parameter. then you call this routine with ("raw", "tiffs") and again with ("web","jpgs")
2) you just copy/paste the tell finder block and change "raw"/"tiffs" manually to "web" / "jpgs"
Up to you...
Warning : don't forget to run this on a full copie of your parent folder !

Related

Mac Automator - Compress (ZIP) a folder and rename as original folder name without the entirety of the path

Hi all, total newbie to any kind of programming, but I've written an Automator workflow to do a repetitive task I do daily. Basically I'm prepping artwork files and folders to send out to printers.
The steps are as follows:
Set Value of Variable (variable is ArtworkFolder)
where I drop the artwork folder containing the illustrator file and
PDF
Get Specified Finder Items
locates a file on a RAID server
Copy Finder Items (to ArtworkFolder)
copies said file into the folder
Ask for Confirmation -
prompts user to select an updated PDF
Ask for Finder Items -
opens the folder where the new PDF sits
Copy Finder Items (to ArtworkFolder)-
copies PDF file into the folder (overwriting older files)
Ask for Confirmation
prompts user to select print guidelines
Ask for Finder Items
opens the folder where the print guidelines sit
Copy Finder Items (to variable ArtworkFolder) -
copies print guidelines into the folder
THIS IS WHERE I THINK IT MAY BE GOING AWRY
Get Value of Variable (ArtworkFolder)
this is supposed to get the name of the original folder
Get Specified Finder Items
and all its contents
Create Archive
Then Zip the whole lot together
But it zips it with the name Archive.zip which I don't want so...
Rename Finder Items: Replace Text
which is set to find "Archive" in basename only ignoring Case. then
replace that with the variable ArtworkFolder
but it renames the folder with the entire path
I've tried to strip out the path by adding…
Rename Finder Items: Replace Text
which is set to find everything in the path basename only up to the
unique folder name and replace with nothing (eg I left the Replace box
blank)
But then it duplicates the folder name like this:
00000- Test Artwork Folder/00000- Test Artwork Folder.zip
All I want is to original folder name (00000- Test Artwork Folder) as
the zip file name.
Any tips would be greatly received!
For whatever reason, Automator doesn't have an included action to get file names, so you will need to use a third-party action or do it yourself. A Run AppleScript action can be used, but either way, note that you will need to save the workflow items and use the Ignore Input option as appropriate around getting the names, so you don't get them mixed in with the file items (also note that the workflow and action items are a list).
To get the name of a single item, something like the following can be inserted into your workflow, where the variable for the name can be used in the rename action:
Set Value of Variable { Variable: Original Input } -- stash current items
Run AppleScript: -- get the name
on run {input, parameters}
set {theContainer, theName, theExtension} to getNamePieces from (first item of input)
return theName
end run
to getNamePieces from somePath
tell application "System Events" to tell disk item (somePath as text)
set _container to path of container
set {_name, _extension} to {name, name extension}
end tell
if _extension is not "" then
set _name to text 1 thru -((count _extension) + 2) of _name -- just the name part
set _extension to "." & _extension
end if
return {_container, _name, _extension}
end getNamePieces
Set Value of Variable { Variable: Item Name }
Get Value of Variable { Variable: Original Input } (Ignore Input) -- continue workflow

Automatically move files of a certain type from folder A to B whenever they are added to folder A on macOS

Using Automator, I created a "Folder Action" to move files of certain types (i.e. "Kind IS Movie") from folder "FolderA" to folder "FolderB" as soon as they are added to folder "FolderA". It works fine except that it doesn't move files from subfolders (e.g. doesn't work if the movie files are added to "FolderA/SubA/"), and apparently there's no option on Automator to make it work on the subfolders.
Does anyone have any idea about how to either change that on Automator or how could I create a script for that? Thanks!
This following AppleScript folder action code should give you a general idea of how to accomplish what you are looking for. When a file or folder is added to a folder that this folder action is attached to, it will search all folders and sub folders for files (I used QuickTime movie as the kind of file... But you can change that to whatever you want) then those files will be moved to the folder that you will define in property moveToFolder
You can save this following code in Script Editor.app directly into your /Users/Your_User_Name/Library/Workflows/Applications/Folder Actions folder, as a .scpt file. Once the script is saved into that folder, it will be available to attach to any folder as a folder action using the Folder Actions Setup.
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Kind Of File To Act Upon
property fileKind : "QuickTime movie"
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose kind is fileKind)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to
OR Here Is A Version Which Defines The Name Extensions Of The Files To Act Upon
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Name Extensions Of Files To Act Upon
property videoFileExtensions : {"m4v", "mkv", "avi", "mp4", "mov", "wmv", "mpg"}
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose name extension is in videoFileExtensions)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to

What logic and why is AppleScript applying to folder names? Guess what this script is doing

I just stumbled across one of the most awkward behaviours of a programming language in my live.
Guess what this AppleScript is doing:
set workspace to "tmp"
set folder1 to "08_0000012_11"
set folder2 to "8_12_11"
tell application "Finder"
if (not (exists folder folder1 of (workspace as alias))) then
make new folder at workspace as alias with properties {name:folder1}
end if
if (not (exists folder folder2 of (workspace as alias))) then
make new folder at workspace as alias with properties {name:folder2}
else
log "Folder already exists " & folder2
end if
end tell
It should create create two folders 08_0000012_11 and 8_12_11 inside the /tmp, right? ..... Wrong! It creates first one and claims the other one already exists. But it does not!
It seems that it tries to apply some logic to these names. Splits them into 3 numbers and ignores zeroes. Please tell me there is some reasonable explanation to this. Or this is happening only to me...
That's a bug in Finder. It should only ignore leading zeros when sorting file names, not when comparing them. File a bug report.
The same logic works correctly in System Events, e.g.:
set fname to "001"
tell application "System Events"
if not exists folder fname of desktop folder then
make new folder at desktop folder with properties {name:fname}
end if
end tell

Applescript: Copy all contents of a folder into every folder in a directory

I'm in a bit of a bind and an Applescript noob. I've been stumbling through a script that would allow me to copy the contents of (TemplateFolder) into all of the client folders in a directory, with the added crux that all of the existing clients' folder contents be moved into a folder within the client labeled "Old Files" or something of the sort. I'll include images with my script thus far to illustrate what I'm trying to achieve. Thank you everyone in advance for all your help.
P.S. Since I don't have the rep required, I'll have to post links to my images.
http://imgur.com/a/lL9q7
The first image is the template folder (the folder I'd like all contents copied into each client folder).
The second image is an example of an existing client folder with all the bad structure (or lack thereof).
The final image is the expected results where the template folder's contents are moved into the client folder and the original content of the client's folder are moved into a separate folder titled "Old Structure Files".
Below is the applescript I've written, with help from others, to copy the contents. However there are missing components and some elements that need to change; currently the applescript simply copies the entire folder rather than just the contents and it makes a simple copy rather than inserting, the script is not recursive for an entire directory, and there's no function to move the existing client files into a "Old Structure Files" folder. Again, any and all help is greatly appreciated :)
on run
set source_folder to choose folder with prompt "Select folder to be duplicated:" as string
my do_main_script(source_folder)
end run
on open of source_folder_list
repeat with i from 1 to number of items in the source_folder_list
set this_folder_path to item i of the source_folder_list as string
if last character of this_folder_path is ":" then
my do_main_script(this_folder_path)
end if
end repeat
end open
on do_main_script(source_folder)
tell application "Finder" to set source_folder to folder (source_folder)
tell application "Finder" to set the target_folder to (parent of source_folder)
if source_folder is not "" and target_folder is not "" then
set new_folder_name to (name of source_folder as string) & " duplicate"
set source_folder to source_folder as string
set target_folder to target_folder as string
my create_new_folder(target_folder, new_folder_name)
my duplicate_folder_structure(source_folder, target_folder & new_folder_name & ":")
end if
end do_main_script
In the script bellow, the template folder is at fixed place on desktop, all the content of the customer selected folder is moved to a new folder "old structure", and after that, all content of the template folder is duplicated into the customer folder. If I well understand, this is what you're looking for :
set Templatefolder to "HD:Users:my_user:Desktop:Template"
set CustomerFolder to (choose folder with prompt "Select customer folder to be re-organised")
tell application "Finder"
set CustomerData to every item of CustomerFolder
set OldF to make new folder in CustomerFolder with properties {name:"Old Strutucture Files"}
move CustomerData to OldF
set Newstructure to every item of folder Templatefolder
duplicate Newstructure to CustomerFolder
end tell

Applescript to copy each file individually to target folder

I have all these movies in .dvdmedia format and I want to covert them all to a smaller file size such as .mp4.
But what I need to do is create an applescript that will copy the individual file to a folder 'Conversion'. Once the file in the folder is deleted it copies the next item and deletes the previous.
I've completed an Automation script that once the item is added to the folder is starts formatting the file through TurboHD then deletes the file and moves the converted item to another folder 'Completed'
Does anyone able to help me with this?
Please note that the location of the movies are on a NAS drive
there :) I coded the following. Please note to save it as an Application with Stay open after run handler checked.
You have to set the source and the target path to the paths in your environment.
Finally you have to set the return value. The value sets the interval in seconds to wait until next execution. If each of your conversions takes about an hour, I think I would check every 5 minutes meaning the handler has to return 300.
-- The idle-Handler defines a repetitive task
-- Note: Save as Application with option "Stay open after run handler"
on idle
-- define the folders to watch
set theSourceFolder to (POSIX file "/Users/myHomeFolder/Desktop/Conversion_Source") as alias
set theTargetFolder to (POSIX file "/Volumes/myMountedVolume/Conversion") as alias
-- check the contained files (get visible files only because of .DS_Store etc.)
tell application "System Events"
set availableSourceFiles to every file of theSourceFolder whose visible is true
set filesOfTargetFolder to files of theTargetFolder whose visible is true
end tell
-- if no more source file is available, quit this script
if (count of availableSourceFiles) = 0 then
quit
end if
-- if the target folder is empty start move
if (count of filesOfTargetFolder) = 0 then
-- get the first item from source folder
set sourceFile to (first item of availableSourceFiles) as alias
-- use the Finder to copy the file
tell application "Finder"
-- duplicate the file to the target folder
duplicate sourceFile to theTargetFolder
-- move the source file to trash after copy
move sourceFile to trash
end tell
end if
-- the integer returned is the time to wait (in seconds)
-- here: two minutes
return 120
end idle
Enjoy, Michael / Hamburg

Resources