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?

2

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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")

1 Comment

Here you can see desktopPath as variable but you can put any other path with slashes. Correct me if I'm wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.