1

The supposed duplicate question explains how to DELETE A FILE but I need to CREATE ONE (OR MORE) NONEXISTENT DIRECTORIES a completely different task!

As a followup to my previous (solved) question Can Applescript be used to tell whether or not a directory (path) exists?

I now need to know how to create any directories in the path that do not already exist?

Community
  • 1
  • 1
Bryan Dunphy
  • 799
  • 1
  • 10
  • 22
  • Possible duplicate of [AppleScript set directory path in Finder](http://stackoverflow.com/questions/16124232/applescript-set-directory-path-in-finder) – VMeijer Feb 06 '17 at 15:30
  • Possible duplicate of [Applescript to make new folder](http://stackoverflow.com/questions/4493335/applescript-to-make-new-folder) – Darrick Herwehe Feb 06 '17 at 16:24

3 Answers3

1

The easiest way is to use the shell, mkdir -p creates the folder only if it does not exist.

do shell script "mkdir -p  ~/Desktop/TestFolder"

However there is a caveat: If there is a space character in the path you need to replace every space with two backslashes because the usual quoted of does not expand the tilde.

do shell script "mkdir -p  ~/Desktop/Test\\ Folder"

Alternatively

set thePath to "~/Desktop/Test Folder ABC"
if thePath starts with "~" then
    set quotedPath to text 1 thru 2 of thePath & quoted form of (text 3 thru -1 of thePath)
else
    set quotedPath to quoted form of thePath
end if

do shell script "mkdir -p  " & quotedPath
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Add POSIX file before the path to get a file object:

tell application "Finder"
    set f to POSIX file "/Users/username/Documents/new.mp3"
    if exists f then delete f
end tell

system attribute "HOME" is replaced with /Users/username:

set f to POSIX file ((system attribute "HOME") & "/Documents/new.mp3")
tell application "Finder" to if exists f then delete f

Or use the pre-OS X path format:

tell application "Finder"
    set f to "Macintosh HD:Users:username:Documents:new.mp3"
    -- set f to (path to documents folder as text) & "new.mp3"
    if exists f then delete f
end tell

Bron: AppleScript set directory path in Finder

Community
  • 1
  • 1
VMeijer
  • 415
  • 1
  • 5
  • 15
0

If your question is still:

"CREATE ONE (OR MORE) NONEXISTENT DIRECTORIES a completely different task?"

In order to manage my folders, I use those lines in the relevant case:

Create all the folders from "a" to "e". If folder "a" already exists then from "b" to "e". etc...

set mkdirFolder to "mkdir -p " & desktopPath & "a/b/c/d/e/"

do shell script mkdirFolder

Create a folder "a" if not exists and folders "b to e" at his top-level

set mkdirFolder to "mkdir -p " & desktopPath & "a/{b,c,d,e}/"
        do shell script mkdirFolder

Create folder with a part of the name

-- (Note the single quotes round the space to mark it as part of the name.)
    set mkdirFolder to "mkdir -p " & desktopPath & "a/Chapter' '{1,2,3,4}/"

do shell script mkdirFolder

result--> Folders "Chapter 1", "Chapter 2", "Chapter 3", and "Chapter 4" are created in folder "a"

You can find more informations here (Learn more about creating folder with "mkdir")