Set Safari's download folder via script for private browsing

on run {}
    -- Set these variables to suit your system.
    -- .... 'DownloadFolder' is the path to your normal folder for downloads
    -- .... 'PrivateImage' is the path and filename of the disk image to use for downloads when private browsing is enabled
    -- .... 'PrivateVolume' is the name of your private disk image when mounted
    
    set DownloadFolder to "/Users/myusername/Downloads/Safari"
    set PrivateImage to "/Users/myusername/EncryptedDownloads.sparseimage"
    set PrivateVolume to "EncryptedDownloads"
    
    
    set doSetDownloadFolder to true
    my check_requirements()
    
    display dialog "Set Private Browsing Mode" & return with icon 1 buttons {"Cancel", "Enable", "Disable"}
    set clickedButton to button returned of result
    
    -- User canceled, just quit
    if clickedButton is "Cancel" then error number -128
    
    -- User wants to enable Private Browsing
    if clickedButton is "Enable" then
        if is_disk_mounted(PrivateVolume) is false then
            -- private disk image hasn't already been mounted, so let's do that now
            if mount_disk(PrivateImage) is false then
                -- eek.  Mounting didn't work - let's just enable Private Browsing without changing Safari's download location
                display dialog "Could not attach private volume." & return & return & ¬
                    "Private browsing will be enabled, but the download folder will not be changed." with icon 2 buttons {"Cancel", "Continue"}
                set doSetDownloadFolder to false
            else
                -- disk mounted, ready to go
                set doSetDownloadFolder to true
            end if
        else
            -- disk image has already been mounted -- we're OK to set Safari's download location
            set doSetDownloadFolder to true
        end if
        
        -- Check if Private Browsing's already enabled, and set it if we need to
        if is_pb_enabled() = false then set_pb(true)
        
        -- If the image has been mounted ok, then set Safari's download location
        if doSetDownloadFolder is true then set_download_folder("/Volumes/" & PrivateVolume)
    end if
    
    
    -- User wants to disable Private Browsing
    if clickedButton is "Disable" then
        -- If Private Browsing's on, turn it off
        if is_pb_enabled() = true then set_pb(false)
        
        -- Set Safari's download folder to the normal location
        set_download_folder(DownloadFolder)
        
        -- If the private image is mounted, unmount it
        if is_disk_mounted(PrivateVolume) then
            unmount_disk(PrivateVolume)
        end if
    end if
    
end run


-- Some boiler-plate code to make sure GUI scripting's available and enabled on this system.
on check_requirements()
    tell application "System Events"
        get system attribute "sysv"
        if result is greater than or equal to 4144 then -- Mac OS X 10.3.0
            if UI elements enabled is false then
                beep
                display dialog "GUI Scripting is not enabled" & return & return & "Open System Preferences and check Enable Access for Assistive Devices in the Universal Access preference pane, then run this script again." with icon stop
                if button returned of result is "OK" then
                    tell application "System Preferences"
                        activate
                        set current pane to pane "com.apple.preference.universalaccess"
                    end tell
                else
                    error number -128
                end if
            end if
        else
            beep
            display dialog "This computer cannot run this script" & return & return & "The script uses GUI Scripting technology, which requires an upgrade to Mac OS X 10.3 Panther or newer." with icon caution buttons {"Quit"} default button "Quit"
            error number -128
        end if
    end tell
end check_requirements


-- Returns TRUE if Private Browsing is currently enabled, otherwise returns FALSE
on is_pb_enabled()
    activate application "Safari"
    tell application "System Events" to tell application process "Safari"
        set check_value to value of attribute "AXMenuItemMarkChar" of menu item "Private Browsing" of menu 1 of menu bar item "Safari" of menu bar 1
        return ((check_value as string) = "3") as boolean
    end tell
end is_pb_enabled


-- Sets Private Browsing (On if State=TRUE, Off if State=FALSE)
-- Also automatically dismisses the information window Safari shows when enabling Private Browsing
on set_pb(state)
    activate application "Safari"
    tell application "System Events" to tell application process "Safari"
        if state is true then
            click menu item "Private Browsing" of menu 1 of menu bar item "Safari" of menu bar 1
            click button "OK" of window 1
        else
            click menu item "Private Browsing" of menu 1 of menu bar item "Safari" of menu bar 1
        end if
    end tell
end set_pb


-- Sets Safari's download location
-- See comment below if the script fails during the setting of the download folder
on set_download_folder(dlFolder)
    activate application "Safari"
    tell application "System Events" to tell application process "Safari"
        
        click menu item "Preferences?" of menu 1 of menu bar item "Safari" of menu bar 1
        click button "General" of tool bar 1 of window 1
        tell pop up button 3 of group 1 of group 1 of window "General"
            click
            click menu item "Other?" of menu 1
        end tell
        
        keystroke "g" using {shift down, command down}
        delay 2 -- NOTE:  This value should be increased if the new download location doesn't appear in the Go To Folder dialog
        
        tell window "Go To Folder"
            set value of text field 1 to dlFolder
            click button "Go"
        end tell
        click button "Select" of sheet 1 of window "General"
        click button 1 of window "General"
    end tell
end set_download_folder


-- Mounts a disk image
on mount_disk(imageFile)
    try
        do shell script "hdiutil attach " & imageFile
    on error number 1
        return false
    end try
    return true
end mount_disk


-- Returns TRUE if the volume whose name matches diskName is currently mounted, FALSE otherwise.
on is_disk_mounted(diskName)
    tell application "Finder"
        set diskMounted to (exists disk (diskName))
    end tell
    return diskMounted
end is_disk_mounted


-- Ejects the volume whose name matches diskName.
on unmount_disk(diskName)
    tell application "Finder" to eject the disk diskName
end unmount_disk