使用 AppleScript 打开最新下载的 Excel CSV 文件

Opening the latest downloaded CSV file with Excel using AppleScript

我需要找到一种使用 excel

自动打开上次下载的 CSV 文件的方法

我一直在努力寻找一些代码并根据我的需要对其进行调整,但它不起作用

下面的代码将按预期打开带有数字的最新文件。我无法更改 CSV 文件的默认数字使用方式

tell application "Finder"   
    open last item of (sort (get files of (path to downloads folder)) by creation date) 
end tell

当我更改为告诉应用程序 "excel" 时,它 returns 出错。

"Expected “,” but found “by”."

感谢您的帮助!

您必须将 Finder 项强制转换为文件路径。这个路径引用可以用 Excel

打开
tell application "Finder"
    set latestFile to last item of (sort (get files of (path to downloads folder)) by creation date) as text
end tell
tell application "Microsoft Excel" to open latestFile

Finder 的 open 命令有一个 using 参数,可让您指定打开文件的应用程序。即,这样编码:

tell application "Finder"
    set theFolder to folder (path to downloads folder)
    set latestFile to last item of (sort (files of theFolder) by creation date)
    open latestFile using (path to application "Microsoft Excel")
end tell