【问题标题】:getting last modified date for file in AppleScript在 AppleScript 中获取文件的最后修改日期
【发布时间】:2013-09-27 18:59:10
【问题描述】:

我正在尝试获取 AppleScript 中文件的最后修改日期。我以为我可以使用:

set thePath to (((path to documents folder) as text) & "speed.txt")
set modDate to modification date of file thePath

这似乎返回了一个有效值,但是当我把它放在on idle 的代码中时,我得到一个:

“无法获得类 of...”错误

我在其他地方看到了使用建议:

set the modDate to (do shell script "mdls -name kMDItemLasUsedDate " & quoted form of the POSIX path of thePath)

但这会返回null。关于如何获得修改日期的任何想法?

【问题讨论】:

  • 必须是 applescript 吗?您可以使用 stat filename 轻松做到这一点
  • 仅供参考:对于您的 do shell 脚本命令,有一个“kMDItemContentModificationDate”可以工作。

标签: file applescript last-modified


【解决方案1】:

您需要引用该文件。

试试

set thePath to (((path to documents folder) as text) & "speed.txt")
tell application "System Events" to set modDate to modification date of file thePath

tell application "System Events" to set thePath to file (((path to documents folder) as text) & "speed.txt")
set modDate to modification date of thePath

【讨论】:

  • 我建议的唯一调整是告诉应用程序系统事件而不是 Finder。一般来说,最好尽可能避免使用 Finder。因此,由于 Finder 和系统事件都具有“修改日期”属性,我会使用后者。
  • 另外,我很惊讶您的第二个示例有效,因为“修改日期”不是 applescript 知道的属性。 Finder 和系统事件都知道该属性,因此您真的应该告诉其中一个应用程序执行该命令。我确实看到,当您使用在文件引用末尾包含“应用程序查找器”的应用程序之一创建文件引用时......我猜这就是为什么“修改日期”调用在应用程序之外起作用的原因告诉堵塞。你有不同的解释吗?
  • 嗨轩辕。系统事件更快并且也接受 posix 路径,所以我会更新答案。 (我有一个填充 Finder 的脚本调试器扩展,我很懒惰。)我发现一旦你在 Finder 或 SE 中引用了一个文件,你就可以在之后将它从一个 tell 块中排除。
  • 有趣的帖子。我注意到脚本返回如下字符串: date "Wednesday, 12 August 2015 14:13:33" 我假设这是特定于语言/地区的,那么如何将其转换为已知格式以便通过解析?
猜你喜欢
  • 2019-02-26
  • 2012-07-07
  • 2011-05-20
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
相关资源
最近更新 更多