Create diskimage without files, Only folderstructure

I found this old script here @ MacScripter and this does almost what I need


set Ordner to choose folder

tell application "Finder"
    set diskNames to name of Ordner
    set myNewPath to POSIX path of (path to desktop folder) as string
    set theListe to every process
end tell

set myNewPath to myNewPath & diskNames & ".dmg"
set Quellordner to POSIX path of Ordner as string

set Skript to "hdiutil create -srcfolder " & Quellordner & " " & myNewPath

do shell script Skript

I need to make images from disks or folders but without all the files stored in those folders or disks. Only the volume/foldername and folderstructure is needed.

I tried to alter the script with other hdutil commands but I still get the same result.

Anyhelp would be highly appreciated.

Hello.

incidentally I recently wrote the deletion of every file in a subtree as an example of a general construct for traversing a subtree, in the post here.

You would first make a copy of your folder which contains the directory structure you want to replicate.
Then you would loot my code and use something like this on the duplicated folder:


set theRootFolderAsAlias to choose folder

traverseTree(theRootFolderAsAlias, fileHandlerObject, missing value)

Then you would have to make the names and such match to the volume names and such in your hdiutil statement.

This should be the easiest way, if not fastest, do you want speed, then you would investigate the find command with all its options, or maybe you can do it with rsync, the example in the post are ready made and known to work.

It would be very nice if you posted any problems with having my code working, or if it made it easy to get the desired result.

Thanks for your answer & ideas however I think this gets a little bit to complicated. A hacked this together and I am almost there. I have it working but can do better. Now you first have to choose the disk. The folderstructure is copied to a folder on the desktop. 2nd part of te script ask to point to a folder and the image is created. I would like to see verything done in just one click. Choose disk and go…

The only thing I have to figure out is

  • autocreate the image. don’t ask the user to choose the folder that has been created on the desktop
  • delete the folder that has been made on the desktop. only keep the image
  • set the image to 100MB


-- COPY FOLDERS & SUBFOLDERS FROM A DISK
set sourceFolder to choose from list (list disks)

set sourceFolderName to (sourceFolder as text)

set destinationFolder to ((path to desktop as text) & (sourceFolder as text))

set folderList to {}

tell application "Finder"
	set folderList to every folder of entire contents of folder sourceFolder as alias list
end tell

set imagename to sourceFolder as text

if folderList is not {} then
	do shell script "mkdir -p " & quoted form of POSIX path of destinationFolder
	
	set pathLength to ((length of POSIX path of sourceFolder) + 2)
	repeat with oneFolder in folderList
		set restPath to quoted form of POSIX path of (oneFolder as Unicode text)
		set restPath to text pathLength thru -2 of restPath as Unicode text
		set eindres to POSIX path of destinationFolder & restPath
		do shell script "mkdir -p " & (quoted form of POSIX path of (eindres)) -- with administrator privileges
		
		
	end repeat
end if

-- CHOOSE EARLIER CREATED FOLDER ON DESKTOP AND CREATE IMAGE
set Ordner to choose folder
tell application "Finder"
	set diskNames to name of Ordner
	set myNewPath to POSIX path of (path to desktop folder) as string
	--set theListe to every process
	set theListe to every folder of entire contents of folder Ordner as alias list
	
end tell

set myNewPath to myNewPath & diskNames & ".dmg"
set Quellordner to POSIX path of Ordner as string

set Skript to "hdiutil create -srcfolder " & Quellordner & " " & myNewPath

do shell script Skript

Any suggestions would be appreciated :slight_smile:

KMLSKI

Already figured out how to

autocreate the image. don’t ask the user to choose the folder that has been created on the desktop

  • delete the folder that has been made on the desktop. only keep the image

Now only the 100mb part and I’m ready :slight_smile:

Hello.

I’m recently sure that you have already found that in the hdiutil man page, but just for the record read the
create size_spec image section in that man page. You can see that man page by entering man hdiutil in a terminal window. You may press “h” to see some help while watching the manual page.

Yes I did :slight_smile: the complete scipt is now:



-- COPY FOLDERS & SUBFOLDERS FROM A DISK
set sourceFolder to choose from list (list disks)

set sourceFolderName to (sourceFolder as text)

set destinationFolder to ((path to desktop as text) & (sourceFolder as text))

set folderList to {}

tell application "Finder"
	set folderList to every folder of entire contents of folder sourceFolder as alias list
end tell

set imagename to sourceFolder as text

