2013-09-27 174 views
2

我试图在AppleScript中获取文件的最后修改日期。我以为我已经使用这个工作:在AppleScript中获取文件的最后修改日期

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

,这似乎返回一个有效的值,但是当我把这个一个on idle片的代码里面我得到一个:

“不能得到的......”错误

类<>我看到别的地方建议使用:

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

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

+1

它必须AppleScript的?你可以很容易地使用stat filename – michael501

+1

FYI来做到这一点:对于你的shell脚本命令,有一个可以工作的“kMDItemContentModificationDate”。 – regulus6633

回答

4

您需要引用该文件。

尝试

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 
+1

我建议的唯一调整是告诉应用程序系统事件而不是Finder。一般来说,尽可能避免使用Finder是最好的选择。因此,既然Finder和System Events都具有“修改日期”属性,我就会使用后者。 – regulus6633

+1

此外,我很惊讶你的第二个例子工作,因为“修改日期”不是一个applescript知道的属性。该属性被Finder和系统事件所了解,因此您应该告诉其中一个应用程序执行该命令。我确实看到,当你使用那些在文件引用结尾处的应用程序创建文件引用时,它包括“应用程序Finder”...我猜这就是为什么“修改日期”调用在应用程序之外工作块。你有不同的解释吗? – regulus6633

+1

嗨reg。系统事件更快,并接受posix路径,所以我会更新答案。 (我有一个脚本调试器扩展,它填充了Finder并且我很懒)。我发现一旦你在Finder或SE中引用了一个文件,你可以在之后将它从一个tell块中排除。 – adayzdone

相关问题