Create Folder Structure then put images into folder based on extension

Hello all,

My goal is to be able to drag folders from my camera’s memory card into a parent folder. Then run a script and have the folders renamed appropriately, move the .CR2 and .JPG files from the camera into a new subfolders based on extension, and create another new empty folder.

Folders from camera example:


  • Parent Folder
    - 100EOS5D --Contains .CR2 raw image files. May contain .JPG files depending on camera settings. (Numbers change based on when the photos were taken)
    - 101EOS5D
    - 102EOS5D

Folder structure that I want:


  • Position_01 (Renamed from first folder)
    - Position_01_Raw --contains the .CR2 files
    - Position_01_Jpg --contains the .JPG files (if there are any)
    - Position_01_Stitched --Empty folder
  • Position_02 (Renamed from second folder)
    - Position_02_Raw --contains the .CR2 files
    - Position_02_Jpg --contains the .JPG files (if there are any)
    - Position_02_Stitched --Empty folder

Ideally this would be a script that I could double click on when I’m ready to sort them.

Thank you so much!

Model: Macbook Pro Retina
AppleScript: Version 2.9 (191)
Browser: Chrome Version 55.0.2883.95 (64-bit)
Operating System: Mac OS X (10.10)

You may try:

[format]replaced by the version posted in message #4 according to enhanced informations[/format]

CAUTION : I defined myRaw as “.NEF” because it’s what is used by my Nikon camera.

Save the script as an application.
If you double click its icon you will be urged to navigate to the source folder
If you drag & drop the folder’s icon onto the script one, it will be treated with no other action.

If I have free time I will build an alternate version using ASObjC which would be faster.

The job may be done using the Finder but I hate it.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) dimanche 18 décembre 2016 19:19:11

Thanks so much for working on this! :smiley:

The script seems to be deleting some things it shouldn’t and it’s renaming the images instead of moving them.

Here’s a window view of what the folder looked like before and after:

BEFORE


AFTER

Can we add a clause that tells it to skip folders that don’t start with EOS5D so that it doesn’t affect any other folders other than image folders?

It’s funny to see comments which don’t match what was described in the original question.
Original contents:
[format]- Parent Folder
- 100EOS5D --Contains .CR2 raw image files. May contain .JPG files depending on camera settings. (Numbers change based on when the photos were taken)
- 101EOS5D
- 102EOS5D[/format]

New comment:
[format]Can we add a clause that tells it to skip folders that don’t start with EOS5D so that it doesn’t affect any other folders other than image folders?[/format]
And of course when I look at the screenshot there is no folders whose name resemble to 100EOS5D, 101EOS5D

But this is not sufficient.

I carefully wrote:
[format]CAUTION : I defined myRaw as “.NEF” because it’s what is used by my Nikon camera.[/format]
It’s clear that you didn’t took care of that so that the script doesn’t recognize your abcd.CR2 files.

From my point of view, the only true problem is that I failed to insert a slash between a foldername and a filename in two instructions.

Honestly, I was unable to guess that you were storing so many different things in the folder to treat.
What is written in your message:
[format]My goal is to be able to drag folders from my camera’s memory card into a parent folder. Then run a script and have the folders renamed appropriately, move the .CR2 and .JPG files from the camera into a new subfolders based on extension, and create another new empty folder.[/format]
was far from letting think that there is such a mess in the folder to treat.

I took care to filter folder to don’t treat the folders treated by a first use.
If I was tempted to filter the source folder, according to your description, I would have checked that the folder names were beginning with a group of three digits, not by “EOS”.

Here is an edited version:

on run
	set mainFolder to choose folder
	my germaine(mainFolder)
end run

on open sel
	set mainFolder to item 1 of sel
	my germaine(mainFolder)
end open

on germaine(mainFolder)
	set myRaw to ".NEF" # IT'S YOUR DUTY TO PUT YOUR NAME EXTENSION HERE !
	
	tell application "System Events"
		set theFolders to folders of mainFolder
		set knt to 0
		repeat with aFolder in theFolders
			tell me to set POSIXsource to POSIX path of aFolder
			if POSIXsource does not end with "/" then set POSIXsource to POSIXsource & "/"
			set origName to name of aFolder
			if origName contains "EOS5D" then
				set knt to knt + 1
				set kntPadded to text -2 thru -1 of ((1000 + knt) as text)
				set newName to "Position_" & kntPadded
				try
					make new folder at end of mainFolder with properties {name:newName}
				end try
				set newRaw to newName & "_Raw"
				try
					make new folder at end of folder newName of mainFolder with properties {name:newRaw}
				end try
				set POSIXRaw to my getPOSIXPath((mainFolder as text) & newName & ":" & newRaw)
				set theNames to (name of every file of aFolder whose visible is true)
				set beurk to theNames as text
				if (beurk contains ".jpg") or beurk contains ".jpeg" then
					set newJpg to newName & "_Jpg"
					try
						make new folder at end of folder newName of mainFolder with properties {name:newJpg}
					end try
					set POSIXJpg to my getPOSIXPath((mainFolder as text) & newName & ":" & newJpg)
				end if
				repeat with aName in theNames
					if aName ends with myRaw then
						tell me to do shell script "mv " & quoted form of (POSIXsource & aName) & space & POSIXRaw # EDITED
					else if (aName ends with ".jpg") or (aName ends with ".jpeg") then
						tell me to do shell script "mv " & quoted form of (POSIXsource & aName) & space & POSIXJpg # EDITED
					end if
				end repeat
				delete aFolder
			end if # origname.
		end repeat
	end tell
