简体   繁体   English

如何从终端(osascript)设置文件夹动作?

[英]How to setup a folder action from the terminal(osascript)?

I'm trying to write an applescript that will prompt a dialog for selecting a folder, then another dialog for selecting an applescript file and then it will create a folder action attaching that script to that folder. 我正在尝试编写一个applescript,它将提示一个用于选择文件夹的对话框,然后是另一个对话框,用于选择一个applescript文件,然后它将创建一个将该脚本附加到该文件夹​​的文件夹操作。

    set thisFolder to (choose folder with prompt "Choose the folder to which you want to attach the action." default location (path to desktop))
set thisScript to (choose file with prompt "Choose the action you want to attach to the folder." )

tell application "System Events"
    set folder actions enabled to true
    -- Only one 'folder action' can be attached to any one folder at any one time, but each may contain one or more 'scripts'. It's the scripts' folder action handlers which are triggered, not the scripts themselves. So a single script may contain different handlers for different triggers or you can have several scripts.
    -- The folder path specified when creating an action can currently be either an HFS path or a POSIX path, BUT the value actually set is a POSIX path without a trailing slash — which is revelant for any script which may test folder action 'path' values.
    set thisAction to (make new folder action at end of folder actions with properties {path:(POSIX path of thisFolder)})
    -- Only the script's name is specified as it's assumed to be in one of the (~)/Library/Scripts/Folder Action Scripts/ folders.
    make new script at end of thisAction's scripts with properties {name:(get thisScript's name)}
end tell

When I try to run this it works fine till the last line "make new script..." then it fails with a -1000 code 当我尝试运行此命令时,它可以正常工作,直到最后一行“ make new script ...”为止,然后失败,并显示-1000代码

script.scpt:357:450: execution error: System Events got an error: AppleEvent handler failed (-10000)

The script that I'm trying to attach is the following: 我尝试附加的脚本如下:

on adding folder items to this_folder after receiving added_items
     display dialog "added"
 end adding folder items to

What could be the thing I'm doing wrong here? 我在这里做错了什么事?

Thanks for your time 谢谢你的时间

The syntax to attached folder action has been changed some time ago. 附加文件夹操作的语法在一段时间前已更改。 "Attached action" is depreciated and "make" is now used. “附加的操作”已弃用,现在使用“ make”。 The correct syntax is as follow : 正确的语法如下:

set thisFolder to (choose folder with prompt "select folder")
set thisScript to (choose file with prompt "Select script to attach")

tell application "Finder" to set ScripName to name of thisScript
set ScriptPath to POSIX path of thisScript

tell application "System Events"
    set thisAction to (make new folder action at end of folder actions with properties {path:(POSIX path of thisFolder)})
    tell thisAction to make new script at end of scripts with properties {name:ScripName, POSIX path:ScriptPath}
end tell

Warning : the make new folder action (1st instruction) will fail if this folder is already set for action (even if now script attached). 警告:如果已为此文件夹设置了操作(即使现在附加了脚本),则创建新文件夹操作(第一条指令)将失败。 Therefore, when testing, either you put it in try/end try block or you must delete action of this folder each time you are testing that script. 因此,在测试时,您可以将其放在try / end try块中,或者每次测试该脚本时都必须删除此文件夹的操作。

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

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