【发布时间】:2019-08-23 10:11:03
【问题描述】:
希望获得有关 AppleScript 的帮助然后将其更新为当前日期。
例如:
图片 1.jpeg
到
图片 1 2019-04-02.jpeg
我已经从Applescript New Folder Named By Date 中找到了一些有用的代码 和https://gist.github.com/mnot/221399
我已经修改了它们以创建一个大部分工作的applescript。 我无法弄清楚的唯一部分是如何检查文件名中是否已经存在日期,然后如何替换它。目前,我的 applescript 每次运行时都会继续添加日期。
不确定这是否会有所不同,但可以安全地假设没有文件会以 XXXX-XX-XX(X = 任何数字)结尾。
tell application "Finder"
try
set the source_folder to (folder of the front window) as text
on error -- no open folder windows
display dialog "Please select a file to apply date to" buttons {"Cancel"} default button 1
end try
set these_items to the selection
end tell
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items) as alias
set this_info to info for this_item
set {file_name, file_ext} to splitExtension from the name of this_info
set x to my the_perfect_datestring()
set new_name to file_name & " " & x & "" & file_ext
-- check to see if it's already there
tell application "Finder"
try
set name of this_item to new_name
on error -- no open folder windows
display dialog "Error - File may already exist" buttons {"Cancel"} default button 1
end try
end tell
end repeat
on the_perfect_datestring()
try
set cd to (the current date)
set the_year to year of (cd) as number
set the_month to month of (cd) as number
set the_day to day of (cd) as number
if the_month < 10 then set the_month to "0" & the_month
if the_day < 10 then set the_day to "0" & the_day
return ((the_year & "-" & the_month & "-" & the_day) as text)
on error
return "-ERROR"
end try
end the_perfect_datestring
to splitExtension from file_name
set dot to "."
tell AppleScript
set oT to text item delimiters
set text item delimiters to dot
if (count text items of file_name) > 1 then
set out_name to (text items 1 through -2 of file_name) as string
set ext to last text item of file_name
else
set out_name to file_name
set ext to ""
end if
set text item delimiters to oT
if ext is not "" then set ext to dot & ext
return {out_name, ext}
end tell
end splitExtension
提前致谢
【问题讨论】:
标签: file date applescript directory rename