【问题标题】:Applescript to make new folderApplescript制作新文件夹
【发布时间】:2011-05-28 11:00:16
【问题描述】:

我想在苹果脚本中创建一个新的文件夹命令

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

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


    【解决方案1】:

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

    脚本下方,可以在脚本编辑器中测试

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

    如果您在“tell process” 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
    

    【讨论】:

      【解决方案2】:

      注意:这可能会因两个原因而失败;
      (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" 
      

      【讨论】:

        【解决方案3】:

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

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

        当您需要在子文件夹尚不存在时即时创建子文件夹时,这很有用:

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

        【讨论】:

          【解决方案4】:
          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.
          

          【讨论】:

            【解决方案5】:

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

            【讨论】:

            • 如何首先检查某个文件夹(此处:"New Folder")在路径(此处:p)中不存在?我收到 error “Finder 出错:操作无法完成,因为已经有一个具有该名称的项目。”数字-48
            猜你喜欢
            • 2010-10-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-17
            • 2011-06-12
            • 2012-08-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多