Applescript制作新文件夹

时间:2010-12-20 19:54:33

标签: macos scripting applescript

我想在applescript中创建一个新的Folder命令

为什么这个脚本不起作用?

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

5 个答案:

答案 0 :(得分:24)

您可以使用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

答案 1 :(得分: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.

答案 2 :(得分:1)

我不知道在AppleScript中运行bash命令是否在作弊,但你也可以这样做:

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

当您需要在尚不存在的情况下即时创建子文件夹时,这非常有用:

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

答案 3 :(得分:0)

注意:失败有两个原因。
(1)单引号中的'〜'不会解析。
(2)“ / New Folder /”中的空格将破坏路径。

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

已解决:

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

答案 4 :(得分:0)

您可以通过模拟击键(“ N”以及命令和移动)直接使用applescript脚本,这将在桌面或打开的Finder窗口中创建一个新文件夹。

在脚本下方,您可以在脚本编辑器中对其进行测试

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

如果您在“告诉过程” Finder下添加脚本,则您的脚本将起作用 “将最前面设置为true” 哪个给

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