Monday, April 22, 2019

#1 2019-03-24 08:27:49 am

pavilion
Member
Registered: 2016-07-28
Posts: 16

Set Modification Timestamp To File Name

I have a folder of files, all named in the following Date/Time format:

2018/12/18 11.30.27

and I need to set each file's modification date and time to match the filename.

The folder's location is: "Hard Drive:Users:Bob's Mac:Documents:Travel Photos". I would appreciate anyone's help in writing an Applescript that would do this without any external code necessary.

Thank you very much!

Offline

 

#2 2019-03-24 11:34:20 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: Set Modification Timestamp To File Name

pavilion wrote:

I have a folder of files, all named in the following Date/Time format:

2018/12/18 11.30.27

and I need to set each file's modification date and time to match the filename.

The folder's location is: "Hard Drive:Users:Bob's Mac:Documents:Travel Photos". I would appreciate anyone's help in writing an Applescript that would do this without any external code necessary.

Thank you very much!



Probably, you need Read this topic. Is one of the solutions for your problem

Last edited by KniazidisR (2019-03-24 11:38:04 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#3 2019-03-24 12:25:45 pm

pavilion
Member
Registered: 2016-07-28
Posts: 16

Re: Set Modification Timestamp To File Name

Thanks for that, but is there some way to do this with purely Applescripting and not using an external command line file?

Offline

 

#4 2019-03-24 01:18:05 pm

ldicroce
Member
Registered: 2017-11-25
Posts: 92

Re: Set Modification Timestamp To File Name

pavilion wrote:

I would appreciate anyone's help in writing an Applescript that would do this without any external code necessary.

Thank you very much!


This should work. Although I am sure there is a more elegant and better way to do it.


Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set fileFolder to (choose folder) --
tell application "System Events"
   set hfsPaths to path of files of fileFolder
end tell

repeat with i from 1 to (number of items in hfsPaths)
   set thisFile to item i of hfsPaths
   quoted form of POSIX path of thisFile
   tell application "Finder"
       set thisFile_ext to name extension of (thisFile as alias)
       set thisFile_name to text 1 thru -((length of thisFile_ext) + 2) of (name of (thisFile as alias) as text)
       set full_name to name of (thisFile as alias)
   end tell
   set my_info to splitText(thisFile_name, " ")
   set myDate to splitText(item 1 of my_info, "/")
   set myTime to splitText(item 2 of my_info, ".")
   set my_command to (text 3 thru -1 of (item 1 of myDate) & (item 2 of myDate) & (item 3 of myDate) & (item 1 of myTime) & (item 2 of myTime) & "." & (item 3 of myTime))
   
   do shell script "touch -t " & my_command & " " & quoted form of POSIX path of thisFile
end repeat



on splitText(theText, theDelimiter)
   set AppleScript's text item delimiters to theDelimiter
   set theTextItems to every text item of theText
   set AppleScript's text item delimiters to ""
   return theTextItems
end splitText

Last edited by ldicroce (2019-03-24 01:38:35 pm)

Offline

 

#5 2019-03-24 03:38:35 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3397

Re: Set Modification Timestamp To File Name

I would try with this simple :

Applescript:

set fileFolder to (choose folder)
tell application "System Events"
   set hfsPaths to path of files of fileFolder
   repeat with aPath in hfsPaths
       set theName to name of disk item aPath --> your name "2018/12/18 11.30.27.png" is returned as "2018:12:18 11.30.27.png"
       set dateComponents to my splittext(theName, {":", " ", "."})
       --> {"2018", "12", "18", "", "11", "30", "27", "png"}
       set newDate to date ("1 / 1 / 1")
       tell newDate
           set year to dateComponents's item 1
           set day to dateComponents's item 3
           set its month to dateComponents's item 2
           set its hours to dateComponents's item 5 # ADDED its
           set its minutes to dateComponents's item 6 # ADDED its
           set its seconds to dateComponents's item 7
       end tell
       set modification date of file aPath to newDate
   end repeat
end tell

on splittext(theText, theDelimiter)
   set AppleScript's text item delimiters to theDelimiter
   set theTextItems to every text item of theText
   set AppleScript's text item delimiters to ""
   return theTextItems
end splittext

Alternate syntax :

Applescript:

set fileFolder to (choose folder)
tell application "System Events"
   set hfsPaths to path of files of fileFolder
   repeat with aPath in hfsPaths
       set theName to name of disk item aPath --> your name "2018/12/18 11.30.27.png" is returned as "2018:12:18 11.30.27.png"
       set {theYear, theMonth, theDay, bof, theHours, theMinutes, theSeconds} to my splittext(theName, {":", " ", "."})
       --> {"2018", "12", "18", "", "11", "30", "27"}
       set newDate to date ("1 / 1 / 1")
       tell newDate
           set {year, day, its month, its hours, its minutes, its seconds} to {theYear, theDay, theMonth, theHours, theMinutes, theSeconds}
       end tell
       set modification date of file aPath to newDate
   end repeat
end tell

on splittext(theText, theDelimiter)
   set AppleScript's text item delimiters to theDelimiter
   set theTextItems to every text item of theText
   set AppleScript's text item delimiters to ""
   return theTextItems
end splittext

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mars 2019 22:38:21

Last edited by Yvan Koenig (2019-03-25 04:03:39 am)

Offline

 

#6 2019-03-24 03:46:18 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: Set Modification Timestamp To File Name

Following script works for me. I use the 24 hour setting for time, the US system. OS X Captain.

Applescript:



set myFolder to choose folder with prompt "Choose the folder with the photos!"

tell application "Finder"
   
   set photosFiles to (every file of myFolder whose name extension is "png") as alias list
   repeat with nextFile in photosFiles
       
       set nextName to name of nextFile
       set theYear to text 1 thru 4 of nextName
       set theMonth to text 6 thru 7 of nextName
       set theDay to text 9 thru 10 of nextName
       set theHour to text 12 thru 13 of nextName
       set theMinutes to text 15 thru 16 of nextName
       set theSeconds to text 18 thru 19 of nextName
       
       set dateString to theDay & "/" & theMonth & "/" & theYear & space & theHour & ":" & theMinutes & ":" & theSeconds
       set the_date to my date dateString
       set modification date of file nextFile to the_date
       
   end repeat
end tell

The most important in the script is the use of the keyword my in the sentence set the_date to my date dateString. This tells the script that the script itself should process the date command, not the Finder.

Due to the omission of this keyword, many scripters believe that it is impossible to change the modification date only using AppleScript. As you will see, it is not.

Last edited by KniazidisR (2019-03-24 03:56:17 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#7 2019-03-24 04:02:29 pm

pavilion
Member
Registered: 2016-07-28
Posts: 16

Re: Set Modification Timestamp To File Name

KniazidisR:

I just tried your script but it set the date to: Jun 12, 2019 12:00 AM (in the future!)

What am I missing?

Offline

 

#8 2019-03-24 04:16:38 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: Set Modification Timestamp To File Name

pavilion wrote:

KniazidisR:

I just tried your script but it set the date to: Jun 12, 2019 12:00 AM (in the future!)

What am I missing?



My script is designed for 24-hour setting of the US system. For example, Finder shows me the dates of file modification in the following format:

21 Mar 2018 20:42

Check if you have it. If not, then you need to either change the setting for dates through the System Settings panel, or slightly alter the script for your computer

Show me one of the modification dates of some file on your computer (as shown by the Finder)

For example Jun 12, 2019 6:50 PM would mean that you have a 12-hour setting. Also note the order of the day / month / year. If you have a different order in the Finder, I will tell you what to fix in the script.

Also, I assume that the names of your files use the 24-hour format, from the name format you specified. It means that everything works, you need to set up a 24-hour format for Finder at least temporarily. This temporary replacement could also be automated, but I would not like to dig deep into the subject.

Last edited by KniazidisR (2019-03-24 04:43:07 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#9 2019-03-24 05:10:04 pm

pavilion
Member
Registered: 2016-07-28
Posts: 16

Re: Set Modification Timestamp To File Name

If it would be possible to slightly modify the script for a 12-hour setting, I would be most grateful.

The date/time format is exactly as I had copied earlier and does use 24-hour format.

Thanks again for your help!

Offline

 

#10 2019-03-24 05:55:14 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: Set Modification Timestamp To File Name

pavilion wrote:

If it would be possible to slightly modify the script for a 12-hour setting, I would be most grateful.

The date/time format is exactly as I had copied earlier and does use 24-hour format.

Thanks again for your help!



What you copied is from the Script Editor's Results area. I asked you to show something completely different: in what format the Finder shows the modification dates in its windows. In a concatenation clause set dateString to theDay & "/" & theMonth & "/" & theYear & space & theHour &... the order of the components of a date must be the same in which Finder shows it. That is, we put correspondence in what our Apple-script does and what the Finder does.

For 12-hour setting, the order in Finder remain day/mounth/year:

Applescript:


set myFolder to choose folder with prompt "Choose the folder with the photos!"

tell application "Finder"
   
   set photosFiles to (every file of myFolder whose name extension is "png") as alias list
   repeat with nextFile in photosFiles
       
       set nextName to name of nextFile
       set theYear to text 1 thru 4 of nextName
       set theMonth to text 6 thru 7 of nextName
       set theDay to text 9 thru 10 of nextName
       set theHour to text 12 thru 13 of nextName
       set theMinutes to text 15 thru 16 of nextName
       set theSeconds to text 18 thru 19 of nextName
       
       set myAmPm to "AM"
       if (theHour as integer) ≥ 12 then
           set theHour to ((theHour as integer) - 12) as string
           set myAmPm to "PM"
       end if
       
set dateString to theDay & "/" & theMonth & "/" & theYear & space & theHour & ":" & theMinutes & ":" & theSeconds & space & myAmPm
       set the_date to my date dateString
       set modification date of file nextFile to the_date
       
   end repeat
end tell

Last edited by KniazidisR (2019-03-24 06:20:29 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#11 2019-03-24 06:23:32 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 60

Re: Set Modification Timestamp To File Name

Applescript:

set myAmPm to "AM"
if (theHour as integer) ≥ 12 then
set theHour to ((theHour as integer) - 12) as string
set myAmPm to "PM"
end if

Does this yield the correctly-formatted time between 12 midnight and 1 AM?

Offline

 

#12 2019-03-24 06:26:22 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: Set Modification Timestamp To File Name

peavine wrote:


Does this yield the correctly-formatted time between 12 midnight and 1 AM?



Why not?

Last edited by KniazidisR (2019-03-24 06:27:57 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#13 2019-03-24 07:16:10 pm

pavilion
Member
Registered: 2016-07-28
Posts: 16

Re: Set Modification Timestamp To File Name

This new 12-hour script also does not work either (nor does setting the system time format to 24 hours using the prior script).

The Finder timestamp for the filename

2018/12/18 11.30.27

says:

June 12, 2019 11:30 AM

so there is something more majorly off with the script.

Offline

 

#14 2019-03-24 07:17:21 pm

pavilion
Member
Registered: 2016-07-28
Posts: 16

Re: Set Modification Timestamp To File Name

In other words, it's not just an AM/PM issue because the date is way off, in fact it's in the future!

Offline

 

#15 2019-03-24 07:26:00 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: Set Modification Timestamp To File Name

pavilion wrote:

This new 12-hour script also does not work either (nor does setting the system time format to 24 hours using the prior script).

The Finder timestamp for the filename

says:

June 12, 2019 11:30 AM



No issue, no.  If this is indeed the Finder displays your dates in this form, then you should simply replace the code line

set dateString to theDay & "/" & theMonth & "/" & theYear & space & theHour & ":" & theMinutes & ":" & theSeconds & space & myAmPm

in the script with such a line:

set dateString to theMonth & "/" & theDay & "/" & theYear & space & theHour & ":" & theMinutes & ":" & theSeconds & space & myAmPm

It's just that your Finder shows the date in the order month/day/year, and I have a day/month/year. What I have been talking about for 2 hours

Last edited by KniazidisR (2019-03-24 07:42:02 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#16 2019-03-24 07:36:34 pm

pavilion
Member
Registered: 2016-07-28
Posts: 16

Re: Set Modification Timestamp To File Name

Yes, perfect! Thanks for your help and patience!

Offline

 

#17 2019-03-25 03:10:38 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3397

Re: Set Modification Timestamp To File Name

The script which I posted is not related to the date-time format in use on your machine.
So it may be used worldwide .

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mars 2019 10:10:23

Offline

 

#18 2019-03-25 07:27:13 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 60

Re: Set Modification Timestamp To File Name

KniazidisR wrote:
peavine wrote:


Does this yield the correctly-formatted time between 12 midnight and 1 AM?



Why not?



Wouldn't your script, for example, change 00:01 (24-hour clock) to 00:01 AM (12-hour clock) rather than 12:01 AM and 12:01 (24-hour clock) to 00:01 PM (12-hour clock) rather than 12:01 PM.

BTW, my use of "correctly-formatted" was a poor choice of words.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)