Date Applescript:获取所选文件的修改日期并输出为文本

Date Applescript:获取所选文件的修改日期并输出为文本,date,applescript,osx-elcapitan,automator,Date,Applescript,Osx Elcapitan,Automator,我最近一直在使用Apple Automator应用程序来帮助我保持文件结构和文件名的有序。 我发现了一个漂亮的脚本,它以YYYY-MM-DD格式输出今天的日期,并替换选定的文本。这很酷,因为我的文件和文件夹的基本数据结构是YYYY MM DD项目名称。 现在,我希望能够将提到的日期重命名为上次修改的日期,以便跟踪我何时编辑了内容。(基本上允许我只选择上一个日期,点击键盘快捷键调用服务脚本并覆盖它。) 我尝试过一些方法,但似乎无法完成。 非常感谢您的帮助!事先非常感谢你。(我希望这不是重复的。)

我最近一直在使用Apple Automator应用程序来帮助我保持文件结构和文件名的有序。 我发现了一个漂亮的脚本,它以YYYY-MM-DD格式输出今天的日期,并替换选定的文本。这很酷,因为我的文件和文件夹的基本数据结构是YYYY MM DD项目名称。
现在,我希望能够将提到的日期重命名为上次修改的日期,以便跟踪我何时编辑了内容。(基本上允许我只选择上一个日期,点击键盘快捷键调用服务脚本并覆盖它。)
我尝试过一些方法,但似乎无法完成。 非常感谢您的帮助!事先非常感谢你。(我希望这不是重复的。)
干杯 如果你需要我说的剧本:

on todayISOformat()
    set theDate to current date
    set y to text -4 thru -1 of ("0000" & (year of theDate))
    set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
    set d to text -2 thru -1 of ("00" & (day of theDate))
    return y & "-" & m & "-" & d
end todayISOformat

on run {input, parameters}
   return todayISOformat()
end run

下面的示例选择一个文件并获取该文件的上次修改日期

当然,您可以再次使用例程来设置日期格式:

set MyFile to choose file
tell application "Finder" to set DateUpdate to modification date of MyFile

set StringToday to todayISOformat(current date)-- get the string value of current date
set StringModif to todayISOformat(DateUpdate)-- get the string value of modification's date

on todayISOformat(LDate)
set y to text -4 thru -1 of ("0000" & (year of LDate))
set m to text -2 thru -1 of ("00" & ((month of LDate) as integer))
set d to text -2 thru -1 of ("00" & (day of LDate))
return y & "-" & m & "-" & d
end todayISOformat

下面的示例选择一个文件并获取该文件的上次修改日期

当然,您可以再次使用例程来设置日期格式:

set MyFile to choose file
tell application "Finder" to set DateUpdate to modification date of MyFile

set StringToday to todayISOformat(current date)-- get the string value of current date
set StringModif to todayISOformat(DateUpdate)-- get the string value of modification's date

on todayISOformat(LDate)
set y to text -4 thru -1 of ("0000" & (year of LDate))
set m to text -2 thru -1 of ("00" & ((month of LDate) as integer))
set d to text -2 thru -1 of ("00" & (day of LDate))
return y & "-" & m & "-" & d
end todayISOformat

