AppleScript教程:保持运行脚本与文件夹操作

需积分: 44 37 下载量 164 浏览量 更新于2024-08-08 收藏 2.4MB PDF 举报
"本文档是AppleScript的基础教程,讲解了如何创建保持打开的脚本应用程序以及文件夹操作的原理和应用。" 在AppleScript中,创建一个保持打开的脚本应用程序能够实现持续运行的任务,这在自动化工作流程或监控特定事件时非常有用。要创建这样的脚本,你需要将脚本保存为应用程序,并在设置中勾选“保持打开”。这会启用两个关键的事件处理器:`idle` 和 `quit`。 `idle` 事件处理器在应用程序空闲时执行,常用于处理后台任务。默认情况下,它每30秒执行一次,但你可以通过在`idle`事件中使用`return n`来定制间隔时间,比如`return 10`会让脚本每10秒执行一次。下面是一个简单的`idle`事件示例,它会每5秒蜂鸣两次并显示一个对话框: ```applescript on idle beep 2 display dialog "程序正在运行" giving up after 1 return 5 end idle ``` `quit` 事件处理器则在用户尝试退出应用程序时触发。在这个事件中,如果想要正常退出程序,必须包含`continue quit`命令。例如,下面的`quit`事件会询问用户是否确认退出: ```applescript on quit display dialog "真的要退出?" buttons {"是的", "不"} if button returned of result = "是的" then continue quit end if end quit ``` 如果直接在AppleScript编辑器中运行,可能看不到预期效果,需要保存并双击运行应用程序。 接下来,教程提到了文件夹操作(Folder Actions),这是Finder的一个功能,当文件夹内的文件或文件夹发生变化时,关联的AppleScript脚本会被触发执行。这种特性可以用来自动化文件管理,比如监控新文件的添加、删除或修改,然后根据这些变化执行相应的操作。 例如,你可以创建一个脚本监听特定文件夹,每当有新文件添加时,自动进行备份或者发送通知。要设置文件夹操作,需要在Finder中右键点击目标文件夹,选择“服务”->“添加文件夹动作”并关联一个AppleScript脚本。 AppleScript作为MacOS中的脚本语言,简单易学且功能强大,适用于各种自动化任务和应用程序间的交互。通过学习和实践,用户可以大大提高工作效率,减轻重复性工作的负担。本教程以简洁的形式介绍了AppleScript的基础知识,包括语法、事件处理和实用技巧,旨在帮助初学者快速掌握这一工具。

解释:% 'Distance' - Distance measure, in P-dimensional space, that KMEANS % should minimize with respect to. Choices are: % {'sqEuclidean'} - Squared Euclidean distance (the default) % 'cosine' - One minus the cosine of the included angle % between points (treated as vectors). Each % row of X SHOULD be normalized to unit. If % the intial center matrix is provided, it % SHOULD also be normalized. % % 'Start' - Method used to choose initial cluster centroid positions, % sometimes known as "seeds". Choices are: % {'sample'} - Select K observations from X at random (the default) % 'cluster' - Perform preliminary clustering phase on random 10% % subsample of X. This preliminary phase is itself % initialized using 'sample'. An additional parameter % clusterMaxIter can be used to control the maximum % number of iterations in each preliminary clustering % problem. % matrix - A K-by-P matrix of starting locations; or a K-by-1 % indicate vector indicating which K points in X % should be used as the initial center. In this case, % you can pass in [] for K, and KMEANS infers K from % the first dimension of the matrix. % % 'MaxIter' - Maximum number of iterations allowed. Default is 100. % % 'Replicates' - Number of times to repeat the clustering, each with a % new set of initial centroids. Default is 1. If the % initial centroids are provided, the replicate will be % automatically set to be 1. % % 'clusterMaxIter' - Only useful when 'Start' is 'cluster'. Maximum number % of iterations of the preliminary clustering phase. % Default is 10. %

2023-05-18 上传