Saturday, December 16, 2017

#1 2014-11-25 07:56:47 pm

starchyfind
Member
Registered: 2013-12-14
Posts: 8

Create folder structure and rename

I'm trying to create an workflow that will generate a folder structure (see below) where "ShowName" is a variable based on user input.  I've managed to do it without any subfolders, but can't manage to get it any further.  Does it need to be done with Applescript?


ShowName
    ShowName Folder 1
        YYYY-MM-DD
    ShowName Folder 2
    ShowName Folder 3
        zOLD
    ShowName Folder 4
        zOLD
    ShowName Folder 5
    ShowName Folder 6
    ShowName Folder 7
        YYYY-MM-DD
    ShowName Folder 8
    ShowName Folder 9
        Folder A
        Folder B
        Folder C
    ShowName Folder 10


Any ideas?  Thanks for the help,

-adf

Offline

 

#2 2014-11-26 04:25:41 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Create folder structure and rename

Hi,

in Automator

• add a Ask for Finder Items action, set the prompt to Choose Destination Folder (or whatever you like) and the type to Folders.
• add a Run AppleScript action and replace the code with

Applescript:


property prefix : ""

on run {input, parameters}
   set folderpath to POSIX path of (item 1 of input)
   
   set prefix to text returned of (display dialog "Enter Prefix:" default answer "")
   set timeStamp to do shell script "date +%Y-%m-%d"
   
   set folderHierarchy to "/{" & folderName(1) & "/" & timeStamp & ¬
       "," & folderName(2) & "," & folderName(3) & "/zOld" & ¬
       "," & folderName(4) & "/zOld" & ¬
       "," & folderName(5) & ¬
       "," & folderName(6) & "," & folderName(7) & "/" & timeStamp & ¬
       "," & folderName(8) & "," & folderName(9) & "/{'Folder A','Folder B','Folder C'}" & ¬
       "," & folderName(10) & "}"
   do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of prefix & "/" & folderHierarchy
   return input
end run

on folderName(v)
   return quoted form of (prefix & space & "Folder" & space & v)
end folderName


regards

Stefan

Offline

 

#3 2014-11-26 06:16:26 am

starchyfind
Member
Registered: 2013-12-14
Posts: 8

Re: Create folder structure and rename

Hi Stefan,

Thanks for the quick reply.

