Applescript: create a new folder structure if it does not exist

I searched through message boards, trying to figure out how to execute this script. Essentially, the goal is to be able to run this script while inside a folder, and if certain folders do not exist, these folders will then be created. If they already exist, nothing happens. Here is what I still remember:

property archivesFolder : "Archives" property imagesFolder : "Images" property proofreadFolder : "Proofreading" property proofFolder : "Proofs" property sourceFolder : "Source" try tell application "Finder" to set theLocation to (folder of the front window as alias) end try tell application "Finder" if (exists folder archivesFolder) then (* do nothing *) else make new folder at theLocation with properties {name:archivesFolder} end if if (exists folder imagesFolder) then (* do nothing *) else make new folder at theLocation with properties {name:imagesFolder} end if if (exists folder proofreadFolder) then (* do nothing *) else make new folder at theLocation with properties {name:proofreadFolder} end if if (exists folder proofFolder) then (* do nothing *) else make new folder at theLocation with properties {name:proofFolder} end if if (exists folder sourceFolder) then (* do nothing *) else make new folder at theLocation with properties {name:sourceFolder} end if end tell 

What am I doing wrong? (forgive my formatting of the n00b code, at work and can't figure out how to create code blocks). In addition, is this possible not only in the front window, but also in the folder that you just selected? Any help provided would be awesome.

+4
source share
4 answers

You can also use the shell script with mkdir since the option to create staging folders will not be an error if the folder already exists.

 # define a list of folders - items will need to be quoted if they contain spaces, etc. property theFolders : {"Archives", "Images", "ProofReading", "Proofs", "Source"} -- can also nest, eg "Inside/One" try tell application "Finder" to set targetFolder to (target of the front window) as alias on error -- no window set targetFolder to (choose folder) end try # build a parameter string from the folder list set {tempTID, AppleScript text item delimiters} to {AppleScript text item delimiters, space} set {theFolders, AppleScript text item delimiters} to {theFolders as text, tempTID} do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders 
+4
source

I suggest two options (for running a script):

Option 1 Take this code (provided that it does what you are planning) and save it as an application (using the Script Editor).

Then just drag this application onto the window toolbar (you need the toolbar to be visible). To do this, hold the key while dragging.

Option 2 : use Butler: http://manytricks.com/butler/ (there is a free version, I don’t know your OSX version).

It allows you to define system-wide nodal keys for applescript.

Create a smart element (applescript); paste the code there and add the keyboard shortcuts in the name Script: example: create folder here ⇧βŒ₯⌘N

EDIT:

According to your comment, I understand your problem, and I can tell you that you are missing the path (the current folder in your case is theLocation )

So, in each case, if (exists folder archivesFolder) then you need to add of theLocation as follows: if not (exists folder archivesFolder of theLocation) then

Finally, knowing that you will not do anything if the folder exists, just check the false case.

I tested this code and posted it here:

 property archivesFolder : "Archives" property imagesFolder : "Images" property proofreadFolder : "Proofreading" property proofFolder : "Proofs" property sourceFolder : "Source" try tell application "Finder" to set theLocation to (folder of the front window as alias) end try tell application "Finder" if not (exists folder archivesFolder of theLocation) then make new folder at theLocation with properties {name:archivesFolder} end if if not (exists folder imagesFolder of theLocation) then make new folder at theLocation with properties {name:imagesFolder} end if if not (exists folder proofreadFolder of theLocation) then make new folder at theLocation with properties {name:proofreadFolder} end if if not (exists folder proofFolder of theLocation) then make new folder at theLocation with properties {name:proofFolder} end if if not (exists folder sourceFolder of theLocation) then make new folder at theLocation with properties {name:sourceFolder} end if end tell 
+3
source

Try the following:

 tell application "Finder" set theLocation to (target of front window) as string set folderNames to {"archivesFolder", "imagesFolder", "proofreadFolder", "proofFolder", "sourceFolder"} repeat with theFolder in folderNames try make new folder at theLocation with properties {name:"" & theFolder & ""} end try end repeat end tell 
0
source

This is a script I did to sort a bunch of medical files in subfolders based on the service date (explaining the β€œtext 5 to 14” in the script, the files are named according to the template, so the script can extract the service date from the file name). Instead of checking if a folder exists, I simply put the make statement of the folder in a try block; if the folder already exists, "try" fails, but the script continues on the assumption that the folder already exists. Using the text element instead of character returns the extracted string as a single string, and not as an array of characters that need to be converted back to a string.

 tell application "Finder" set theDirectory to "Internal Disk:Users:steven:Documents:Vondalee:Medical" set theFiles to every file in folder theDirectory whose name extension is "pdf" repeat with eachFile in theFiles set theFileName to the name of eachFile set theFolder to text 5 thru 14 of theFileName try make new folder at theDirectory with properties {name:theFolder} end try move eachFile to folder theFolder of folder theDirectory end repeat end tell 
0
source

Source: https://habr.com/ru/post/1398285/


All Articles