Probably easy - but I am a newbie

I’ve just gotten into Applescripting and my first project has been giving me some troubles

I want to drag a folder onto a script, then have it separate the files inside into folders that are 650mb or less. This is going to be used to quickly separate files for CD burning purposes (cds are 650mb each) each new cd folder should be named what the original folder was plus CD1, CD2…ect

For example if i had a folder called This_Folder with 800mb of files inside:

I would drag the folder onto the script and it would pump out two new folders inside the original This_Folder: This_FolderCD1 (~650mb) and This_FolderCD2 (~150mb)
But I would like it to be able to do it to any folder name and any size, obviously…

any help would be great, I’m quite stuck…

Please post the code you have already written so we can use that to work from.

I’ve been working on this one. i had it working perfectly yesterday but I had the file names in the script and wanted to automate the naming. I know the problem is something really basic, but I alway mess up file locations. Somebody want to take a look and let me know what’s wrong? I’m beating my head against my desk.

on open this_folder
	set these_items to list folder this_folder without invisibles
	set stage_folder to (path to desktop as string)
	set cd_num to 0 as number
	set fol_size to 0 as number
	set current_size to 0 as number
	set fol_name to (name of (info for this_folder) as string)
	
	
	tell application "Finder"
		
		set cd_num to cd_num + 1
		
		make new folder at stage_folder with properties {name:fol_name & "CD" & cd_num}
		
		
		repeat with this_item in these_items
			set the_item to (this_item as string)
			
			set file_size to size of (info for (this_item))
			
			set fol_size to fol_size + fil_size
			
			display dialog fol_size
			if fol_size is greater than or equal to 6.5E+8 then
				set cd_num to cd_num + 1
				make new folder at stage_folder with properties {name:fol_name & "CD" & cd_num}
				set fol_size to 0 as number
				
			end if
			
			set the_item to (this_item as string)
			
			
			copy file (stage_folder & fol_name & ":" & the_item) to folder (stage_folder & "CD" & cd_num)
			
		end repeat
		
	end tell
	
end open

Well, off the cuff, I can see this problem:
You’ve declared stage_folder as a string path, but are trying to refer to it directly as a folder object.


       make new folder at stage_folder with properties {name:fol_name & "CD" & cd_num}

Change it to:

       
        make new folder at folder stage_folder with properties {name:fol_name & "CD" & cd_num}

No, Sir, the Finder accepts also a string path in the make new folder command :wink:

@ StepsAscend
First, too many useless coercions

.
 set cd_num to 0 -- this IS a number
   set fol_size to 0 -- this IS a number
   set current_size to 0 -- this.
   set fol_name to name of (info for this_folder) -- this IS a string
.

Second, the error occurs, because info for expects an alias, and doesn’t work with file specifiers of the Finder


.
set file_size to size of (info for this_item as alias)
.

StepsAscend - your code was a lot better than my first attempt. I did a lot of what looks to be unnecessary things. So I think I am going to work of off the great code you wrote. so this is what I have now (after tweaking):


on open this_folder
	set these_items to list folder this_folder without invisibles
	set stage_folder to (path to desktop as string)
	set cd_num to 0
	set fol_size to 0
	set current_size to 0
	set fol_name to name of (info for this_folder)
	
	
	tell application "Finder"
		
		set cd_num to cd_num + 1
		
		make new folder at stage_folder with properties {name:fol_name & "CD" & cd_num}
		
		
		repeat with this_item in these_items
			set the_item to (this_item as string)
			
			set file_size to size of (info for this_item as alias)
			
			set fol_size to fol_size + fil_size
			
			display dialog fol_size
			if fol_size is greater than or equal to 6.5E+8 then
				set cd_num to cd_num + 1
				make new folder at stage_folder with properties {name:fol_name & "CD" & cd_num}
				set fol_size to 0 as number
				
			end if
			
			set the_item to (this_item as string)
			
			
			copy file (stage_folder & fol_name & ":" & the_item) to folder (stage_folder & "CD" & cd_num)
			
		end repeat
		
	end tell
	
end open

Still not working just right…

This line has a wrong syntax

copy file (stage_folder & fol_name & ":" & the_item) to folder (stage_folder & "CD" & cd_num)

copy assignes a value to a variable, if you want to move one or more files, use move
or duplicate, which moves a copy of the file(s)

move file (stage_folder & fol_name & ":" & the_item) to folder (stage_folder & "CD" & cd_num)

I stand corrected. Thats what I get for not testing!

Thanks for all the wisdom guys!

@ kmrynders, if you save this as an application on your desktop, it should work. I used “duplicate” rather than “move” so as not to change the original file. You could add a line to delete the original after copying if you wanted.

Also note, this code may not neccesarily create the most efficient cd archives. For example, if you’re at 500 MB and the next file is 160, it will create a new folder, even though the next file after that may only be 10 MB. If your files are relatively small the effect will be minimal and the new folders will be close to 650 MB. Plus, CD’s are dirt cheap nowadays. Of course, modifying this to create folders the size of DVD’s would be easy. I suppose if you got creative you could write a script that checks the next 5 or so files and copies the one that would fit in the remaining space on the current disc, making it more efficient. Anyway, it’s a start!

on open this_folder
	set these_items to list folder this_folder without invisibles
	set stage_folder to (path to desktop as string)
	set cd_num to 0
	set fol_size to 0
	set fol_name to name of (info for this_folder)
		
	tell application "Finder"
		
		set cd_num to cd_num + 1
		
		make new folder at stage_folder with properties {name:fol_name & "CD" & cd_num}
				
		repeat with this_item in these_items
			set the_item to (this_item as string)
			
			set file_size to size of (info for stage_folder & fol_name & ":" & the_item as alias)
						
			set fol_size to fol_size + file_size
						
			if fol_size is greater than or equal to 6.5E+8 then
				set cd_num to cd_num + 1
				make new folder at stage_folder with properties {name:fol_name & "CD" & cd_num}
				set fol_size to 0 as number
				
			end if
			
			set the_item to (this_item as string)
						
			duplicate file (stage_folder & fol_name & ":" & the_item) to folder (stage_folder & fol_name & "CD" & cd_num)
			
		end repeat
		
	end tell
	
end open