Change download folder of Safari and access a URL

Hello All I have been trying to get a script for a scenario where i need to setdownload path to a mounted Volume and then access a url from Safari. I got bits a pieces of code from the forums but i seem to be getting a error.

set downloadsFolder to quoted form of POSIX path of “HELLO:”

set url_item to {“https://www.learningcontainer.com/download/sample-text-file/?wpdmdl=1669&refresh=63b94aeb182c41673087723”}

tell application “Safari” to quit

tell application “System Events”

repeat while application process “Safari” exists

delay 0.1

end repeat

end tell

do shell script ("defaults write com.apple.Safari DownloadsPath " & downloadsFolder)

tell application “Safari”

activate

set current tab to make new tab with properties {URL:url_item}

end tell

I get a error called error “Safari got an error: Can’t make or move that element into that container.” number -10024.

Please help

Two things:

`current tab` is a property but you're attempting to use it as a variable.

Second, you probably have to position the new tab somewhere using at.

Try:

set xy to make new tab at window 1 with properties {URL:url_item}

Something like the following will be returned:

--> tab 3 of window id 5683 of application "Safari"

And you can then use xy to refer to that tab, e.g.

name of xy

--> "Untitled"
1 Like

Thank you so much one other query though. I see the url getting navigated and file downloaded but file Doesnt get downloaded to the path.
The Path that I set is the a external mounted Volume

I’m not sure that you can set that (outside of Safari’s preferences). When I run defaults read com.apple.Safari, nothing like that is listed. I’m running Sierra so it’s very possible that apple has added and removed some of the defaults.

However, if your version does have such a default to change, then typically it doesn’t happen instantly. You might have to restart safari or kill its process first. I’m not sure when Safari reads its defaults.

Absent that, you might be able to just move the file there once it’s downloaded.

set url_item to {"https://www.learningcontainer.com/download/sample-text-file/?wpdmdl=1669&refresh=63b94aeb182c416730877"}
tell application "Finder"
	set df to (path to downloads folder)
	set fdfBef to files of df as alias list
end tell
tell application "Safari"
	set xy to make new tab at window 1 with properties {URL:url_item}
end tell

tell application "Finder"
	delay 4
	set fdfAft to files of df as alias list
	
	repeat with x in fdfAft
		if x is not in fdfBef then
			set label index of x to 4
			-- or…
			move x to desktop
		end if
	end repeat
end tell

Of course, if you add other files to the downloads folder by other means, they would get caught up in this too.