Applescript获取文件的最后打开日期

时间:2011-08-27 12:03:17

标签: macos file applescript

我可以使用
set modDate to the modification date of theFile as string获取文件的最后修改日期,以及
set modDate to the creation date of theFile as string获取文件创建日期。

是否有类似last opened date的内容来获取上次打开文件的日期?

3 个答案:

答案 0 :(得分:2)

是。有一个名为kMDItemLastUsedDate的UNIX命令,它返回上次使用目标项的日期。

set the Last_opened_date to (do shell script "mdls -name kMDItemLastUsedDate " & quoted form of the POSIX path of theFile)

但是,此命令不返回文字日期对象。相反,它返回ISO 8601:2004格式的日期对象(YYYY-MM-DD HH:MM:SS),如果您尝试将date放在它之前,则会出现语法错误。

以下是修订后的脚本:

property months : {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}

set the Last_opened_date to date convert_date(do shell script "mdls -name kMDItemLastUsedDate " & quoted form of the POSIX path of theFile)

on convert_date(passed_data)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to space
    set the ISO_date to the first text item of (passed_data as string)
    set AppleScript's text item delimiters to "-"
    set the date_parts to every text item of the ISO_date
    set the_year to the first text item of the date_parts
    set the_month to the second text item of the date_parts
    set the_day to the third text item of the date_parts
    set AppleScript's text item delimiters to space
    set the time_string to the second text item of (passed_data as string)
    set AppleScript's text item delimiters to prevTIDs
    return item (the_month as integer) of months & the_day & ", " & the_year & space & the time_string
end convert_date

答案 1 :(得分:1)

我有一个问题,想要获得一个仅在聚光灯元数据中可用的日期(图像文件的内容创建日期“kMDItemContentCreationDate” - 来自相机的原始日期,我认为)。所以我想出了这个;注意:我使用“copy”和“tell”表示我自己的清晰度/ ocd。对于“do shell script”有一个“as date”强制,但它只是给了我不同的错误。还有更简单和更好的“awk”来做更多/更好的事情,但“ - name”只给出你要求的一个mdls值。

(*获取“metaDate”的mdls值,即许多可用元数据日期之一    “qpImg”是某些文件的“引用形式的posix路径”    awk将其剥离为实际的日期/时间字符串*)

tell current application to copy (do shell script ("mdls " & " -name " & metaDate & " " & qpImg & " | awk -F ' ' '/Date/ {print $3,$4};'")) to targDate

(*在表格“2012-01-19 14:37:38 -500”中获取mdls信息日期并使其成为Applecripty。    “inText”是一个posix路径,可以即时转换为它的“引用形式”。    “%x%r”是“标准数字”日期和12小时时间,可以通过“作为日期”强制执行*)

tell current application to set formtdDate to do shell script "date -j -f '%Y-%m-%d %H:%M:%S' " & quoted form of inText & " +'%x %r'"

- 可以使用xargs

组合两者
tell current application to copy (do shell script ("mdls -name " & metaDate & " " & qpImg & " | awk -F ' ' '{print $3,$4};' | xargs -0 -I indate date -j -f '%Y-%m-%d %H:%M:%S' indate +'%x %r'")) to targDate

答案 2 :(得分:0)

没有纯AppleScript解决方案来获取文件的上次访问日期。使用shell工具statdate的组合,您可以构建AppleScript帮助程序函数,该函数提供上次打开的日期:

on LastOpenedDate(theFile)
    set theStr to do shell script "date -r $(stat -f %a " & quoted form of (POSIX path of theFile) & ") +%Y-%m-%dT%H:%M:%S"
    ISODateStrToDate(theStr)
end LastOpenedDate

该函数使用以下辅助函数将最后打开的时间戳转换为AppleScript日期,该时间戳作为ISO 8601格式化字符串返回:

on ISODateStrToDate(theStr)
    set dt to (current date)
    set savedDelimeters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"-", "T", ":"}
    set {dt's year, dt's month, dt's day, dt's hours, dt's minutes, dt's seconds} to (every text item of theStr)
    set AppleScript's text item delimiters to savedDelimeters
    return dt
end ISODateStrToDate

可以使用LastOpenedDatealiasfile作为参数调用函数POSIX file,例如:

LastOpenedDate(POSIX file "/var/log/system.log")

返回

date "Saturday, August 27, 2011 4:04:52 PM"

在我的机器上。