default browser's download folder

I want a script function that discovers the default browser, and it’s download folder. I’ve figured out how to find the default browser with user defaults and launch services… does any one know the user defaults keys that will get me Safari’s download folder… also how would I go about finding Firefox’s or IE’s download folder?

i’m kind of trying to do the same thing.

for safari, i tried:

set df to (do shell script “defaults read com.apple.Safari NSNavLastRootDirectory”)

strangely, the result is the PARENT folder of the download folder set, which is called “downloads”.

maybe there is another preference plist key that stores the download location

Hello all -

I resurrect this thread because I am trying to do the same thing – namely, to get the path to the currently specified Safari download folder.

Looking in com.apple.safari.plist, there is only the “last” location. This does not change when I change the preference in Safari, so that isn’t it.

Moving on, there is a com.apple.internetconfigpriv.plist and a com.apple.internetconfig.plist, both of which contain an element called “DownloadFolder”.

However, the value for this key is pure hexadecimal.

For example, with the Download Folder set to the Desktop, this value is


which is hex for

8GrumpyÀÏ{H+Ã¥dDesktopåÉÀ0oùÿÿÿÿ holbrookÀËÀ0¶IÃ¥dÈ1Grumpy:Users:holbrook:DesktopDesktopGrumpyUsers/holbrook/Desktop/ÿÿ

The path “Grumpy/Users/holbrook/Desktop” is the actual path to the desktop. The actual path does not even occur in this hex data!!

Does anyone know

  1. How to parse this data, or what its format is? I think it is a Cocoa dictionary, and could use Call Method to get it, I guess, but it’s hard to believe there isn’t a text path somewhere.
  2. Anywhere else that the current default download folder path might be found?

Thanks

This works on my Tiger machine. I don’t think System Events’s “Property List Suite” existed before that.

set plPath to (path to preferences as Unicode text) & "com.apple.internetconfigpriv.plist"
tell application "System Events"
	set aliasData to value of property list item "DownloadFolder" of property list file plPath
end tell

-- Save the result (which is raw data, not text) to a temporary file and read it back as an alias.
set fRef to (open for access file ((path to temporary items as Unicode text) & "Download Folder Alias.dat") with write permission)
try
	set eof fRef to 0
	write aliasData to fRef
	set downloadFolder to (read fRef from 1 as alias)
end try
close access fRef

downloadFolder --> The required alias.

Awesome, Nigel.

A stroke of genius.

Thanks muchly.

Johnny

This version works on both my Jaguar and Tiger machines:

on getDownloadFolder()
	considering case
		-- Read the appropriate internetconfig.plist file in 'defaults read' format.
		try
			set defaultsRead to (do shell script "defaults read com.apple.internetconfigpriv")
			if (defaultsRead does not contain "DownloadFolder") then error
		on error
			set defaultsRead to (do shell script "defaults read com.apple.internetconfig")
			if (defaultsRead does not contain "DownloadFolder") then
				tell application (path to frontmost application as Unicode text)
					display dialog "Default download folder not found." buttons {"OK"} default button 1 with icon stop
				end tell
				error number -128
			end if
		end try
		
		-- Parse the (Unicode) text for the hexadecimal representation of the download folder alias.
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "DownloadFolder" as Unicode text
		set dfHex to text from text item 2 to end of defaultsRead
		set AppleScript's text item delimiters to ">;" as Unicode text
		set dfHex to text item 1 of dfHex
		set AppleScript's text item delimiters to "" as Unicode text
		set dfHex to (dfHex's words) as Unicode text
		set AppleScript's text item delimiters to "0000000001" as Unicode text
		set dfHex to text ((count text item 1 of dfHex) + 1) thru end of dfHex
		set AppleScript's text item delimiters to astid
	end considering
	
	-- Open a temporary file.
	set fRef to (open for access file ((path to temporary items as Unicode text) & "Download Folder Alias.dat") with write permission)
	try
		set eof fRef to 0
		
		-- In groups of 240 characters or less, convert the hexadecimal text to binary data and
		-- save to the file. (240 is about the maximum that can be handled by 'run script'.)
		set c to (count dfHex)
		set j to c mod 240
		write (run script "«data ****" & (text 1 thru j of dfHex) & "»") to fRef
		repeat with i from j + 1 to c by 240
			set j to i + 239
			write (run script "«data ****" & (text i thru j of dfHex) & "»") to fRef
		end repeat
		
		-- Read the data back as an alias.
		set downloadFolder to (read fRef from 1 as alias)
	end try
	close access fRef
	
	return downloadFolder
end getDownloadFolder

getDownloadFolder()

Making the text item delimiters Unicode text too makes a hell of a lot of difference to the speed in Jaguar!