Applescript制作新文件夹

问题描述 投票:1回答:5

我想在Apple脚本中创建一个新的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
macos scripting applescript
5个回答
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
投票
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.

0
投票

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

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

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

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

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" 

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“”设置最前面的“真实”,你的脚本是有用的

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
© www.soinside.com 2019 - 2024. All rights reserved.