Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script for launching a DMG file

Hi,

I posted this question in another forum and it was suggested that I try this forum.

Desired functionality: Insert USB flash drive into USB port and launch an encrypted DMG file so that it mounts (on machines where the password is saved in the keychain) and is ready to use along with the flash drive.

I have never created an applescript before, so I would be greatful for as much detail as possible about what i should do.

Thanks.

iMac(intel), MacBook (intel), MacBook Pro(intel), Mac OS X (10.4.9)

Posted on May 3, 2007 5:30 PM

Reply
Question marked as Best reply

Posted on May 7, 2007 6:07 PM

To write an AppleScript, you need to use the Script Editor. This is located in your Applications folder, in a folder called AppleScript.

You can easily mount a disc image using AppleScript, simply by pointing to the location of the image and using the hdiutil command, through a shell script (executes the command in the Terminal).

do shell script "hdiutil mount 'diskimagename.dmg'"

So, in your case, you're mounting the file off a flash drive. So your command might look something like this:

do shell script "hdiutil mount '/Volumes/My Flash Drive/encrypted.dmg'"

You will want to substitute 'My Flash Drive' for the name of your flash drive. If you like, you can insert the flash drive, navigate to the file, ctrl+click (or right-click) the DMG file, select Get Info, and simply copy the 'Where' value under 'General', adding the name of the DMG file on the end.

Your code will probably look a lot like this:

try
do shell script "hdiutil mount '/Volumes/My Flash Drive/encrypted.dmg'"
on error
(* Do nothing. *)
end try


I've used a try with an error handler so that it tries to mount the volume, and if it can't find it (the flash drive isn't plugged in) then it simply does nothing. You could also swap '(* Do nothing. *) for:

display alert "The flash drive isn't plugged in."

if you wish to be warned if the volume isn't available.

I hope this has helped!
6 replies
Question marked as Best reply

May 7, 2007 6:07 PM in response to drmoque

To write an AppleScript, you need to use the Script Editor. This is located in your Applications folder, in a folder called AppleScript.

You can easily mount a disc image using AppleScript, simply by pointing to the location of the image and using the hdiutil command, through a shell script (executes the command in the Terminal).

do shell script "hdiutil mount 'diskimagename.dmg'"

So, in your case, you're mounting the file off a flash drive. So your command might look something like this:

do shell script "hdiutil mount '/Volumes/My Flash Drive/encrypted.dmg'"

You will want to substitute 'My Flash Drive' for the name of your flash drive. If you like, you can insert the flash drive, navigate to the file, ctrl+click (or right-click) the DMG file, select Get Info, and simply copy the 'Where' value under 'General', adding the name of the DMG file on the end.

Your code will probably look a lot like this:

try
do shell script "hdiutil mount '/Volumes/My Flash Drive/encrypted.dmg'"
on error
(* Do nothing. *)
end try


I've used a try with an error handler so that it tries to mount the volume, and if it can't find it (the flash drive isn't plugged in) then it simply does nothing. You could also swap '(* Do nothing. *) for:

display alert "The flash drive isn't plugged in."

if you wish to be warned if the volume isn't available.

I hope this has helped!

May 7, 2007 6:29 PM in response to Richard Henry

Thank you. Do you know how I could get the script to launch when I insert my USB drive. I know I could launch via the pull down script menu but that is actually no shorter than the two clicks to launch the file manually though admittedly if add commands to launch my email and IM program, the script becomes more useful.

Ideally, I would like to be able to insert my USB drive and have the script launch automatically.

May 7, 2007 7:39 PM in response to drmoque

Sure thing, although this might take a moment or two to setup once you do there will be no more effort required.

The AppleScript you will need to use is:

repeat
try
do shell script "hdiutil mount '/Users/richard/Desktop/My Flash Drive/encrypted.dmg'"
on error
(* Do nothing. *)
end try
end repeat


The addition of the repeat means that this script will loop in the background only to take effect once the disk image becomes available.

Here's what to do:

1) Copy this AppleScript into the Script Editor.

2) File > Save As... > Change 'File Format' to 'application bundle', and uncheck 'Run Only' and 'Startup Screen', and then check 'Stay Open'. > Save the file somewhere that you can keep it.

3) Quit the Script Editor.

4) Navigate to the folder in which you saved your script application bundle, and ctrl+click (right click) it. Choose 'Show Package Contents'.

5) In the Contents folder, there will be a file called Info.plist. Open this file using TextEdit.

6) Scroll to the very bottom of the document, and create a new line above:

</dict>
</plist>


7) Paste on to the new line:

<key>LSUIElement</key>
<true/>


8) Save this file, and quit TextEdit.

9) Reboot your Mac (this step is essential!).

10) Open System Preferences, navigate to Accounts, select 'Login Items', and then click the + button. Select the script application bundle.

Done! Restart your Mac (again!) for the script to come into effect. Steps 4, 5, 6, 7, 8 and 9 are all to do with making the script not appear in the Dock when it's running, but if you don't mind seeing the icon constantly you can skip these steps.

This should pretty much work, but there's the slim chance I got something wrong so if this doesn't behave how you expect it to let me know and I'll figure out a fix.

All the best, hope I solved your question.

May 7, 2007 7:54 PM in response to drmoque

You mention wanting to launch an IM and Mail app?

You can launch applications in AppleScript very easily, using the tell application to activate command (I'll do a couple of examples):

tell application "Mail" to activate

tell application "Microsoft Messenger" to activate

tell application "Safari" to activate

tell application "Pages" to activate

So, in the context of our script, you could use:

repeat
try
do shell script "hdiutil mount '/Users/richard/Desktop/My Flash Drive/encrypted.dmg'"
on error
set launchApps to "false"
end try
end repeat
if launchApps is not "false" then
tell application "Mail" to activate
tell application "Microsoft Messenger" to activate
tell application "Safari" to activate
tell application "Pages" to activate
end if


This should work, let me know if it doesn't.

All the best!

May 7, 2007 9:36 PM in response to drmoque

Richard,

I almost got it working. I seem to be having troubles with the repeat loop.

This part of the script is working as it should:

try
do shell script "hdiutil mount '/Volumes/CRUZER/private.dmg'"
set launchApps to "true"
on error
set launchApps to "false"

end try

if launchApps is not "false" then
tell application "Microsoft Entourage" to activate
tell application "Portable Adium" to activate
end if


I tried putting the repeat and end repeat around the try statements. However, in that scenario, it would mount the diskimage but not lauch any of the applications.

I also tried moving the end repeat after the end if. This did manage to mount and launch the apps but then the two applications kept cycling in the finder (an endless repeat) so something was not quite right.

Is there something else i should try?

Also I was concerned about being able to eject the flash drive if we are continuously mounting it. If I am too slow will I run into a problem of not being able to eject until i can kill the script.

Thanks so much for your help so far. Even if we can not get the repeat part working I have still have a very useful script.

Script for launching a DMG file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.