是否可以自动将脚本附加到文件夹 [英] Is it possible to automate attaching a script to a folder

查看:105
本文介绍了是否可以自动将脚本附加到文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have new folders added daily. Is it possible to link an existing folder action script to the new folder automatically?

推荐答案

由于某些原因它们一直被隐藏,但是系统事件脚本中有attach action toremove action from命令字典.语法在Snow Leopard时期也有所变化,但也无济于事.用于附加文件夹动作脚本的通用处理程序(在Mojave中测试)将类似于:

They have always been hidden for some reason, but but there are attach action to and remove action from commands in the System Events scripting dictionary. The syntax also changed a bit around the time of Snow Leopard, which didn't help either. A general purpose handler for attaching folder action scripts (tested in Mojave) would be something like:

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

这篇关于是否可以自动将脚本附加到文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