Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript help

Hi all,


I'm looking for a script that would do the following (already tried withAutomator, but in vain):


I start with a folder (including subfolders & files), some of them having names ending with "_cc"


I need to create a specific number of copies of this folder, including subfolders and files, each one of them having the "_cc" replaced by another string.


example: i start with the folder Whatever_cc, and i want to obtain Whatever_aa, Whatever_dr, Whatever_gt, etc...


The replacement strings I need to use are: _cs, _da, _de, _es, _fr, _fi, etc... (I have a finite list of those)


Any idea how to do that with Applescript? I managed to create a workflow to do one copy (even though it was surprisingly difficult to ensure that folder names and file names were modified correctly, i had to use 3 different Change name actions), but can't find a way create the multiples copies and apply the rename actions correctly, either by using a loop and variables or sequential duplicate+renames.


Any help/advice appreciated,

Thanks in advance,

MacBook Pro, OS X Yosemite (10.10.3)

Posted on Apr 11, 2015 12:09 PM

Reply
5 replies

Apr 12, 2015 1:35 AM in response to lostinireland

Hello


You might try the following shell script in Run Shell Script Action in Automator workflow.



1) Ask for Finder Items action

- choose folder(s)


2) Run Shell Script action

- Shell = /bin/bash

- Pass input = as arguments

- Code = as follows



#!/bin/bash SUFFIXES=(cs da de es fr fi) for d in "$@" do cd "$d" || continue find . -type d -iname '*_cc' -depth -print0 | while read -d $'\0' f do for x in "${SUFFIXES[@]}" do cp -v -pPR "$f" "${f%_*}_$x" done done done



A resulting workflow will look like this:


User uploaded file



Please make sure you have complete backup of folders in advance when running this sort of script.


Briefly tested under OS X 10.6.8 but no warranties of any kind.


Good luck,

H

Apr 13, 2015 12:43 PM in response to Hiroto

Hi H,


Thanks a lot for your help!


There are only two issues with your script:

- it only changes folder names, not filenames

- it creates the copy inside the subfolders.


Let's see if illustrate better what i need:

I start with a folder like this, with some random folders and files


\test_cc

\test_cc\truc1_cc

\test_cc\truc1_cc\futc_cc.txt

\test_cc\truc2

\test_cc\truc2\bidule.txt

\test_cc\truc2\bidule2_cc.txt

\test_cc\truc2\machin_cc.bmp

\test_cc\truc3

\test_cc\truc3\chose_cc.bmp

\test_cc\truc3\New_cc


I want to create a list of semi-duplicates like that:


\test_de

\test_de\truc1_de

\test_de\truc1_de\futc_de.txt

\test_de\truc2

\test_de\truc2\bidule.txt

\test_de\truc2\bidule2_de.txt

\test_de\truc2\machin_de.bmp

\test_de\truc3

\test_de\truc3\chose_de.bmp

\test_de\truc3\New_de


or


\test_ru

\test_ru\truc1_ru

\test_ru\truc1_ru\futc_ru.txt

\test_ru\truc2

\test_ru\truc2\bidule.txt

\test_ru\truc2\bidule2_ru.txt

\test_ru\truc2\machin_ru.bmp

\test_ru\truc3

\test_ru\truc3\chose_ru.bmp

\test_ru\truc3\New_ru


See? exact copies of the source folder, except with all instances of _cc (in forlder or filenames) replaced by _ru, _de, _etc


Thanks in advance,

Cheers,

L

Apr 13, 2015 11:49 PM in response to lostinireland

Here's a self-contained (non-Automator specific) AppleScript solution that should do what you ask.


It prompts you for a folder, then duplicates that folder, renames all the _cc files to the corresponding replacement name, and renames the folder itself.


You can either run this in Automator (via a single 'Run AppleScript' action), or run it in AppleScript editor. Without knowing how you intend/hope to invoke this it's hard to tell you which is better.


property suffixes : {"_cs", "_da", "_de", "_es", "_fr", "_fi"}

property source_suffix : "_cc"


set theFolder to (choose folder with prompt "Select the base folder:")


repeat with thisSuffix in suffixes

tell application "Finder"


-- duplicate the folder

set newFolder to (duplicatetheFolderto (get container of theFolder))


-- find a list of items to rename (based on matching file name)

set filesToRename to ((every item of entire contents of newFolder whose name contains source_suffix) as alias list)


-- iterate through the items

repeat with eachFile in filesToRename


-- rename them

my doRename(eachFile as alias, thisSuffix as text)

end repeat


-- and rename the folder, too

set name of newFolder to (my textReplace((get name of theFolder), thisSuffix as text))


end tell

end repeat


on doRename(i, suffix)

tell application "Finder"


-- get the existing name

set _name to name of i


-- work out the new/replacement name

set new_name to my textReplace(_name, suffix)


-- and rename the file:

set name of i to new_name

end tell

end doRename


on textReplace(sourceText, replaceTerm)


-- basic text item replacement handler to convert filenames

set {od, my text item delimiters} to {my text item delimiters, source_suffix}

set tmp to text items of sourceText

set my text item delimiters to replaceTerm

set tmp to tmp as text

set my text item delimiters to od

return tmp

end textReplace

Apr 14, 2015 7:18 AM in response to lostinireland

Ah I see. Then you might try the following script instead. Other settings in Automator workflow are the same as the previous.



#!/bin/bash SUFFIXES=(de ru) for a in "$@" do cd "$a" || continue for d in *_cc do for x in "${SUFFIXES[@]}" do # new name for $d whose _cc replaced by _$x d1="${d%_cc}_$x" # copy $d to $d1 if $d1 does not exist [[ -d "$d1" ]] || cp -pPR "$d" "$d1" # for each node in depth-first order with name *_cc.ext or *_cc; find -E "$d1" -iregex '.*_cc(\..*|$)' -depth -print0 | while read -d $'\0' f do # new name for $f by replacing last _cc with _$x f1="$(sed -E "s%(.*)(_cc)%\1_$x%" <<< "$f")" # rename $f if leaf name is changed [[ "${f##*/}" != "${f1##*/}" ]] && mv "$f" "$f1" done done done done



Regards,

H

Apr 15, 2015 3:19 AM in response to Hiroto

Previous scripts of mine assume you choose parent(s) of *_cc folder(s) in preceding Automator action.


Just in case, here's a revision of the last script which lets you choose *_cc folders themselves in the action.



#!/bin/bash # set -- ~/desktop/test/*_cc # for stand-alone test SUFFIXES=(de ru) for d in "$@" do [[ $d =~ .*_cc$ ]] || continue for x in "${SUFFIXES[@]}" do # new name for $d whose _cc replaced by _$x d1="${d%_cc}_$x" # copy $d to $d1 if $d1 does not exist [[ -d "$d1" ]] || cp -pPR "$d" "$d1" # for each node in depth-first order with name *_cc.ext or *_cc; find -E "$d1" -iregex '.*_cc(\..*|$)' -depth -print0 | while read -d $'\0' f do # new name for $f by replacing last _cc with _$x f1="$(sed -E "s%(.*)(_cc)%\1_$x%" <<< "$f")" # rename $f if leaf name is changed [[ "${f##*/}" != "${f1##*/}" ]] && mv "$f" "$f1" done done done



Regards,

H

Applescript help

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.