当前位置: 首页 > 知识库问答 >
问题:

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

宋英杰
2023-03-14

我试图写一个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

当我试着运行这个程序时,它运行良好,直到最后一行“生成新脚本…”然后它以-1000代码失败

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

我试图附加的脚本如下:

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

我到底做错了什么?

谢谢你抽出时间

共有1个答案

申屠鹏
2023-03-14

附加文件夹操作的语法已在一段时间前更改。“附加动作”被折旧,现在使用“make”。正确的语法如下所示:

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

警告:如果此文件夹已经设置为操作(即使现在附加了脚本),制作新文件夹操作(第一条指令)将失败。因此,在测试时,要么将其放在try/end try块中,要么每次测试该脚本时都必须删除此文件夹的操作。

 类似资料:
  • 本文向大家介绍python实现操作文件(文件夹),包括了python实现操作文件(文件夹)的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了pyhton操作文件的具体代码,供大家参考,具体内容如下 copy_file 功能:将某个文件夹下的所有文件(文件夹)复制到另一个文件夹 zip_file 功能:将某个文件夹下面的所有文件(文件夹)压缩 del_file 功能:将某个文件夹下面的所

  • 我正在尝试使用jib-maven-plugin构建docker映像,我想为特定文件夹设置权限。如果我使用的是docker文件,配置将如下所示: 现在,我该如何使用jib maven插件实现这一点?我相信在波姆的某个地方。jib maven插件中的xml

  • 在批处理脚本中,可以使用任何编程语言执行普通的基于文件夹的操作。 以下是一些可以在文件夹上执行的操作。 创建文件夹 列出文件夹 遍历文件夹中的文件 删除文件夹 重命名文件夹

  • 在入口页面设置并开启 session 【文件形式】 文件 index.php <?php //session 存储类型,不定义默认 file define('PG_SESSION_TYPE' , 'file'); //session 存储位置 不定义 PG_SESSION_DIR 则session文件默认保存文件夹为 ./sessions define('PG_SESSION_DIR' , './

  • 我使用-dcucumber.options运行test by mvn命令行,以重写prject的选项: 但显示错误:

  • Section " SetFileAttributes" SetFileAttributes "C:\demo" HIDDEN ;设置文件夹属性为隐藏 SetFileAttributes "C:\demo\demo.txt" HIDDEN|READONLY ;设置文件属性为隐藏,只读 SetFileAttributes "C:\demo\demo.txt" NORMAL ;恢复