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

Macos Applescript获取文件的最后打开日期,macos,file,applescript,Macos,File,Applescript,我可以使用 将modDate设置为文件的修改日期作为字符串以获取文件的最后修改日期,并且 将modDate设置为文件的创建日期为string以获取创建文件的日期 是否有类似于上次打开日期的内容来获取文件上次打开的日期?是。有一个名为kMDItemLastUsedDate的UNIX命令,返回上次使用目标项的日期 set the Last_opened_date to (do shell script "mdls -name kMDItemLastUsedDate " & quoted fo

我可以使用
将modDate设置为文件的修改日期作为字符串
以获取文件的最后修改日期,并且
将modDate设置为文件的创建日期为string
以获取创建文件的日期


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

是。有一个名为
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

没有一个纯AppleScript解决方案可以获取文件的最后访问日期。使用shell工具的组合,您可以构造一个提供上次打开日期的AppleScript helper函数:

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日期:

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
可以使用
别名
文件
POSIX文件
作为参数调用函数
LastOpenedDate
,例如:

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

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

在我的机器上。

我有一个问题,就是想要得到一个只在spotlight元数据中可用的日期(图像文件的内容创建日期“kMDItemContentCreationDate”-我想是相机的原始日期)。所以我想到了这个,;注:我使用“复制”和“告知”是为了我自己的清晰性/ocd。“DoShell脚本”有一个“as-date”强制,但它只是给了我不同的错误。还有更简单更好的“awk”来做更多/更好的事情,但是“-name”只给出了您要求的mdls值

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

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

--可以使用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

这是修改当前日期!如果OP想要引用程序中其他地方的当前日期,该怎么办?您不能修改。对。。。忘了吧。对不起我花了很长时间才想到这个。。。我在做其他事情,从来没有时间完成我开始的清理脚本。修改后的脚本不起作用。我认为有一些错误,比如空格遗漏等。
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'"
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