Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to sort files with similar filenames into discrete folders? (AppleScript)

I have a list of files that looks something like this:


  • OBJECTID_45878.dbf
  • OBJECTID_45878.prj
  • OBJECTID_45879.dbf
  • OBJECTID_45879.prj
  • OBJECTID_45880.dbf
  • OBJECTID_45880.prj


I want to run a script that will sort all of these files into subfolders based on their filenames. So the result would look like this:


OBJECTID_45878 (Folder)

  • OBJECTID_45878.dbf
  • OBJECTID_45878.prj

OBJECTID_45879 (Folder)

  • OBJECTID_45879.dbf
  • OBJECTID_45879.prj

OBJECTID_45880 (Folder)

  • OBJECTID_45880.dbf
  • OBJECTID_45880.prj


Here's the code I have now (see below). It creates a new folder for each file, and doesn't sort them like I would like. How should I modify my AppleScript so this works? Thanks so much!


tell application "Finder"
 set selected to selection
 set current_folder to item 1 of selected
 set mlist to every file of current_folder
 repeat with this_file in mlist
  set cur_ext to name extension of this_file
  set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)
  set new_folder to make new folder with properties {name:new_name} at current_folder
  move this_file to new_folder
 end repeat
end tell

MacBook Pro 15”, macOS 10.14

Posted on Jun 5, 2019 3:36 PM

Reply
Question marked as Best reply

Posted on Jun 5, 2019 6:35 PM

Here is my take on the AppleScript and the results. Assumption is that the current folder is what is selected in Finder before the script runs.


Before:


After:


Code:


set DELIM to {".dbf", ".prj", ".", ":"}

tell application "Finder"
	-- assumption: selection is a Finder selected containing folder of the items
	set selected to selection
	set current_folder to (first item of selected) as text as alias
	set mlist to (sort (every item of folder current_folder) by name) as alias list
	
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM}
	if mlist is {} then return
	repeat with this_file in mlist
		-- the DELIM guarantees that this item is the basename without extension
		set basename to text item -2 of (this_file as text)
		-- avoids duplicate folder name collisions
		if not (exists folder ((current_folder as text) & basename)) is true then
			set new_folder to make new folder at current_folder with properties {name:basename}
		end if
		move (this_file as text) to new_folder
	end repeat
	set AppleScript's text item delimiters to TID
end tell
return



Similar questions

4 replies
Question marked as Best reply

Jun 5, 2019 6:35 PM in response to daveamos21

Here is my take on the AppleScript and the results. Assumption is that the current folder is what is selected in Finder before the script runs.


Before:


After:


Code:


set DELIM to {".dbf", ".prj", ".", ":"}

tell application "Finder"
	-- assumption: selection is a Finder selected containing folder of the items
	set selected to selection
	set current_folder to (first item of selected) as text as alias
	set mlist to (sort (every item of folder current_folder) by name) as alias list
	
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM}
	if mlist is {} then return
	repeat with this_file in mlist
		-- the DELIM guarantees that this item is the basename without extension
		set basename to text item -2 of (this_file as text)
		-- avoids duplicate folder name collisions
		if not (exists folder ((current_folder as text) & basename)) is true then
			set new_folder to make new folder at current_folder with properties {name:basename}
		end if
		move (this_file as text) to new_folder
	end repeat
	set AppleScript's text item delimiters to TID
end tell
return



Jun 5, 2019 6:04 PM in response to daveamos21

add log statements to figure it out.


(* 

It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.


	Author: rccharles
	
	For testing, run in the Script Editor.
	  1) Click on the Event Log tab to see the output from the log statement
	  2) Click on Run
	  
	For running shell commands see:
	https://developer.apple.com/library/archive/technotes/tn2065/_index.html
	http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
	


 *)


on run
	-- Write a message into the event log.
	log "  --- Starting on " & ((current date) as string) & " --- "
	--  debug lines
	
	
	set desktopPath to path to home folder
	set unixDesktopPath to POSIX path of desktopPath
	log "unixDesktopPath = " & unixDesktopPath
	
	set quotedUnixDesktopPath to quoted form of unixDesktopPath
	log "quoted form is " & quotedUnixDesktopPath
	
	try
		set fromUnix to do shell script "ls -l  " & quotedUnixDesktopPath
		display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
	on error errMsg
		log "ls -l error..." & errMsg
	end try
	
end run









How to sort files with similar filenames into discrete folders? (AppleScript)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.