2
votes

I am new to AppleScript and would like to know how to reveal the default downloads folder in Safari through AppleScript.

--- WHAT I HAVE ALREADY TRIED ---

set filePath to do shell script "defaults read com.apple.Safari DownloadsPath" do shell script "open " & quoted form of filePath

The above script to my understanding will set the variable "filePath" to Safari's default PList entry in preferences. This works great as long as the location is NOT somewhere within the user home folder. If its within a user folder, the Log shows me a "~/" before the path with no reference to anything prior to the user home folder (relative path)

How do i accomplish my goal? is there a way to get the absolute path to the folder? Or perhaps an alternative method?

4
Safari 13 seems to have broken defaults write com.apple.safari DownloadsPath. Any idea what the new preference is?jhfrontz

4 Answers

2
votes

Based on @WilliamTFroggard's answer:

tell application "Finder"
    set folderPath to do shell script "defaults read com.apple.Safari DownloadsPath"
    if folderPath = "~" or folderPath starts with "~/" then ¬
        set folderPath to text 1 thru -2 of POSIX path of (path to home folder) & rest of characters of folderPath
    open POSIX file folderPath as alias
    activate
end tell

The text element is used to strip the trailing / from the home folder (/Users/johndoe/).

The rest property returns every character following the ~ in folderPath (~/Downloads).

/Users/johndoe + /Downloads = /Users/johndoe/Downloads.

1
votes

You can do this by Applescript GUI Scripting to make Applescript click the 'show downloads' option from the 'view' menu:

tell application "Safari"
    activate
end tell
tell application "System Events"
    tell process "Safari"
        tell menu bar 1
            tell menu bar item "view"
                tell menu "View"
                    click menu item "Show downloads"
                end tell
            end tell
        end tell
    end tell
end tell
1
votes

Here's a method using Python:

do shell script "python -c \"import os; print os.path.expanduser('`defaults read com.apple.Safari DownloadsPath`')\""

If you really want the long AppleScript method, try this:

set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
    set item 1 of filePathWords to (POSIX path of (path to home folder as string))
    set filePath to filePathWords as string
else
    set filePath to "/" & filePathWords as string
end if

If that second "/" in the path bothers you (it sort of bothers me...), you can use this instead:

set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
    set item 1 of filePathWords to (do shell script "cd ~ && pwd")
    set filePath to filePathWords as string
else
    set filePath to "/" & filePathWords as string
end if
1
votes

I think what you want is this:

tell application "Finder"
activate
open ("/Users/yourname/Downloads" as POSIX file)
end tell

Just replace "yourname" with your name in Finder.