2

I Want to make a new Folder command in apple script

Why dosent this script work?

tell application "Finder"
activate
end tell
tell application "System Events"
tell process "Finder"
    tell menu bar 1
        tell menu bar item "File"
            tell menu "File"
                click menu item "New folder"
            end tell
        end tell
    end tell
end tell
end tell
markratledge
  • 17,322
  • 12
  • 60
  • 106
help
  • 79
  • 2
  • 2
  • 5

6 Answers6

28

You can do it more directly with AppleScript:

tell application "Finder"
    set p to path to desktop -- Or whatever path you want
    make new folder at p with properties {name:"New Folder"}
end tell
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • How do I first check that some folder (here: `"New Folder"`) does not exists at a path (here: `p`)? I'm getting _error "Finder got an error: The operation can’t be completed because there is already an item with that name." number -48_ – luckydonald Aug 22 '19 at 10:57
2

I don't know if running bash commands within AppleScript is cheating, but you can also do:

do shell script "mkdir ~'/Desktop/New Folder'"  

Which is useful when you need to create sub folders on-the-fly when they don't exist yet:

do shell script "mkdir -p ~'/Desktop/New Folder/Bleep/Bloop'"  
Blaise
  • 13,139
  • 9
  • 69
  • 97
1
tell application "Finder"
activate
end tell
tell application "System Events"
tell process "Finder"
    tell menu bar 1
        tell menu bar item "File"
            tell menu "File"
                click menu item "new folder"
            end tell
        end tell
    end tell
end tell
end tell

--you capitalize the N in new folder the new folder button is not capped.
Community
  • 1
  • 1
1
tell application "Finder"
    set thepath to alias "Macintosh HD:Users:JasonMagnuson:Documents:" as text
    make new folder at thepath with properties {name:"nov_archive"}
end tell
James Risner
  • 5,451
  • 11
  • 25
  • 47
jason m
  • 11
  • 1
  • I tried this and it worked for me; however, I have the latest Mac OS update Ventura 13.0. – jason m Oct 31 '22 at 12:45
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Nov 04 '22 at 00:05
  • I strongly recommend this approach. It is more reliable than UI scripting. – Atsuo Sakakihara Mar 25 '23 at 16:43
0

NOTE: This can fail for two reasons;
(1) '~' trapped in singlequote won't parse.
(2) space in '/New Folder/' will break the path.

do shell script "mkdir -p '~/Desktop/New Folder/Bleep/Bloop'"

SOLVED:

do shell script "mkdir -p ~/Desktop/" & quoted form of "New Folder/Bleep/Bloop" 
0

You can directly with an applescript script by simulating keystroke on ("N" and command and shift) this will create a new folder on the desktop or in the open Finder window.

Below the script, you can test it in the script editor

tell application "System Events" to tell process "Finder"
    set frontmost to true
    keystroke "N" using {command down, shift down}
end tell

Your script works if you add under "tell process" Finder " "set frontmost to true" Which give

tell application "System Events"
    tell process "Finder"
        set frontmost to true
                tell menu bar 1
            tell menu bar item "File"
                tell menu "File"
                    click menu item "New folder"
                end tell
            end tell
        end tell
    end tell
end tell
deek5
  • 367
  • 1
  • 13