如何判断拖拽对象是否为已挂载卷?AppleScript回收站替代工具开发
桌面回收站替代工具(AppleScript实现)
我帮你完善并整理了满足需求的AppleScript代码,它能实现你想要的三个核心功能:
- 双击时快速打开用户回收站(~/.Trash)
- 拖拽文件/文件夹时自动移至回收站
- 拖拽已挂载的卷时直接弹出该卷
完整可运行代码
-- 桌面回收站替代工具 on run -- 双击触发:打开用户回收站 tell application "Finder" open folder ".Trash" of home activate end tell end run on open droppedItems repeat with itemToProcess in droppedItems set itemPOSIXPath to POSIX path of itemToProcess -- 判断是否为已挂载的外部卷 if itemPOSIXPath starts with "/Volumes/" and itemPOSIXPath ends with "/" then -- 弹出目标卷 tell application "Finder" eject disk (name of itemToProcess) end tell else -- 将文件/文件夹移至回收站(和系统删除逻辑一致) tell application "Finder" delete itemToProcess end tell end if end repeat end open
关键逻辑说明
- 双击触发逻辑:
on run是AppleScript的默认启动事件,当你双击这个脚本(或者导出的app)时,会自动调用Finder打开你的个人回收站,同时激活Finder窗口,方便直接操作。 - 拖拽处理逻辑:
on open droppedItems负责接收拖拽进来的所有项目,通过遍历逐个处理:- 用POSIX路径判断是否为挂载卷:所有外部挂载卷都会放在
/Volumes/目录下,且路径以/结尾,通过这个特征可以准确识别。 - 移至回收站:直接调用Finder的
delete命令,这和你右键选择「移到废纸篓」的系统行为完全一致,不会直接删除文件,而是放到回收站里。 - 弹出卷:调用Finder的
eject命令,安全卸载外部卷,避免数据丢失。
- 用POSIX路径判断是否为挂载卷:所有外部挂载卷都会放在
使用提示
把这段代码保存为AppleScript文件(.scpt),然后导出为应用程序(在Script Editor里选择「文件」→「导出」→ 格式选「应用程序」),之后把这个app放到桌面上,就可以像使用原生回收站一样操作了。
内容的提问来源于stack exchange,提问作者user7450368
相关产品推荐
相关产品推荐

