Mounting disk images

Hi everyone

Complete applescript newbie here. I was hoping you could help me out.

Im creating an applescriptobjc app in xcode to backup some files to to a USB stick that has an encrypted .dmg file on it. What I want it to do is mount the image copy the files unmount the image and unmount the USB stick.

What I’ve got so far is this:


script Server_BackupAppDelegate
	property parent : class "NSObject"
	
	on clickedUSB_(sender)
		
		try
			tell application "Finder"
				set imageFile to POSIX path of (file "Secure Disk.dmg" of disk "Genie Backup" as alias)
				
			end tell
		on error
			display alert "Cannot find the backup destination. Please insert USB stick and try again."
			return
		end try
		
		
		tell application "System Preferences" to quit
		if ((imageFile as string) ends with ".dmg") then

			do shell script "/bin/bash hdiutil mount \"" & ¬
				POSIX path of (imageFile as string) & "\""

		end if

		repeat with aFile in genieFiles
			tell application "Finder" to duplicate file (aFile as alias) to disk "Secure Disk" with replacing
		end repeat
		
		tell application "Finder"
			eject disk "Secure Disk"
			eject disk "Genie Backup"
		end tell
		
		tell application "System Preferences" to launch
		
		display alert "Backup Complete. Safe to now remove USB"
		
	end clickedUSB_
	
	
	on applicationWillFinishLaunching_(aNotification)
		set genieFiles to {":Users:user:Desktop:Genie:Genie 3.rtf", ¬
			":Users:user:Desktop:Genie:Genie 1.rtf", ¬
			"Users:user:Desktop:Genie:Genie 2.rtf"}
		
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

I initially created this script in AppleScript editor and then transferred it to xcode putting in the appropriate objc parts. It runs beautifully in Applescript editor but as soon as I try to run it in xcode it gets up to the do shell script and either does nothing or hangs and I have to terminate the process via xcode.

Any ideas on how to get this working in xcode?

Model: MacBook
AppleScript: Xcode 3.2.1
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

Try:

do shell script "/bin/bash hdiutil mount " & ¬
quoted form of imageFile

no dice… it just did nothing :frowning: i also tried


do shell script "hdiutil mount " & ¬
quoted form of imageFile

but it just hung and i had to terminate the process through xcode. the weird thing is this works perfectly fine in applescript editor!

thanks though!

I suspect your problem is in how you’re building the POSIX path. I just tried hdiutil with a known path, and it works the same in an ASObjC project as it does in a script editor.

BTW, you should not try to do your own quoting of POSIX paths – that’s what “quoted form of” is for.

Turns out the problem is to do with the fact that the .dmg files are encrypted. Applescript Editor handles this fine but Xcode doesn’t for some reason.

I tested 4 different files with my original script an encrypted and a non encrypted on both my internal HD and on the usb stick. Both non encrypted files worked and both encrypted files caused the app to hang.

Im guessing its something to do with authenticating the mount because when I save the password to my keychain it works.

Any idea on how to get applescriptobjc to handle the authentication?

Cheers

Try wrapping the “do shell script” command in a “tell current application” block. Just a wild guess…

still no luck unfortunately :frowning:

Solved it!! Through sheer serendipity I stumbled across the solution!

Turns out the shell script needed an & on the end. No idea why.

ie.


			tell current application to do shell script ("hdiutil mount \"" & ¬
				POSIX path of (filePath as string) & "\"&")

Im not all that familiar with shell scripts. Is there anyone who can tell me what I’ve actually done by including the & on the end? Just for future reference.

Cheers

Adding a & to the end of a shell command makes it run in background. not sure why that should make the difference though.

rob