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

Applescript - creat a folder, subfolders and populate with sample image

Please help me achieve this, probably, simple goal.


I've only done a very small amount of applescripting before and have always butchered other script resources found online but am having trouble working out how to get this done.


I would like to use applescript to automate the creation of a simple folder structure of newFolder and newSubFolders in a destination that the user can choose and the number of subfolders (subCount) chosen using the prompt:

set subCount to text returned of (display dialog "How many subfolders?" default answer 85)

The newFolder will always be in a format of date in reverse eg 160202 for 2nd February 2016.

The sub folder names to end with 3 numerical characters, so leading 0s for any folder number less that 100:

160202_001, 160202_002, 160202_003 etc etc.

Along with all of the above need the user will choose an image file and have it duplicated, or should that be copied, to all of the subFolders created by this script.

All I have managed so far is to create a script that creates the folder structure with subfolders but cannot work out how to make the names of the folders end with the 3 numerical characters, or work out how to copy/duplicate a file to every subfolder created by the script.

Here is my starting point:

set JobName to text returned of (display dialog "Please enter Date Reference:" default answer "160411")

set loc to choose folderwith prompt "Choose Parent Folder Location:"


tell application "Finder"

set targetFolder to makenewfolderatlocwith properties {name:JobName}

end tell


repeat

set subCount to text returned of (display dialog "How many subfolders?" default answer 85)

try

if subCount ≠ "" then


subCount as integer

exit repeat

end if

end try

end repeat


repeat with i from 1 to subCount


display dialog "my variable: " & i

tell application "Finder" to makenewfolderattargetFolderwith properties {name:JobName & "_" & i}


end repeat



set filetoCopy to choose filewith prompt "Choose the image file to copy."


display dialog "my variable: " & filetoCopy

It doesn't really matter to me which order the script has to happen in, i.e. the the image file to be copied can be chosen first before the creation of the all subfolders etc.... as long as the end result is the same structure.


I had a look into adding leading 0s to a folder name but didn't really understand how i could do this within the repeat that exists in my script.


Any help very much appreciated.


Mike

Posted on May 3, 2016 1:57 AM

Reply
7 replies

May 3, 2016 7:35 AM in response to Michael Simpson1

Although this still needs a lot of tidying up (including addition of error checking) I have managed to get the creation of folders to work and the copying of a file to all of the folders.


I would still very much appreciate any help in the renaming with leading 000s after the _in the folder names.


Here is where I am up to:


set JobName to text returned of (display dialog "Please enter Date Reference:" default answer "160411")

set loc to choose folderwith prompt "Choose Parent Folder Location:"


tell application "Finder"

set targetFolder to makenewfolderatlocwith properties {name:JobName}

end tell


repeat

set subCount to text returned of (display dialog "How many subfolders?" default answer 85)

try

if subCount ≠ "" then


subCount as integer

exit repeat

end if

end try

end repeat



repeat with i from 1 to subCount


tell application "Finder" to makenewfolderattargetFolderwith properties {name:JobName & "_" & i}


end repeat



tell application "Finder"


set _destination to choose folderwith prompt "Choose the Main Folder Created above:"

set filetoCopy to choose filewith prompt "Choose image file to copy:"

display dialog "my variable: " & filetoCopy

set _listOfDest to (every folder of _destination) as alias list

repeat with CurrentNumber from 1 to (count_listOfDest)


duplicatefilefiletoCopytofolder (itemCurrentNumber of _listOfDest)

end repeat


end tell

May 3, 2016 7:53 AM in response to Michael Simpson1

Michael Simpson1 wrote:


I would still very much appreciate any help in the renaming with leading 000s after the _in the folder names.



Here's an example of one way to pad with leading 0s.


SG


set theFolderMain to "160202"

set theSubFolderNum to 1

set subFolderName to theFolderMain & "_" & ¬

("000" & theSubFolderNum)'s text -3 thru -1

-->"160202_001"

May 4, 2016 2:28 AM in response to SGIII

I have to accept the fact that i don't really understand how to implement your suggestion above SG.

I don't even know at which point within my script i should try and use the method! I would guess it needs to be after the folders have been created with this repeat:


repeat with i from 1 to subCount


tell application "Finder" to makenewfolderattargetFolderwith properties {name:JobName & "_" & i}


end repeat


Any more input would be very much appreciated as I'm pretty much stuck.

May 5, 2016 1:03 PM in response to Michael Simpson1

Assuming the rest of the script is working as you want and i is a counter for the number of subfolders, then you could do this:


repeat with i from 1 to subCount

tell application "Finder" to makenewfolderattargetFolderwith properties ¬

{name:JobName & "_" & ("00" & i)'s text -3 thru -1}

end repeat


text -3 thru -1 simply means take the last three characters of a string. So if i is 1 and you prepend "00" you get "001". Take the last three characters and you still have "010". If i is 10 and you prepend "00" you get "0010". Take the last three characters and you get "010". If i is 100 and you prepend "00" you get "00100". Take the last three characters of that and you get "100", etc.


SG

May 4, 2016 8:15 AM in response to SGIII

Thank you so much.


I realised that i needed to get the leading 0s into i somehow but don't understand the syntax at all.

This was exactly what I needed and it makes sense having looked at all the other scripts i have been trying to butcher to get this to work.


Now to learn how to tidy the rest of it up (particularly getting rid of the bit where I ask for the location of the folder the script has only just created!)


Thank you again!

May 4, 2016 8:56 AM in response to Michael Simpson1

So I've managed to reduced the script a little by loosing this:

set _destination to choose folderwith prompt "Choose the Main Folder Created above:"

And replacing the remainder with the following as i knew i already had targetFolder


tell application "Finder"


set filetoCopy to choose filewith prompt "Choose image file to copy to SubFolders:"

set _listOfDest to (every folder of targetFolder) as alias list

repeat with CurrentNumber from 1 to (count_listOfDest)


duplicatefilefiletoCopytofolder (itemCurrentNumber of _listOfDest)

end repeat


end tell


What i am wondering is whether i can reduce it further to include the copying of the image file into the earlier repeat?

It doesn't really matter to me if i can or not but just interested to learn more about scripting.


Mike

Applescript - creat a folder, subfolders and populate with sample image

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