if folderList is not {} then
	do shell script "mkdir -p " & quoted form of POSIX path of destinationFolder
	
	set pathLength to ((length of POSIX path of sourceFolder) + 2)
	repeat with oneFolder in folderList
		set restPath to quoted form of POSIX path of (oneFolder as Unicode text)
		set restPath to text pathLength thru -2 of restPath as Unicode text
		set eindres to POSIX path of destinationFolder & restPath
		do shell script "mkdir -p " & (quoted form of POSIX path of (eindres)) -- with administrator privileges
		
		
	end repeat
end if

-- CHOOSE EARLIER CREATED FOLDER ON DESKTOP AND CREATE IMAGE
set Ordner to destinationFolder & ":" as alias

tell application "Finder"
	set diskNames to name of Ordner
	set myNewPath to POSIX path of (path to desktop folder) as string
	--set theListe to every process
	set theListe to every folder of entire contents of folder Ordner as alias list
	
end tell

set myNewPath to myNewPath & diskNames & ".dmg"
set Quellordner to POSIX path of Ordner as string
set imagesize to "100"

set Skript to "hdiutil create -srcfolder " & Quellordner & " " & "-size 1g " & myNewPath
do shell script Skript

tell application "Finder"
	set v to alert volume of (get volume settings)
	set volume alert volume 0
	delete folder destinationFolder
	set volume alert volume v
	
end tell

thanks !

Hi,

you can omit this whole part, because you’re retrieving the folder list a second time but never use it


.
set Ordner to destinationFolder & ":" as alias

tell application "Finder"
   set diskNames to name of Ordner
   set myNewPath to POSIX path of (path to desktop folder) as string
   --set theListe to every process
   set theListe to every folder of entire contents of folder Ordner as alias list
   
end tell
.

Here is a version, which uses Spotlight to get the folder hierarchy.
It’s probably much faster than entire contents in the Finder


set sourceFolder to (choose from list (list disks))
if sourceFolder is false then return
set sourceFolder to alias (item 1 of sourceFolder)
set sourceFolderName to name of (info for sourceFolder)
set destinationFolder to POSIX path of (path to desktop) & sourceFolderName
set dmgPath to quoted form of (destinationFolder & ".dmg")
set FolderList to paragraphs of (do shell script "/usr/bin/mdfind -onlyin " & quoted form of POSIX path of sourceFolder & " 'kMDItemContentType == \"public.folder\"'")
if FolderList is not {} then
	do shell script "/bin/mkdir " & quoted form of destinationFolder
	set pathLength to (length of POSIX path of sourceFolder)
	repeat with oneFolder in rest of FolderList
		set restPath to text pathLength thru -1 of oneFolder
		do shell script "/bin/mkdir -p " & quoted form of (destinationFolder & restPath)
	end repeat
	do shell script "/usr/bin/hdiutil create -srcfolder " & quoted form of destinationFolder & " -megabytes 100 " & dmgPath
	do shell script "/bin/rm -rf " & quoted form of destinationFolder
end if

Stefan,

As always thank you very much. Your script is faster and shorter.

But I was just figuring out how to get write acces to the image. Both of our script create the same end result but
I am not allowed to write files on the mounted image.

KML

The default format for a disk image is compressed / Read only.
Add the -format parameter


.
	do shell script "/usr/bin/hdiutil create -srcfolder " & quoted form of destinationFolder & " -format UDRW -megabytes 100 " & dmgPath
.

Great and works fie!

Stefan,

Your script does’nt work when there is a in a volume name for example ‘DISK 2’

This returns nothing.

Tried to fiddle around with Posix path, quoted form etc but still couldn’t figure out where to
change and what has to be changed.

Kind regards,

KML

And offcourse my script has the same problems. Can you maybe also point out where to change some code and could you explain what you mean with ‘omit this whole part’? Still want to learn from my mistakes.

Thanks as always!

KmL:)

In this “code to omit” part there is a line “set theListe to.”
The variable theListe won’t be used at all later in the script.
I think this came from putting code snippets together.

I can’t see any error in my script, I’m always quoting every POSIX path for being used in a shell script.
But I will rename a disk and try to reproduce the error.

PS: I suspect you changed my script, here it works perfectly even with a disk name containing space characters
Edit Or the disk is not indexed by Spotlight

Ah that might be the case. It’s a video disk with more then 2T of movies and this disk is not indexed by spotlight. I also want to use it on volumes of 20TB or more and those are also not indexed by spotlight. Most of the time a video disk is not indexed by Spotlight if you want fast access.

