使用Applescript覆盖现有文件

时间:2018-01-25 20:37:04

标签: applescript

我有一个简短的AppleScript,可以从Papers3.app导出书目信息。这很简单。但是,它在没有输出文件时第一次工作。但是,如果我想更新输出文件(通过覆盖它),它不起作用。

我是否需要告诉Applescript覆盖当前文件(如果它已经存在)?

tell application "Papers"
    set outFile to "/Users/alex/Dropbox/Papers3_Citations/Bibliography-Master.bib"
    export ((every publication item) as list) to outFile
end tell

1 个答案:

答案 0 :(得分:0)

首先检查并删除文件,然后将其导出:

set outFile to "/Users/alex/Dropbox/Papers3_Citations/Bibliography-Master.bib"

tell application "System Events" to if (exists file outFile) then delete file outFile

tell application "Papers" to export ((every publication item) as list) to outFile

注意:文件会立即删除,不会放入垃圾箱!

如果您想要安全,请使用 Finder 文件放入已删除邮件

set outFile to "/Users/alex/Dropbox/Papers3_Citations/Bibliography-Master.bib"

set itExists to application "System Events"'s (exists file outFile)
if itExists then
    set thisFile to POSIX file outFile as alias
    tell application "Finder" to move thisFile to trash
end if

tell application "Papers" to export ((every publication item) as list) to outFile