此脚本根据以下2条规则重命名任何选定的文件:

  • 如果文件名以
    YYYY-MM-DD-
    开头,则会将此前缀替换为文件当前修改日期的日期
  • 如果文件不是以
    YYYY-MM-DD-
    开头,则将文件的当前修改日期放在文件名前面
  • 不考虑文件夹

    tell application "Finder"
        repeat with anItem in (get selection)
            if class of anItem is not folder then
                set {currentFileName, modificationDate} to {name, modification date} of anItem
                set formattedModificationDate to my formatDate(modificationDate)
                if text 11 of currentFileName is "-" then
                    set newFileName to formattedModificationDate & text 11 thru -1 of currentFileName
                else
                    set newFileName to formattedModificationDate & "-" & currentFileName
                end if
                set name of contents of anItem to newFileName
            end if
        end repeat
    end tell
    
    on formatDate(aDate)
        tell aDate to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
            to return text -4 thru -1 & "-" & text 4 thru 5 & "-" & text 2 thru 3
    end formatDate
    
    更新:

    脚本只检查第11个字符是否为连字符。如果需要检查整个
    YYYY-MM-DD-
    模式,则可以使用此处理程序

    on validateYYYYMMDD(aDateString)  -- returns true on success
        try
            tell aDateString
                (text 1 thru 4) as integer
                (text 6 thru 7) as integer
                (text 9 thru 10) as integer
                return text 5 is "-" and text 8 is "-" and text 11 is "-"
            end tell
        on error
            return false
        end try
    end validateYYYYMMDD 
    
    或者,如果您安装了SatImage.osax,它不需要shell调用即可提供正则表达式搜索

    on validateYYYYMMDD(aDateString) -- returns true on success
        try
            find text "^\\d{4}-(\\d{2}-){2}" in aDateString with regexp
            return true
        on error
            return false
        end try
    end validateYYYYMMDD
    
    若要使用处理程序,请替换该行

    if text 11 of currentFileName is "-" then
    


    此脚本根据以下2条规则重命名任何选定的文件:

  • 如果文件名以
    YYYY-MM-DD-
    开头,则会将此前缀替换为文件当前修改日期的日期
  • 如果文件不是以
    YYYY-MM-DD-
    开头,则将文件的当前修改日期放在文件名前面
  • 不考虑文件夹

    tell application "Finder"
        repeat with anItem in (get selection)
            if class of anItem is not folder then
                set {currentFileName, modificationDate} to {name, modification date} of anItem
                set formattedModificationDate to my formatDate(modificationDate)
                if text 11 of currentFileName is "-" then
                    set newFileName to formattedModificationDate & text 11 thru -1 of currentFileName
                else
                    set newFileName to formattedModificationDate & "-" & currentFileName
                end if
                set name of contents of anItem to newFileName
            end if
        end repeat
    end tell
    
    on formatDate(aDate)
        tell aDate to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
            to return text -4 thru -1 & "-" & text 4 thru 5 & "-" & text 2 thru 3
    end formatDate
    
    更新:

    脚本只检查第11个字符是否为连字符。如果需要检查整个
    YYYY-MM-DD-
    模式,则可以使用此处理程序

    on validateYYYYMMDD(aDateString)  -- returns true on success
        try
            tell aDateString
                (text 1 thru 4) as integer
                (text 6 thru 7) as integer
                (text 9 thru 10) as integer
                return text 5 is "-" and text 8 is "-" and text 11 is "-"
            end tell
        on error
            return false
        end try
    end validateYYYYMMDD 
    
    或者,如果您安装了SatImage.osax,它不需要shell调用即可提供正则表达式搜索

    on validateYYYYMMDD(aDateString) -- returns true on success
        try
            find text "^\\d{4}-(\\d{2}-){2}" in aDateString with regexp
            return true
        on error
            return false
        end try
    end validateYYYYMMDD
    
    若要使用处理程序,请替换该行

    if text 11 of currentFileName is "-" then
    


    您可以使用
    «class isot»
    将日期转换为iso日期时间字符串。您只需要从中获取字符1到10,就可以只获取iso日期字符串

    tell application "Finder"
        repeat with anItem in (get selection)
            if class of anItem is not folder then
                set {fileName, modificationDate} to {name, modification date} of anItem
                set isoDate to text 1 thru 10 of (modificationDate as «class isot» as string)
                if text 11 of fileName is "-" then set fileName to text 11 thru -1 of fileName
                set newFileName to isoDate & "-" & fileName
                set name of contents of anItem to newFileName
            end if
        end repeat
    end tell
    

    您可以使用
    «class isot»
    将日期转换为iso日期时间字符串。您只需要从中获取字符1到10,就可以只获取iso日期字符串

    tell application "Finder"
        repeat with anItem in (get selection)
            if class of anItem is not folder then
                set {fileName, modificationDate} to {name, modification date} of anItem
                set isoDate to text 1 thru 10 of (modificationDate as «class isot» as string)
                if text 11 of fileName is "-" then set fileName to text 11 thru -1 of fileName
                set newFileName to isoDate & "-" & fileName
                set name of contents of anItem to newFileName
            end if
        end repeat
    end tell
    

    嘿,非常感谢你的回答!我测试了它,它返回了正确的日期。太棒了。有没有办法绕过“选择文件”对话框窗口?我想先选择文件,进入重命名模式,然后启动服务,让它输入新的日期。嘿,非常感谢你的回答!我测试了它,它返回了正确的日期。太棒了。有没有办法绕过“选择文件”对话框窗口?我想先选择文件,进入重命名模式,然后启动服务,让它输入新的日期。哦,天哪,这真是太棒了。非常感谢你!这个剧本太完美了!像冠军一样工作!对更新的反应:感谢您在事后仍在努力!老实说,我从来没有接触过AppleScript在我的生活中,即使它基本上只是简单的英语,我不能设法包括新的脚本。根据我的Javascript知识,我会问validateYYYYMMDD(newFileName)是否等于true,但似乎无法让它工作。说实话,我有点不好意思,哈哈。那很容易。再次感谢你!哦,天哪,这简直太棒了。非常感谢你!这个剧本太完美了!像冠军一样工作!对更新的反应:感谢您在事后仍在努力!老实说,我从来没有接触过AppleScript在我的生活中,即使它基本上只是简单的英语,我不能设法包括新的脚本。根据我的Javascript知识,我会问validateYYYYMMDD(newFileName)是否等于true,但似乎无法让它工作。说实话,我有点不好意思,哈哈。那很容易。再次感谢你!