Applescript to make new folder

試著忘記壹切 提交于 2019-12-19 10:53:17

问题


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

回答1:


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



回答2:


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.



回答3:


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



回答4:


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" 



回答5:


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


来源:https://stackoverflow.com/questions/4493335/applescript-to-make-new-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!