end germaine

on getPOSIXPath(hfsPath)
	return quoted form of POSIX path of hfsPath
end getPOSIXPath

Took care of this instruction:

set myRaw to ".NEF" # IT'S YOUR DUTY TO PUT YOUR NAME EXTENSION HERE !

I can’t test with your raw format because such files aren’t available on my machine.

The history is :

tell application "Script Editor"
	choose folder
		--> alias "SSD 500:Users:yvankoenig:Desktop:parent:"
end tell
tell application "System Events"
	get every folder of alias "SSD 500:Users:yvankoenig:Desktop:parent:"
		--> {folder "SSD 500:Users:yvankoenig:Desktop:parent:100EOS5D:", folder "SSD 500:Users:yvankoenig:Desktop:parent:101EOS5D:", folder "SSD 500:Users:yvankoenig:Desktop:parent:101NCD80:"}
	get POSIX path of folder "SSD 500:Users:yvankoenig:Desktop:parent:100EOS5D:"
		--> "/Users/yvankoenig/Desktop/parent/100EOS5D"
	get name of folder "SSD 500:Users:yvankoenig:Desktop:parent:100EOS5D:"
		--> "100EOS5D"
	make new folder at end of alias "SSD 500:Users:yvankoenig:Desktop:parent:" with properties {name:"Position_01"}
		--> folder "SSD 500:Users:yvankoenig:Desktop:parent:Position_01:"
	make new folder at end of folder "Position_01" of alias "SSD 500:Users:yvankoenig:Desktop:parent:" with properties {name:"Position_01_Raw"}
		--> folder "SSD 500:Users:yvankoenig:Desktop:parent:Position_01:Position_01_Raw:"
	get name of every file of folder "SSD 500:Users:yvankoenig:Desktop:parent:100EOS5D:" whose visible = true
		--> {"DSC_0044.NEF", "DSC_0045.NEF", "DSC_0046.jpg", "DSC_0047.NEF", "DSC_0048.NEF"}
	make new folder at end of folder "Position_01" of alias "SSD 500:Users:yvankoenig:Desktop:parent:" with properties {name:"Position_01_Jpg"}
		--> folder "SSD 500:Users:yvankoenig:Desktop:parent:Position_01:Position_01_Jpg:"
end tell
tell current application
	do shell script "mv '/Users/yvankoenig/Desktop/parent/100EOS5D/DSC_0044.NEF' '/Users/yvankoenig/Desktop/parent/Position_01/Position_01_Raw'"
		--> ""
	do shell script "mv '/Users/yvankoenig/Desktop/parent/100EOS5D/DSC_0045.NEF' '/Users/yvankoenig/Desktop/parent/Position_01/Position_01_Raw'"
		--> ""
	do shell script "mv '/Users/yvankoenig/Desktop/parent/100EOS5D/DSC_0046.jpg' '/Users/yvankoenig/Desktop/parent/Position_01/Position_01_Jpg'"
		--> ""
	do shell script "mv '/Users/yvankoenig/Desktop/parent/100EOS5D/DSC_0047.NEF' '/Users/yvankoenig/Desktop/parent/Position_01/Position_01_Raw'"
		--> ""
	do shell script "mv '/Users/yvankoenig/Desktop/parent/100EOS5D/DSC_0048.NEF' '/Users/yvankoenig/Desktop/parent/Position_01/Position_01_Raw'"
		--> ""
end tell
tell application "System Events"
	delete folder "SSD 500:Users:yvankoenig:Desktop:parent:100EOS5D:"
	get POSIX path of folder "SSD 500:Users:yvankoenig:Desktop:parent:101EOS5D:"
		--> "/Users/yvankoenig/Desktop/parent/101EOS5D"
	get name of folder "SSD 500:Users:yvankoenig:Desktop:parent:101EOS5D:"
		--> "101EOS5D"
	make new folder at end of alias "SSD 500:Users:yvankoenig:Desktop:parent:" with properties {name:"Position_02"}
		--> folder "SSD 500:Users:yvankoenig:Desktop:parent:Position_02:"
	make new folder at end of folder "Position_02" of alias "SSD 500:Users:yvankoenig:Desktop:parent:" with properties {name:"Position_02_Raw"}
		--> folder "SSD 500:Users:yvankoenig:Desktop:parent:Position_02:Position_02_Raw:"
	get name of every file of folder "SSD 500:Users:yvankoenig:Desktop:parent:101EOS5D:" whose visible = true
		--> {"DSC_0041.NEF", "DSC_0042.NEF", "DSC_0043.NEF", "DSC_0044.NEF", "DSC_0049.jpg"}
	make new folder at end of folder "Position_02" of alias "SSD 500:Users:yvankoenig:Desktop:parent:" with properties {name:"Position_02_Jpg"}
		--> folder "SSD 500:Users:yvankoenig:Desktop:parent:Position_02:Position_02_Jpg:"
end tell
tell current application
	do shell script "mv '/Users/yvankoenig/Desktop/parent/101EOS5D/DSC_0041.NEF' '/Users/yvankoenig/Desktop/parent/Position_02/Position_02_Raw'"
		--> ""
	do shell script "mv '/Users/yvankoenig/Desktop/parent/101EOS5D/DSC_0042.NEF' '/Users/yvankoenig/Desktop/parent/Position_02/Position_02_Raw'"
		--> ""
	do shell script "mv '/Users/yvankoenig/Desktop/parent/101EOS5D/DSC_0043.NEF' '/Users/yvankoenig/Desktop/parent/Position_02/Position_02_Raw'"
		--> ""
	do shell script "mv '/Users/yvankoenig/Desktop/parent/101EOS5D/DSC_0044.NEF' '/Users/yvankoenig/Desktop/parent/Position_02/Position_02_Raw'"
		--> ""
	do shell script "mv '/Users/yvankoenig/Desktop/parent/101EOS5D/DSC_0049.jpg' '/Users/yvankoenig/Desktop/parent/Position_02/Position_02_Jpg'"
		--> ""
end tell
tell application "System Events"
	delete folder "SSD 500:Users:yvankoenig:Desktop:parent:101EOS5D:"
	get POSIX path of folder "SSD 500:Users:yvankoenig:Desktop:parent:101NCD80:"
		--> "/Users/yvankoenig/Desktop/parent/101NCD80"
	get name of folder "SSD 500:Users:yvankoenig:Desktop:parent:101NCD80:"
		--> "101NCD80"
end tell

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) dimanche 18 décembre 2016 20:53:13

Yvan,

Thank you for your continued help. This works perfectly!!!

Sorry I wasn’t more clear about the parameters of the script in my original post.

Thank you for the feedback.

I deleted the script embedded in message #2

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) dimanche 18 décembre 2016 22:08:46

As calling do shell script takes time, I rewrote the script so that it call do shell script to move lists of files all at once.

on run
	set mainFolder to choose folder
	my germaine(mainFolder)
end run

on open sel
	set mainFolder to item 1 of sel
	my germaine(mainFolder)
end open

on germaine(mainFolder)
	set myRaw to ".NEF" # IT'S YOUR DUTY TO PUT YOUR NAME EXTENSION HERE !
	
	tell application "System Events"
		set theFolders to folders of mainFolder
		set knt to 0
		repeat with aFolder in theFolders
			tell me to set POSIXsource to POSIX path of aFolder
			if POSIXsource does not end with "/" then set POSIXsource to POSIXsource & "/"
			set origName to name of aFolder
			if origName contains "EOS5D" then
				set knt to knt + 1
				set kntPadded to text -2 thru -1 of ((1000 + knt) as text)
				set newName to "Position_" & kntPadded
				try
					make new folder at end of mainFolder with properties {name:newName}
				end try
				
				set thePaths to (path of every file of aFolder whose visible is true)
				set theRaws to {}
				set theJpgs to {}
				repeat with aPath in thePaths
					if aPath ends with myRaw then
						set end of theRaws to my getPOSIXPath(aPath)
					else if (aPath ends with ".jpg") or aPath ends with ".jpeg" then
						set end of theJpgs to my getPOSIXPath(aPath)
					end if
				end repeat
				if theRaws ≠ {} then
					set newRaw to newName & "_Raw"
					try
						make new folder at end of folder newName of mainFolder with properties {name:newRaw}
					end try
					set POSIXRaw to my getPOSIXPath((mainFolder as text) & newName & ":" & newRaw)
					tell me to do shell script "mv " & my recolle(theRaws, " ") & space & POSIXRaw # Move every Raws
				end if
				if theJpgs ≠ {} then
					set newJpg to newName & "_Jpg"
					try
						make new folder at end of folder newName of mainFolder with properties {name:newJpg}
					end try
					set POSIXJpg to my getPOSIXPath((mainFolder as text) & newName & ":" & newJpg)
					tell me to do shell script "mv " & my recolle(theJpgs, " ") & space & POSIXJpg # move every Jpgs
				end if
				delete aFolder
			end if # origname.
		end repeat
	end tell
end germaine

#=====

on getPOSIXPath(hfsPath)
	return quoted form of POSIX path of hfsPath
end getPOSIXPath

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) lundi 19 décembre 2016 09:49:37