【问题标题】:AppleScript set directory path in FinderAppleScript 在 Finder 中设置目录路径
【发布时间】:2013-04-20 19:21:42
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: macos applescript finder


    【解决方案1】:

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

    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
    

    或者使用 pre-OS X 路径格式:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多