【问题标题】:Is it possible to automate attaching a script to a folder是否可以自动将脚本附加到文件夹
【发布时间】:2019-10-02 10:29:23
【问题描述】:

我每天都会添加新文件夹。是否可以将现有文件夹操作脚本自动链接到新文件夹?

【问题讨论】:

  • 是的,有可能。

标签: automation applescript


【解决方案1】:

由于某种原因,它们一直被隐藏,但 系统事件 脚本字典中有 attach action toremove action from 命令。语法在 Snow Leopard 出现时也发生了一些变化,但这也没有帮助。用于附加文件夹操作脚本(在 Mojave 中测试)的通用处理程序将类似于:

on run -- example
    set theFolder to (choose folder with prompt "Choose folder to attach:" default location path to desktop)
    set theScript to (choose file with prompt "Choose folder action script:" default location path to Folder Action scripts)
    tell application "System Events" to set theName to name of theScript
    attachAction(theFolder, theName)
end run

on attachAction(folderPath, scriptName)
    (*
        attach a script from "~/Library/Scripts/Folder Action Scripts" to a folder
            parameters -        folderPath [text or alias]: the folder to attach to
                                scriptName [text]: the name of the script to attach
            returns [boolean]:  true if successfully attached, false otherwise
        *)
    tell application "System Events"
        try -- enable existing folder action
            set folderName to name of disk item (folderPath as text)
            set enabled of folder action folderName to true
        on error errmess -- create new
            log errmess
            try
                make new folder action at end of folder actions with properties {enabled:true, name:folderName, path:folderPath}
            on error errmess -- oops (bad path, etc)
                log errmess
                return false
            end try
        end try
        try -- enable existing folder action script
            tell folder action folderName to set enabled of script scriptName to true
        on error errmess -- create new
            log errmess
            try
                tell folder action folderName to make new script at end of scripts with properties {name:scriptName}
            on error errmess -- oops (bad name, etc)
                log errmess
                return false
            end try
        end try
        return true
    end tell
end attachAction

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 2020-04-02
    • 1970-01-01
    相关资源
    最近更新 更多