Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
822
18
App Q&A testing by request.
I have a folder with over 2000 misc stuff in it...I want to go through it, but I'd like to first sort it into smaller folders to make it a bit easier and help the computer out.

I found an applescript that will sort the items into folders by full date, year/month/date.

That's too fine grain for my needs, so I'd like to organize them by Year/Month.

Naively I think I can alter the script I found to do it, but I need to learn the scripting code used in the the two do shell script lines.

So, my question(s) is will this be as easy I think it should be? Any advice on the code changes? Is there a better way to accomplish what I want?


Code:
tell application "Finder"
	set mifold to choose folder
	set filelist to every file of mifold as list
	set foldlist to every folder of mifold as list
	repeat with i from 1 to number of items in filelist
		set subj to item i of filelist as alias
		set epoch to (do shell script "stat -f \"%B\" " & (quoted form of (POSIX path of subj)))
		set caldate to do shell script ("date -r " & epoch & " \"+%Y-%m-%d\"")
		
		if not (exists folder caldate of mifold) then
			make new folder at mifold with properties {name:caldate}
		end if
		try
			(* try ignores errors from locked files *)
			move file subj to folder caldate of mifold
		end try
	end repeat
	repeat with j from 1 to number of items in foldlist
		set subj to item j of foldlist as alias
		if name of subj does not start with "200" then
			set epoch to (do shell script "stat -f \"%B\" " & (quoted form of (POSIX path of subj)))
			set caldate to do shell script ("date -r " & epoch & " \"+%Y-%m-%d\"")
			if not (exists folder caldate of mifold) then
				make new folder at mifold with properties {name:caldate}
			end if
			try
				(* try ignores errors from locked items *)
				move folder subj to folder caldate of mifold
			end try
			
		end if
	end repeat
end tell

From: http://hints.macworld.com/article.php?story=20090501221803809
 
Last edited:

kage207

macrumors 6502a
Jul 23, 2008
971
56
You need to start looking up the syntax. Basically what AppleScript is a very basic scripting language that is very English-like. It's like pseudo-code. What you can do is go through it and read a line and translate it into an English sentence.

I don't know much about AppleScript but here:
Code:
set caldate to do shell script ("date -r " & 1398314680 & " \"+%Y-%m-%d\"")
Is that the last argument is telling to get Year(%Y), month(%m) and date(%d).

I have no idea what the 1398314680 is. I don't even know how it is related to the programming. I'm pretty sure it doesn't have anything to do with the POSIX library as the line above uses that.

Here's a resource to check out: https://developer.apple.com/library...#//apple_ref/doc/uid/DTS10003093-CH1-SECTION4
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
I have no idea what the 1398314680 is. I don't even know how it is related to the programming. I'm pretty sure it doesn't have anything to do with the POSIX library as the line above uses that.

That is a Unix epoch, ie amount of seconds since January 1st 1970. That is the time stamp that is printed using the year-month-day format string.
 

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
822
18
App Q&A testing by request.
I have no idea what the 1398314680 is. I don't even know how it is related to the programming. I'm pretty sure it doesn't have anything to do with the POSIX library as the line above uses that.


Argh...it's something I forgot to change back before pasting!
I was trying to build the code of the two lines so I could try each by themselves in terminal to figure out the results, and left the altered code when I pasted here. <== dummy

as subsonix said, it's supposed to be the epoch timestamp

Here's the right code (also in the folder section of the original code)
Code:
set caldate to do shell script ("date -r " & epoch & " \"+%Y-%m-%d\"")

Thanks for the comments.

***********

EDIT: in fact, your comment was all I need to knock me on the head to not be stupid (or start a coding idea at 1am).

Is that the last argument is telling to get Year(%Y), month(%m) and date(%d). [/guote]

DUH... remove the "-d" from the line(s) and boom, it works.

Code:
set caldate to do shell script ("date -r " & epoch & " \"+%Y-%m\"")
SMH....THANKS!!:p:D

(Wonder if I an get it to display the month name instead of number.....)
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
(Wonder if I an get it to display the month name instead of number.....)

Start with the man page for the 'date' command:
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/date.1.html

Find where it talks about the "leading plus":
An operand with a leading plus (`+') sign signals a user-defined format string which specifies the format
in which to display the date and time. The format string may contain any of the conversion specifications
described in the strftime(3) manual page, as well as any arbitrary text.
Click the link to the strftime man page.

Find the word "month". Read about the conversions that mention "month name".

Finally, compose a test of the date command with what you think is a suitable conversion, and paste it into a Terminal window. If it produces what you want, you have an answer. If it doesn't, reread the man page(s). If you get stuck, ask here, describing exactly what you tried, what you expected, and what you actually got.
 

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
822
18
App Q&A testing by request.
Thanks...I decided to leave the date as is for now, given it's nice organizing nature...but I'll read up on it for future use.

For now, I've added a toggle to choose between, files, folders or both to be collected...and it's working...mostly...

All the files get sorted right...but if I try it on a folder of folders (the results of the files test), 5 of the 16 folders for some reason don't get moved into a folder.

The expected result is every one winds up in a single folder "2014-04"

From my initial test batch (top of the screen shot) I thought it was date related, but the second test batch (bottom) shows that's not it, but it is the same 5 folders in both.

Any one have thoughts on this?

Code:
tell application "Finder"
	
	display dialog "Files, Folders, or Both?" buttons {"Files", "Folders", "Both"}
	set choice to button returned of result
	set mifold to choose folder
	set filelist to every file of mifold as list
	set foldlist to every folder of mifold as list
	if (choice = "Files" or choice = "Both") then
		repeat with i from 1 to number of items in filelist
			set subj to item i of filelist as alias
			set epoch to (do shell script "stat -f \"%B\" " & (quoted form of (POSIX path of subj)))
			set caldate to do shell script ("date -r " & epoch & " \"+%Y-%m\"")
			
			if not (exists folder caldate of mifold) then
				make new folder at mifold with properties {name:caldate}
			end if
			try
				(* try ignores errors from locked files *)
				move file subj to folder caldate of mifold
			end try
		end repeat
	end if
	if (choice = "Folders" or choice = "Both") then
		repeat with j from 1 to number of items in foldlist
			set subj to item j of foldlist as alias
			if name of subj does not start with "200" then
				set epoch to (do shell script "stat -f \"%B\" " & (quoted form of (POSIX path of subj)))
				set caldate to do shell script ("date -r " & epoch & " \"+%Y-%m\"")
				if not (exists folder caldate of mifold) then
					make new folder at mifold with properties {name:caldate}
				end if
				try
					(* try ignores errors from locked items *)
					move folder subj to folder caldate of mifold
				end try
				
			end if
		end repeat
	end if
	beep 3
end tell
 

Attachments

  • Screenshot 2014-04-24 12.24.56.png
    Screenshot 2014-04-24 12.24.56.png
    182.8 KB · Views: 484

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
Thanks...I decided to leave the date as is for now, given it's nice organizing nature...but I'll read up on it for future use.

For now, I've added a toggle to choose between, files, folders or both to be collected...and it's working...mostly...

All the files get sorted right...but if I try it on a folder of folders (the results of the files test), 5 of the 16 folders for some reason don't get moved into a folder.

The expected result is every one winds up in a single folder "2014-04"

From my initial test batch (top of the screen shot) I thought it was date related, but the second test batch (bottom) shows that's not it, but it is the same 5 folders in both.

Any one have thoughts on this?

Code:
tell application "Finder"
	
	display dialog "Files, Folders, or Both?" buttons {"Files", "Folders", "Both"}
	set choice to button returned of result
	set mifold to choose folder
	set filelist to every file of mifold as list
	set foldlist to every folder of mifold as list
	if (choice = "Files" or choice = "Both") then
		repeat with i from 1 to number of items in filelist
			set subj to item i of filelist as alias
			set epoch to (do shell script "stat -f \"%B\" " & (quoted form of (POSIX path of subj)))
			set caldate to do shell script ("date -r " & epoch & " \"+%Y-%m\"")
			
			if not (exists folder caldate of mifold) then
				make new folder at mifold with properties {name:caldate}
			end if
			try
				(* try ignores errors from locked files *)
				move file subj to folder caldate of mifold
			end try
		end repeat
	end if
	if (choice = "Folders" or choice = "Both") then
		repeat with j from 1 to number of items in foldlist
			set subj to item j of foldlist as alias
[COLOR="Red"]			if name of subj does not start with "200" then
[/COLOR]				set epoch to (do shell script "stat -f \"%B\" " & (quoted form of (POSIX path of subj)))
				set caldate to do shell script ("date -r " & epoch & " \"+%Y-%m\"")
				if not (exists folder caldate of mifold) then
					make new folder at mifold with properties {name:caldate}
				end if
				try
					(* try ignores errors from locked items *)
					move folder subj to folder caldate of mifold
				end try
				
			end if
		end repeat
	end if
	beep 3
end tell

I hilited the likely cause in red.

It seems to be doing exactly what you told it.
 

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
822
18
App Q&A testing by request.
One day I'll realize to start troubleshooting at the basic level....

...that was in the code from macworld, which I know works, so I didn't bother to look at IT for the reason....I remember last night noticing it and then wondering "hmm wonder what that does...." but since I HAD planned on just commenting out the folder clause anyhow, I didn't bother reading it closely....

(I guess it was for the newly created folders to not then get sorted themselves...the code was originaly written in 2009....but that doesn't happen automatically, since foldlist is created before the new sorted folders are...extra programming to be safe, or maybe an applescript change in the past years..)

Thanks!

------------

On a matter of style... I have it asking for the file/folder option, and THEN asking for the folder to do it to...that order, or reverse it?

(I may rejigger the whole thing into a service anyhow...might be useful to have in the back pocket when needed).
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.