That almost does it, but I'm afraid I misled you with my original post.  The folder structure actually has the names below, which generally don't change, but might some day in the future.  The date stamp for the YYYY-MM-DD folders is handy, and will be useful for the future, but I actually just need the folders to literally be "YYYY-MM-DD" (it's just meant to remind the user of the proper date format).  One last thing, is it possible to tag the ShowName Drafting folder with a green label?

ShowName
    ShowName Client Info & Drawings
        YYYY-MM-DD
    ShowName COI
    ShowName Drafting
        zOLD
    ShowName LW
        zOLD
    ShowName Power
    ShowName Prod. Management
    ShowName Release
        YYYY-MM-DD
    ShowName Rig Photos
    ShowName Show Order & Pricing
        Folder A
        Folder B
        Folder C
    ShowName Venue Info

Thanks again for all your help,

-adf

Offline

 

#4 2014-11-26 06:34:56 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Create folder structure and rename

here we go

Applescript:


property prefix : ""

on run {input, parameters}
   set folderpath to item 1 of input
   
   set prefix to text returned of (display dialog "Enter Prefix:" default answer "")
   set folderHierarchy to "/{" & folderName("Client Info & Drawings") & "/YYYY-MM-DD," & folderName("COI") & ¬
       "," & folderName("Drafting") & "/zOld" & ¬
       "," & folderName("LW") & "/zOld" & ¬
       "," & folderName("Power") & ¬
       "," & folderName("Prod. Management") & ¬
       "," & folderName("Release") & ¬
       "/YYYY-MM-DD," & folderName("Rig Photos") & ¬
       "," & folderName("Show Order & Pricing") & "/{'Folder A','Folder B','Folder C'}" & ¬
       "," & folderName("Venue Info") & "}"
   do shell script "/bin/mkdir -p " & quoted form of POSIX path of folderpath & "/" & quoted form of prefix & "/" & folderHierarchy
   tell application "Finder" to set label index of folder (prefix & space & "Drafting") of folder prefix of folderpath to 6
   return input
end run

on folderName(v)
   return quoted form of (prefix & space & v)
end folderName


regards

Stefan

Offline

 

#5 2014-11-26 06:50:41 am

starchyfind
Member
Registered: 2013-12-14
Posts: 8

Re: Create folder structure and rename

Amazingly perfect.  Much appreciated.

-adf

Offline

 

#6 2014-11-26 09:21:29 pm

ccstone
Member
Registered: 2009-02-07
Posts: 409

Re: Create folder structure and rename

starchyfind wrote:

I'm trying to create an workflow that will generate a folder structure


Hey StarchyFind,

This job could be done simply enough with the Finder, but the shell makes it a bit easier still.

Since the folder structure might be changed in future I'd make it easy to read and maintain.

Personally I'd run this from FastScripts or Keyboard Maestro, so I'm not going to cover the Automator bit (and Stefan already has).

The script will look more orderly in the Applescript Editor or in an Automator AppleScript action, as the tabs will expand out properly.

Applescript:


set myShowName to text returned of (display dialog "Enter Show Name:" default answer "Your Show Name")

set shCMD to text 1 thru -1 of "
showName="
& quoted form of myShowName & ";
baseFolderPath=~/\"test_directory/TEST_FOLDER/\"
dtStr=\"YYYY-MM-DD\";

if [ -e \"$baseFolderPath$showName\" ]; then
   echo \"Error: The showName folder already exists!\";
else
   mkdir -p \"$baseFolderPath$showName\"
   cd \"$baseFolderPath$showName\";
   mkdir                                                \\
       \"$showName Client Info & Drawings\"            \\
       \"$showName Client Info & Drawings/$dtStr\"        \\
       \"$showName COI\"                                \\
       \"$showName Drafting\"                            \\
       \"$showName Drafting/zOLD\"                        \\
       \"$showName LW\"                                \\
       \"$showName LW/zOLD\"                            \\
       \"$showName Power\"                                \\
       \"$showName Prod. Management\"                    \\
       \"$showName Release\"                            \\
       \"$showName Release/$dtStr\"                    \\
       \"$showName Rig Photos\"                        \\
       \"$showName Show Order & Pricing\"                \\
       \"$showName Show Order & Pricing/Folder A\"        \\
       \"$showName Show Order & Pricing/Folder B\"        \\
       \"$showName Show Order & Pricing/Folder C\"        \\
       \"$showName Venue Info\";

       echo \"$baseFolderPath$showName/$showName Drafting/\";
fi
"

set shResult to do shell script shCMD

if shResult starts with "Error" then
   error shResult
else
   set fldrToTag to alias POSIX file shResult
   tell application "Finder" to set label index of fldrToTag to 6
end if

Last edited by ccstone (2014-11-26 09:39:50 pm)

Offline

 

#7 2014-11-27 09:22:11 am

starchyfind
Member
Registered: 2013-12-14
Posts: 8

Re: Create folder structure and rename

Chris,

That's fantastic.  Your way makes a lot of sense.  I like that it can be saved as an Applescript application, so the user need only to double-click it to run the script.  This is going to be used in an office where users' skill levels vary greatly, so the simpler, the better. 

One thing I like about the Automator workflow is that it brought up a window that let you choose the directory for the script output.  Is that something that could be added to your script?  And/or could it use the directory in which the script resides as the output directory?  Knowing both ways would be useful.

Many thanks,

-Andy

Offline

 

#8 2014-11-27 09:45:38 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Create folder structure and rename

any compiled AppleScript can be saved as an application.

This is my approach without the Automator stuff and with a check whether the entered prefix already exists

Applescript:


property prefix : ""

set folderpath to choose folder with prompt "Choose Destination Folder"
repeat
   set prefix to text returned of (display dialog "Enter Prefix:" default answer "")
   tell application "System Events" to exists folder prefix of folderpath
   if result then
       display dialog "The folder '" & prefix & "' already exists. Please enter a different one" buttons {"OK"} default button 1
   else
       exit repeat
   end if
end repeat

set folderHierarchy to "/{" & folderName("Client Info & Drawings") & "/YYYY-MM-DD," & folderName("COI") & ¬
   "," & folderName("Drafting") & "/zOld" & ¬
   "," & folderName("LW") & "/zOld" & ¬
   "," & folderName("Power") & ¬
   "," & folderName("Prod. Management") & ¬
   "," & folderName("Release") & ¬
   "/YYYY-MM-DD," & folderName("Rig Photos") & ¬
   "," & folderName("Show Order & Pricing") & "/{'Folder A','Folder B','Folder C'}" & ¬
   "," & folderName("Venue Info") & "}"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of folderpath & "/" & quoted form of prefix & "/" & folderHierarchy
tell application "Finder" to set label index of folder (prefix & space & "Drafting") of folder prefix of folderpath to 6

on folderName(v)
   return quoted form of (prefix & space & v)
end folderName


regards

Stefan

Offline

 

#9 2014-11-28 11:48:41 am

starchyfind
Member
Registered: 2013-12-14
Posts: 8

Re: Create folder structure and rename

Thanks Stefan.  Is there a way to have the script output to the directory in which the script is located so there's no Choose Destination window?

Thanks,

-Andy

Offline

 

#10 2014-11-28 12:16:13 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Create folder structure and rename

replace

Applescript:

set folderpath to choose folder with prompt "Choose Destination Folder"

with

Applescript:


set myPath to path to me
tell application "System Events" to set folderpath to path of container of myPath as alias


regards

Stefan

Offline

 

#11 2014-11-29 04:12:23 pm

ccstone
Member
Registered: 2009-02-07
Posts: 409

Re: Create folder structure and rename

starchyfind wrote:

One thing I like about the Automator workflow is that it brought up a window that let you choose the directory for the script output.


Hey Andy,

Sure, that's easy enough.

I've added an error-handler.

The default location is the Desktop from whence the user can choose whatever location they want as the base folder.

Applescript:


try
   
   set baseFolderPath to POSIX path of (choose folder with prompt "Choose Root Folder:" default location (path to desktop))
   set myShowName to text returned of (display dialog "Enter Show Name:" default answer "Your Show Name")
   
   set shCMD to text 1 thru -1 of "
   showName="
& quoted form of myShowName & ";
   baseFolderPath="
& quoted form of baseFolderPath & ";
   dtStr=\"YYYY-MM-DD\";
   
   if [ -e \"$baseFolderPath$showName\" ]; then
       echo \"Error: The showName folder already exists!\";
   else
       mkdir -p \"$baseFolderPath$showName\"
       cd \"$baseFolderPath$showName\";
       mkdir                                                \\
           \"$showName Client Info & Drawings\"            \\
           \"$showName Client Info & Drawings/$dtStr\"        \\
           \"$showName COI\"                                \\
           \"$showName Drafting\"                            \\
           \"$showName Drafting/zOLD\"                        \\
           \"$showName LW\"                                \\
           \"$showName LW/zOLD\"                            \\
           \"$showName Power\"                                \\
           \"$showName Prod. Management\"                    \\
           \"$showName Release\"                            \\
           \"$showName Release/$dtStr\"                    \\
           \"$showName Rig Photos\"                        \\
           \"$showName Show Order & Pricing\"                \\
           \"$showName Show Order & Pricing/Folder A\"        \\
           \"$showName Show Order & Pricing/Folder B\"        \\
           \"$showName Show Order & Pricing/Folder C\"        \\
           \"$showName Venue Info\";
   
           echo \"$baseFolderPath$showName/$showName Drafting/\";
   fi
   "

   set shResult to do shell script shCMD
   
   if shResult starts with "Error" then
       error shResult
   else
       set fldrToTag to alias POSIX file shResult
       tell application "Finder" to set label index of fldrToTag to 6
   end if
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
       try
           tell current application to button returned of ¬
               (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
                   default button "OK" giving up after 30)
           if ddButton = "Copy" then set the clipboard to e
       end try
   end if
end try

starchyfind wrote:

And/or could it use the directory in which the script resides as the output directory?  Knowing both ways would be useful.


That too is easy enough.  Change the top line of the script to these two lines:

Applescript:


set AppleScript's text item delimiters to ":"
set baseFolderPath to POSIX path of (choose folder with prompt "Choose Root Folder:" default location alias (((text items 1 thru -3 of (path to me as text)) as text) & ":"))

Save the script as an AppleScript application, and you should be good to go.

Offline

 

#12 2014-12-01 10:34:45 am

starchyfind
Member
Registered: 2013-12-14
Posts: 8

Re: Create folder structure and rename

ccstone wrote:
starchyfind wrote:

And/or could it use the directory in which the script resides as the output directory?  Knowing both ways would be useful.


That too is easy enough.  Change the top line of the script to these two lines:

Applescript:


set AppleScript's text item delimiters to ":"
set baseFolderPath to POSIX path of (choose folder with prompt "Choose Root Folder:" default location alias (((text items 1 thru -3 of (path to me as text)) as text) & ":"))

Save the script as an AppleScript application, and you should be good to go.


Hi Chris,

I actually intended for this to automatically output to the same directory and not bring up a Choose Folder window, like it does in one of Stefan's scripts.

-adf

Offline

 

#13 2014-12-01 12:32:55 pm

ccstone
Member
Registered: 2009-02-07
Posts: 409

Re: Create folder structure and rename

starchyfind wrote:

I actually intended for this to automatically output to the same directory and not bring up a Choose Folder window, like it does in one of Stefan's scripts.


Hey Andy,

That too is easy enough.  Change that top line to this instead.

Applescript:


set AppleScript's text item delimiters to ":"
set baseFolderPath to POSIX path of (((text items 1 thru -3 of (path to me as text)) as text) & ":")

Offline

 

#14 2014-12-01 01:15:53 pm

starchyfind
Member
Registered: 2013-12-14
Posts: 8

Re: Create folder structure and rename

Chris and Stefan, thanks for all your help.

I had one thought for a future enhancement.  Is it possible for the script, either before or after you choose the destination (after probably makes more sense), to have an ability to select between two (or possibly more) unique folder structures within the script?

For example, a dialog box with a button for "Theater" and "Corporate" and a button to Cancel.

I can easily make two different scripts, but it would just be cleaner and slicker to have it one script.  If it's a pain, no worries.

-adf

Offline

 

#15 2014-12-01 01:27:47 pm

ccstone
Member
Registered: 2009-02-07
Posts: 409

Re: Create folder structure and rename

starchyfind wrote:

Is it possible for the script… to have an ability to select between two (or possibly more) unique folder structures within the script?


Hey Andy,

Of course.  It's a script.  cool

Applescript:


set folderStructureTypeList to {"Theater", "Corporate", "How", "Many", "Choices", "Do", "You", "Want?"}
set fldrStrChoice to choose from list folderStructureTypeList default items (item 1 of folderStructureTypeList)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)