AppleScript在Finder中设置目录路径 [英] AppleScript set directory path in Finder

查看:177
本文介绍了AppleScript在Finder中设置目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过AppleScript删除计算机上的文件.当我应用下面的代码时,似乎从桌面删除了文件.我想删除"/Users/andrew/Documents"中的文件.这是下面的代码,该代码从桌面删除文件:

I'm trying to delete a file on my computer via AppleScript. When I apply the code below, it seems to delete the file from desktop. I'd like to delete the file in the "/Users/andrew/Documents". This is the code below which deletes the file from the desktop:

tell application "Finder"
    if exists file "new.mp3" then
        delete file "new.mp3"
    end if
end tell

这是尝试过的方法,但不起作用:

This is what tried and it doesn't work:

tell application "Finder"
    if exists file "/Users/andrew/Documents/new.mp3" then
        delete file "/Users/andrew/Documents/new.mp3"
    end if
end tell

如果有人可以提出任何建议,将不胜感激!

If anyone could any advice it would be much appreciated!!

推荐答案

在获取文件对象的路径之前添加POSIX file:

Add POSIX file before the path to get a file object:

tell application "Finder"
    set f to POSIX file "/Users/username/Documents/new.mp3"
    if exists f then delete f
end tell

system attribute "HOME"替换为/Users/username:

set f to POSIX file ((system attribute "HOME") & "/Documents/new.mp3")
tell application "Finder" to if exists f then delete f

或使用OS X之前的路径格式:

Or use the pre-OS X path format:

tell application "Finder"
    set f to "Macintosh HD:Users:username:Documents:new.mp3"
    -- set f to (path to documents folder as text) & "new.mp3"
    if exists f then delete f
end tell

这篇关于AppleScript在Finder中设置目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