Mount disk image and copy contents (of image) to folder

I have now tried several applescripts and automator and shell to try and achieve this with no luck.

Would like to copy the contents of .img files to the same folder where the .img file is stored.

Could be done with automator (watch folder that triggers the script)

Most scripts out there assume that I know the name of the disc image, wich I don’t.

Asked for help in another tread but that does not work, even though I have tried to modify it.

Something like this is what I would like to achive but with disc images in .img format:
(Almost impossible to google this and find anything. Maybe it aint doable?

set diskName to cdDVDName()
if diskName is missing value then error "Could not get the name of the inserted disk"

on cdDVDName()
    set diskName to missing value
        set theConst to "Volume Name: "
        set drutilStatus to do shell script "drutil status -drive internal | grep \"/dev/\""
        set theDrive to "/dev/" & item -1 of (words of drutilStatus)
        set diskutilInfo to do shell script "diskutil info " & theDrive & " | grep \"" & theConst & "\""

        set text item delimiters to theConst
        set a to text items of diskutilInfo
        set text item delimiters to ""
        set diskName to item -1 of a
        repeat while diskName begins with space
            set diskName to text 2 thru -1 of diskName
        end repeat
        repeat while diskName ends with space
            set diskName to text 1 thru -2 of diskName
        end repeat
    end try
    return diskName
end cdDVDName


Here’s a proof-of-concept script which works with the .dmg files I have. Do note the caveats though.

Your query suggests that you may be thinking of turning it into a “watch folder” script or folder action, presumably tiggered by the appearance of the disk image file in the folder. Don’t forget that copying its contents into the folder will retrigger the action.

	The code below contains no error checking and makes assumptions which may not be true in all cases.
	1. That a disk image file exists in the chosen folder and that it's the only such file in the folder.
	2. That ditto will successfully copy over all the disk's normal contents before attempting to copy any trashes etc. for which permission will be denied. (The error generated by any permission denial is hidden from the script because the ditto command's followed by another command in the same shell script.)
	3. That there's nothing else which could go wrong..

-- Get the POSIX path to the folder.
set folderPath to POSIX path of (choose folder)
-- Get the POSIX path to the disk image file in the folder. 
set imagePath to (do shell script ("find -f " & quoted form of folderPath & " \\( -type f \\( -name '*.?mg' -or -name '*.smi' \\) -depth 1 \\)"))
-- Mount the image and get the path to the resulting volume.
set mountPath to (do shell script ("hdiutil attach " & quoted form of imagePath & " | grep -o '/Volumes/.*'"))
-- Copy the volume's contents to the original folder and dismount the volume.
do shell script ("ditto " & quoted form of mountPath & space & quoted form of folderPath & " ; hdiutil detach " & quoted form of mountPath)