:frowning:

I did not change anything in your script anyhow. You are the script guru. Why should I :wink:

I will try to change my script. Maybe I can get that one working. Don’t mind if it’s slower then yours.

Thanks again :slight_smile:

KML:)

Stefan,

If you have any time…

This is the line for the spotlight search routine

set folderList to paragraphs of (do shell script “/usr/bin/mdfind -onlyin " & quoted form of POSIX path of sourceFolder & " ‘kMDItemContentType == "public.folder"’”)

Can you point me in a direction for an alternative way so not to use spotlight?

Kind regards

Hello.

I know you addressed Stefan but I could not help help my self, so I created a solution almost like your own, but based on Stefan’s code for using Spotlight.



set fromDisk to false

if fromDisk is true then
	set sourceFolder to (choose from list (list disks))
	if sourceFolder is false then return
	set sourceFolder to alias (item 1 of sourceFolder)
else
	set sourceFolder to (choose folder)
end if

set sourceFolderName to name of (info for sourceFolder)
set destinationFolder to (path to desktop as Unicode text) & sourceFolderName

set dmgPath to quoted form of POSIX path of (destinationFolder & ".dmg")
tell application "Finder"
	set FolderList to every folder of entire contents of folder sourceFolder as alias list
end tell
if FolderList is not {} then
	do shell script "/bin/mkdir " & quoted form of POSIX path of destinationFolder
	set pathLength to (length of (sourceFolder as Unicode text))
	
	repeat with oneFolder in FolderList
		set restPath to text pathLength thru -2 of (oneFolder as Unicode text)
		set finalPath to quoted form of POSIX path of (destinationFolder & restPath)
		do shell script "/bin/mkdir -p " & finalPath
	end repeat
	do shell script "/usr/bin/hdiutil create -srcfolder " & quoted form of POSIX path of destinationFolder & " -megabytes 100 " & dmgPath
	do shell script "/bin/rm -rf " & quoted form of POSIX path of destinationFolder
end if


McUser,

Your input is also highly appreciated :slight_smile:

When I choose a folder without a space in the name (example ‘FOLDERX’) I get this error:
error “hdiutil: create failed - No such file or directory” number 1

I see the initial folder created on my desktop with all the subfolders, but somehow the image can’t be created I think.

Also when I choose a folder or volume with a space in the name (example ‘FOLDER Y Z’) I get the error error “Can’t make alias "FOLDER Y Z" into type alias list.” number -1700 from alias “FOLDER Y Z” to «class alst». This is the same error I get with my script and I can’t solve it.

Also I would try another way of indexing the folders. I use the ‘entire contents in the Finder’ way, because not al the folders that I want images of are on my mac and some of these folders or disks are not indexed by spotlight.

Well I’ll also keep on trying experimenting today :slight_smile:

KML

Hi,

destinationFolder must be a POSIX path and there were some other little problems.
Try this


set sourceFolder to (choose from list (list disks))
if sourceFolder is false then return
set sourceFolder to alias (item 1 of sourceFolder)
set sourceFolderName to name of (info for sourceFolder)
set destinationFolder to POSIX path of (path to desktop) & sourceFolderName

set dmgPath to quoted form of (destinationFolder & ".dmg")
tell application "Finder"
	set FolderList to every folder of entire contents of sourceFolder as alias list
end tell
if FolderList is not {} then
	do shell script "/bin/mkdir -p " & quoted form of destinationFolder
	set pathLength to (length of POSIX path of sourceFolder)
	
	repeat with oneFolder in FolderList
		set restPath to text pathLength thru -1 of (POSIX path of oneFolder)
		set finalPath to quoted form of (destinationFolder & restPath)
		do shell script "/bin/mkdir -p " & finalPath
	end repeat
	do shell script "/usr/bin/hdiutil create -srcfolder " & quoted form of destinationFolder & " -megabytes 100 " & dmgPath
	do shell script "/bin/rm -rf " & quoted form of destinationFolder
end if

Hello.
I just updated the code in the post above, when I saw that Stefan had beaten me with a working solution.
I have added POSIX Path of a couple of places, but his solution is still cleaner, as he made a POSIX path earlier in the scrIpt. :slight_smile:

@Stefan: MacScripterWatcher is really neat; it is great to be able to sit there and watch people come and go. It is kind of social - yet kind of “magical”.:slight_smile:
It reminds me of “that map” in one of the Harry Potter movies. :slight_smile: