macos Applescript 制作新文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4493335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:28:43  来源:igfitidea点击:

Applescript to make new folder

macosscriptingapplescript

提问by help

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

回答by mipadi

You can do it more directly with AppleScript:

您可以更直接地使用 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

回答by ryan Delaney

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.

回答by Blaise

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

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

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

回答by Mr. Science

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

注意:这可能由于两个原因而失败;
(1) '~' 被困在单引号中不会解析。
(2) '/New Folder/' 中的空格会破坏路径。

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

SOLVED:

解决了:

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

回答by deek5

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.

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

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

如果您在“告诉进程”Finder“”“set frontmost to 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