插入USB驱动器后如何自动运行AppleScript?

Kit*_*ton 2 applescript

我需要重命名并填充大约70个USB记忆棒.如果Applescript在插入时自动运行会使其更加简单.

kop*_*hke 6

连接到OS X计算机的任何外部驱动器都已安装到/Volumes.如果您在该文件夹中进行更改,则可以选择添加的外部驱动器并对其进行处理.运行此代码:

property ignoredVolumes : {"Macintosh HD", "Time Machine Backups"} -- add as needed
tell application "System Events"
    set rootVolume to disk item (POSIX file "/Volumes" as text)
    set allVolumes to name of every disk item of rootVolume
    repeat with aVolume in allVolumes
        if aVolume is not in ignoredVolumes then
            set name of disk item (path of rootVolume & aVolume) to newName
        end if
    end repeat
end tell
Run Code Online (Sandbox Code Playgroud)

将重命名不在ignoredVolumes列表中的驱动器(拔掉除了您要忽略的驱动器,ls /Volumes在终端中运行并将名称添加到属性中)newName.要在每次更改时触发此操作,请将代码修改为Stay-Open脚本应用程序:

property pollIntervall : 60 -- in seconds
property ignoredVolumes : {…} -- from above

on run
    my checkVolumes()
end run

on idle
    my checkVolumes()
    return pollInterval
end idle

on checkVolumes()
    tell … end tell -- from above
end checkVolumes
Run Code Online (Sandbox Code Playgroud)

并将其保存在AppleScript编辑器中(选择"AppleScript应用程序",确保勾选"保持打开状态").一旦启动,脚本将继续运行,on idle每秒执行一次处理程序pollInterval.

如果您基本上只进行一次一次批处理作业,这将很好.如果您想要一个不依赖于运行保持打开的脚本应用程序的更永久的解决方案,您也可以