如何使用 AppleScript 在 Finder 中引用文件
How to reference a file in Finder with AppleScript
我有一个非常简单的问题,我在谷歌上搜索了很长时间但无法解决:
tell application "Finder"
set videoFile to "Users:username:Desktop:No Backup:Music:video.mp4"
delete file videoFile
end tell
这会导致以下错误:
Finder got an error: Can’t get file "Users:username:Desktop:No
Backup:Music:video.mp4".
如果我尝试“打开文件”或其他命令而不是“删除”,也会发生同样的情况。
但是,以下命令确实可以正确打开文件:
tell application "QuickTime Player"
set videoFile to "Users:username:Desktop:No Backup:Music:video.mp4"
open videoFile
end tell
这会打开视频,所以文件路径必须正确。
我还搜索了所有 Applescript Finder 命令的列表,但找不到任何看起来像适当文档的东西。
编辑:我也试过了
我尝试了 delete (every item of folder folderPath whose name is "video.mp4")
,但这导致了 Finder got an error: Can’t get folder "Users:username:Desktop:No Backup:Music".
HFS 路径必须始终 以磁盘名称开头(将Macintosh HD
替换为真实名称)
tell application "Finder"
set videoFile to "Macintosh HD:Users:username:Desktop:No Backup:Music:video.mp4"
delete file videoFile
end tell
不过有一种方便的方法就是使用相对路径,不管启动盘的名称和用户名如何
set desktopFolder to path to desktop as text -- returns "Macintosh HD:Users:username:Desktop:"
tell application "Finder"
set videoFile to desktopFolder & "No Backup:Music:video.mp4"
delete file videoFile
end tell
对于当前用户的桌面文件夹,还有更短的方法,因为桌面文件夹是 Finderroot 文件夹
tell application "Finder"
delete file "video.mp4" of folder "No Backup:Music:"
end tell
解决方案是将“Macintosh HD:”添加到文件路径的开头——这让我很困惑,因为这在不同的应用程序之间表现不一致
我有一个非常简单的问题,我在谷歌上搜索了很长时间但无法解决:
tell application "Finder"
set videoFile to "Users:username:Desktop:No Backup:Music:video.mp4"
delete file videoFile
end tell
这会导致以下错误:
Finder got an error: Can’t get file "Users:username:Desktop:No Backup:Music:video.mp4".
如果我尝试“打开文件”或其他命令而不是“删除”,也会发生同样的情况。
但是,以下命令确实可以正确打开文件:
tell application "QuickTime Player"
set videoFile to "Users:username:Desktop:No Backup:Music:video.mp4"
open videoFile
end tell
这会打开视频,所以文件路径必须正确。
我还搜索了所有 Applescript Finder 命令的列表,但找不到任何看起来像适当文档的东西。
编辑:我也试过了
我尝试了 delete (every item of folder folderPath whose name is "video.mp4")
,但这导致了 Finder got an error: Can’t get folder "Users:username:Desktop:No Backup:Music".
HFS 路径必须始终 以磁盘名称开头(将Macintosh HD
替换为真实名称)
tell application "Finder"
set videoFile to "Macintosh HD:Users:username:Desktop:No Backup:Music:video.mp4"
delete file videoFile
end tell
不过有一种方便的方法就是使用相对路径,不管启动盘的名称和用户名如何
set desktopFolder to path to desktop as text -- returns "Macintosh HD:Users:username:Desktop:"
tell application "Finder"
set videoFile to desktopFolder & "No Backup:Music:video.mp4"
delete file videoFile
end tell
对于当前用户的桌面文件夹,还有更短的方法,因为桌面文件夹是 Finderroot 文件夹
tell application "Finder"
delete file "video.mp4" of folder "No Backup:Music:"
end tell
解决方案是将“Macintosh HD:”添加到文件路径的开头——这让我很困惑,因为这在不同的应用程序之间表现不一致