Applescript get last opened date of file

Deadly 提交于 2019-12-08 04:05:38

问题


I can use
set modDate to the modification date of theFile as string to get the last modified date of a file, and
set modDate to the creation date of theFile as string to get the date when the file was created.

Is there anything like last opened date to get the date when the file was last opened?


回答1:


Yes. There is a UNIX command called kMDItemLastUsedDate that returns the date the target item was last used.

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

However, this command doesn't return a literal date object. Instead, it returns a date object in ISO 8601:2004 format (YYYY-MM-DD HH:MM:SS) which, if you try to put date before it, you'll get a syntax error.

Here is the revised script:

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



回答2:


i had the problem of wanting to get a date that was only available in the spotlight metadata (image file's content created date "kMDItemContentCreationDate" - original date from camera, i think). so i came up with this; note: i used "copy" and "tell" for my own clarity/ocd. there is an "as date" coercion for "do shell script" but it just gave me different errors. there is also simpler and better "awk"'s to do more/better things but the "- name " gives just the one mdls value you ask for.

(* gets the mdls value of "metaDate" ie one of the many available metadata dates "qpImg" is the "quoted form of posix path" of some file the awk strips it down to just the actual date/time string *)

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

(* takes mdls info dates in form "2012-01-19 14:37:38 -500" and makes it applescripty. "inText" is a posix path that is converted to it's "quoted form" on the fly. the "%x %r" is "standard numeric" date and 12hr time and can be coerced via "as date" *)

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'"

-- the two can be combined using 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



回答3:


There isn't a pure AppleScript solution to obtain a file's last access date. Using a combination of the shell tools stat and date you can construct an AppleScript helper function that provides the last opened date:

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

The function uses the following helper function to convert the last opened time stamp which is returned as a ISO 8601 formatted string to an AppleScript date:

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

The function LastOpenedDate can be invoked with an alias, file or POSIX file as an argument, e.g.:

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

returns

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

on my machine.



来源:https://stackoverflow.com/questions/7214410/applescript-get-last-opened-date-of-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!