简体   繁体   English

Applescript制作新文件夹

[英]Applescript to make new folder

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

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

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

NOTE: This can fail for two reasons; 注意:这可能由于两个原因而失败:
(1) '~' trapped in singlequote won't parse. (1)单引号中的'〜'不会解析。
(2) space in '/New Folder/' will break the path. (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" 

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”以及命令和转换)直接使用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 如果您在“告诉过程”查找器下添加“将最前面设置为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM