Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 10-03-2007, 11:40 AM   #1
1981
Prospect
 
Join Date: Oct 2007
Posts: 5
Wink To Applescript, Shell command or Automater?

Hi,

I'm looking for some advice and (possible) help to implement some type of automated workflow of a set of directories.

Essentially, I need some way of taking a directory of any size and running it through a workflow that automatically creates multiple directories based on a maximum size (i.e. 4gb for DVD media) that are incrementally named.

For example I have a folder called "Work" that contains 40gb worth of other directories. The workflow then creates 10 Directories named "Archive 1, Archive 2 and so on..." and moves the files around so each Archive directory holds around 4gb

*Can you guess what I'm trying to do

I would appreciate any help or advice if this would be better being an Applescript, Shell Command, or an Automater workflow. Also being really cheeky if anyone can give me a starter for 10 that would be magic.

Thanks in advance for any help.
1981 is offline   Reply With Quote
Old 10-03-2007, 12:02 PM   #2
cwtnospam
League Commissioner
 
Join Date: Jan 2005
Posts: 8,475
Have you heard of Time Machine? It will be part of Leopard. Maybe you should wait a few weeks and use that.
cwtnospam is offline   Reply With Quote
Old 10-03-2007, 12:07 PM   #3
1981
Prospect
 
Join Date: Oct 2007
Posts: 5
Ah, yes that would be lovely - unfortunately it all has to go back to PC based server, so I'll need an independent workflow rather than something built into the OS. But thanks for the reply.
1981 is offline   Reply With Quote
Old 10-03-2007, 03:29 PM   #4
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,263
this is fairly easy to do in applescript, though I'm guessing (perhaps erroneously) that there are other constraints (such as you'd prefer to keep files with similar dates together, or files with similar names, or you'd like to waste as little space as possible on the DVDs). these other constraints can complicate things immensely. but the basic script looks like this:

Code:
tell application "Finder"
	set burnFolderContainer to make new folder at desktop with properties {name:"Burn Baby Burn"}
	set theItems to every item of folder "path to folder to be copied"
	set Gb to 1.073741824E+9
	-- I used the precise number for a Gb; you might want to round it to 1000000000
	set sizeCap to 4 * Gb
	set i to 1
	set j to 1
	-- the '600 times' here and below is just to avoid endless loops if something goes awry.  
	-- if for some reason you need to make more then 600 burn folders, you scare me.  :)
	repeat 600 times
		set burnFolder to make new folder at burnFolderContainer with properties {name:("Burn Me " & i & ".fpbf")}
		set extension hidden of burnFolder to true
		set cumulativeSize to 0
		repeat 600 times
			if j is greater than (count of theItems) then
				beep
				return
			end if
			set fileSize to physical size of (item j of theItems)
			if fileSize is greater than sizeCap then
				display alert "File " & (name of item j of theItems) & " skipped because it is larger than the disc size cap."
				set j to j + 1
			else if fileSize + cumulativeSize is less than sizeCap then
				make new alias at burnFolder to item j of theItems
				set j to j + 1
				set cumulativeSize to cumulativeSize + fileSize
			else
				exit repeat
			end if
		end repeat
		set i to i + 1
	end repeat
end tell
this goes through the folder item by item, putting aliases for the subfolders in a burn folder until maximum size is reached, and then making a new burn folder and starting over. you can then just open the burn folders and burn them to disk, and discard them afterwards.

I don't suppose it would be too hard to turn it into an automator workflow, if that's what you wanted...
tw is offline   Reply With Quote
Old 10-03-2007, 03:41 PM   #5
cpragman
All Star
 
Join Date: Jan 2004
Location: Limerick, PA
Posts: 858
zip and tar both claim to support "spanning" to break an archive into multiple members. That might be simpler for you, and save on disks as well.
cpragman is online now   Reply With Quote
Old 10-03-2007, 03:59 PM   #6
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,263
well, for that matter, I seem to remember that Toast will automatically split things into multiple disks (if you're burning as HFS+). the only issue I would have with a segmented zip or hqx, though, is that you're in trouble if one of the disks gets scratched or lost.
tw is offline   Reply With Quote
Old 10-04-2007, 02:37 PM   #7
1981
Prospect
 
Join Date: Oct 2007
Posts: 5
wow, thank you to everyone for all their help! Very much appreciated.

TW, sorry to be a pain, I'm more used to a CLI - how does one specify the "path to directory" in Applesript? I've tried /Users/Username/Desktop to no avail. (And of course, a huge thank you for writing the script!)
1981 is offline   Reply With Quote
Old 10-04-2007, 03:03 PM   #8
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,263
well, in applescript you use the colon notation rather than the slash notation. so, to reference a folder on the desktop, the path would be:
Code:
HD:Users:Username:Desktop:Folder Name
you can also use either of the following, for portability:
Code:
-- use this only outside of a Finder tell block
set theThing to (path to desktop as text) & "Folder Name"

--inside a Finder tell, you have desktop as a special term, so...
tell application "Finder"
     set theThing to (desktop as text) & "Folder Name"
end tell
to make the text string into a file reference:
Code:
-- outside a Finder tell
set theThing to alias "HD:Users:Username:Desktop:Folder Name"

--inside a Finder tell
set theThing to file "HD:Users:Username:Desktop:Folder Name:Some File"
set theThing to folder "HD:Users:Username:Desktop:Folder Name"
tw is offline   Reply With Quote
Old 10-06-2007, 12:50 PM   #9
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,156
I am trying to use this rather nice "splitter-upper" script on a very large folder stored on a mounted volume that is not the root volume. It fails because it says the fileSize is missing value. I remember something about the Finder being reluctant to compute size on other volumes, but not how to cure that.
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)
NovaScotian is offline   Reply With Quote
Old 10-06-2007, 04:34 PM   #10
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,263
Quote:
Originally Posted by NovaScotian
I am trying to use this rather nice "splitter-upper" script on a very large folder stored on a mounted volume that is not the root volume. It fails because it says the fileSize is missing value. I remember something about the Finder being reluctant to compute size on other volumes, but not how to cure that.

well, there shouldn't be a problem using this script on a non-root volume (unless the volume is not hfs...). Unfortunately, I don't have my secondary drive with me at the moment, so I can't test it. I have noticed that the Finder is lazy about creating physical sizes. for instance, if I pick a random folder and and run this scriptlet a few times:

Code:
get physical size of every item of folder "--random--"
the first couple of times the list will be filled with missing values, and then suddenly all the correct values are there. that might solve your problem straight out.

you could also hack the file size up out of unix, at need:
Code:
set fileSize to physical size of (item j of theItems)
if fileSize is missing value then 
	set filePath to quoted form of (POSIX path of ((item j of theItems) as text))
	set fileSize to (do shell script "du -sk " & filePath & " | awk '{print $1}' -") * 1024
end if
that should give the same file size, though I'm suspicious about its accuracy if it hits an alias or an old-style dual fork file.

Last edited by tw; 10-06-2007 at 04:35 PM. Reason: spelling error
tw is offline   Reply With Quote
Old 10-06-2007, 04:57 PM   #11
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,156
Quote:
Originally Posted by tw
...I pick a random folder and and run this scriptlet a few times:

Code:
get physical size of every item of folder "--random--"
the first couple of times the list will be filled with missing values, and then suddenly all the correct values are there. that might solve your problem straight out.

Thanks tw: I think the quote above is the clue -- run a little loop asking for the physical size that continues (with a short delay) while PS is 'missing value' and then continues. Alternatively, put the phys size in an try block, and just try again. I'll try those on for size (sorry about that).
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)
NovaScotian is offline   Reply With Quote
Old 10-08-2007, 01:17 PM   #12
1981
Prospect
 
Join Date: Oct 2007
Posts: 5
Sorry to be a pain, just got back to this tonight and tried to run the script as a test. An alert box pops up saying "AppleScript Error: Can't make missing value into type number."

With the value "cumulativeSize" highlighted on the line:

"else if fileSize + cumulativeSize is less than sizeCap then"

Sorry to be such a newbie.
1981 is offline   Reply With Quote
Old 10-08-2007, 01:29 PM   #13
1981
Prospect
 
Join Date: Oct 2007
Posts: 5
sorry, my bad - seems some of the folders had bad permissions/errors. All fixed now and working a bloody treat! Thank You!
1981 is offline   Reply With Quote
Old 10-08-2007, 01:57 PM   #14
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,263
Quote:
Originally Posted by 1981
sorry, my bad - seems some of the folders had bad permissions/errors. All fixed now and working a bloody treat! Thank You!

ah, yeah, this is something NovaScotian discovered earlier. seems the Finder is a bit slow in calculating sizes for items. for future attempts, you might want to add this bit of code at the beginning of the Finder tell block (right after the tell application "Finder" line):

Code:
set splitThisFolder to folder "path to folder to be copied"
repeat 120 times
	set dummyVar to physical size of every item of splitThisFolder
	if dummyVar does not contain missing value then exit repeat
	delay 1
end repeat
basically this bit waits patiently while the Finder calculates all the item sizes in that folder before letting you move on to the rest of the script
tw is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:32 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.