打开文件夹中最旧的文件

时间:2019-11-22 05:06:06

标签: applescript

我一直在寻找如何打开文件夹中最旧的文件。我已经制作了一个气象站,每天将数据记录到带有新文件的SD卡上。月和年是添加了“ WX”的文件名。每个文件都在SD卡上,因此位置始终相同(Finder中的WX数据)。所有文件均为CSV文件。打开文件后,我让AppleScript分析数据。当我打开文件并选择第一个单元格(A1)时,该部分有效。我希望能够插入SD卡,启动脚本并打开最旧的文件,对其进行分析,然后将结果粘贴到另一个Numbers电子表格中,从SD卡中删除文件,然后打开下一个重复直到所有文件被分析。我发现的所有代码示例都不起作用,或者需要使过程变得比我需要的复杂。

这是我到目前为止所拥有的。它会打开Numbers,但出现错误提示找不到文件:

set result to "WXDATA"
set sourceFolder to result

tell application "Finder"
    activate
    sort (sourceFolder) by creation date
    --sort (get files of sourceFolder) by creation date
    -- This raises an error if the folder doesn't contain any files
    set theFile to (item 1 of result) as alias
end tell

结果是:

  

别名“ WXDATA:”

在尝试其他操作时,我有一些注释掉了。

如何获取此文件以打开SD卡上列出的最早的文件?数字会打开,但看起来好像不知道要打开哪个文件。

1 个答案:

答案 0 :(得分:0)

如果文件夹为空,则必须对源文件夹的文件进行排序,并添加一个try块以处理错误

set diskName to "WXDATA"

tell application "Finder"
    set sourceFolder to disk diskName
    try
        set theFile to item 1 of (sort files of sourceFolder by creation date) as alias
    on error
        set theFile to missing value
    end try
end tell
if theFile is not missing value then
    tell application "Numbers" to open theFile
end if