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)
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.
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.
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.
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.
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
Oohh. Getting interesting. One of the applications I want to launch is Microsoft Entourage. the other actually sits on the DMG that gets launched. Do I need to specify the path to either of these applications?
I am going to compile at least the rest of it to see how it works out.
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.