Sunday, December 15, 2019

#1 2019-06-14 08:48:16 am

Lilyah
Member
Registered: 2019-03-27
Posts: 4

Change default download folder in Safari

Hi Guys,
I am trying to change default download folder in Safari using defaults command,but it not takes effect when i running it via shell script.

Could you please help me - i need it by any way.
I stuck for choosing the folder using UI elements in apple script also.

Many thanks,
Lilya

Offline

 

#2 2019-06-15 03:30:08 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 802

Re: Change default download folder in Safari

UI Scripting solution:

Applescript:


-- Set alternative downloads folder posix path
set downloadsFolder to POSIX path of (path to desktop folder)
-- copy the download folder path to the clipboard
set the clipboard to downloadsFolder

-- determine the frontmost application to restore when script is done
set frontApp to path to frontmost application as text

activate application "Safari"
tell application "System Events"
   tell application process "Safari"
       
       -- open the preferences
       keystroke "," using {command down}
       repeat until (window 1 exists)
           delay 0.1
       end repeat
       
       -- Change to the General tab
       click menu item 4 of menu 1 of menu bar item "Safari" of menu bar 1
       
       -- Change download location to "Other..."
       click pop up button 2 of group 1 of group 1 of window "General"
       tell pop up button 2 of group 1 of group 1 of window "General"
           click menu 1
           click menu item 4 of menu 1
       end tell
       
       -- Open the "go to folder" dialogue box
       repeat until (exists sheet 1 of window "General")
           delay 0.1
       end repeat
       keystroke "g" using {shift down, command down}
       delay 0.5
       
       -- Paste the folder path into the location field
       tell window "Go To Folder"
           keystroke "v" using {command down}
           delay 0.5
           keystroke return
       end tell
       
       delay 1
       click UI element "Select" of sheet 1 of window "General"
       delay 1
       
       -- close the preferences
       keystroke "w" using {command down}
   end tell
end tell

-- restore frontmost application after job is complete
tell application frontApp to activate

And this reads and modifies Safari's preferences plist, but without affecting Safari itself in Mojave. I don't know why, ask Apple... :

Applescript:


set dFolder to choose folder
set dFolder to "\"" & (POSIX path of dFolder) & "\""

set pathToSafariPlist to (((path to home folder) as string) & "Library:Containers:com.apple.Safari:Data:Library:Preferences:com.apple.Safari.plist") as alias
set posixPathToToSafariPlist to "\"" & POSIX path of pathToSafariPlist & "\""

do shell script ("defaults read " & posixPathToToSafariPlist)
do shell script ("defaults write " & posixPathToToSafariPlist & " NSNavLastRootDirectory " & dFolder)

NOTE: You should give to "Terminal" permission for controlling Mac in "Accessibility" of "Security & Privacy".

Last edited by KniazidisR (2019-06-18 01:04:50 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#3 2019-06-15 09:39:37 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3685

Re: Change default download folder in Safari

Hello KniazidisR

I'm puzzled.
There is no folder com.apple.Safari in the folder Containers of my Home folder.

Applescript:

set path2plist to (path to home folder as text) & "Library:Containers:"
tell application "Finder"
   exists folder path2plist
   log result
end tell

set path2plist to path2plist & "com.apple.Safari:"

tell application "Finder"
   exists folder path2plist
   log result
end tell

The events log is :

Applescript:

tell current application
   path to home folder as text
       --> "SSD 500:Users:**********:"
end tell
tell application "Finder"
   exists folder "SSD 500:Users:**********:Library:Containers:"
       --> true
   (*true*)
   exists folder "SSD 500:Users:**********:Library:Containers:com.apple.Safari:"
       --> false
   (*false*)
end tell

The only folder with this name is :
"SSD 500:Users:**********:Library:WebKit:com.apple.Safari:"

In the Containers folder, the unique folder related to Safari is :
"SSD 500:Users:**********:Library:Containers:com.apple.Safari.CacheDeleteExtension:"

The default Downloads folder is defined in the entry : DownloadsPath
in the file :
"SSD 500:Users:**********:Library:Preferences:com.apple.Safari.plist"

In the same file, the entry NSNavLastRootDirectory points to the Desktop.

Auxiliary comment, in the UI code there is no need for:

Applescript:

set downloadsFolder to POSIX path of (path to desktop folder)
-- copy the download folder path to the clipboard
set the clipboard to downloadsFolder as text

because downloadsFolder, like every POSIX path IS a string

Applescript:

set downloadsFolder to POSIX path of (path to desktop folder)
-- copy the download folder path to the clipboard
set the clipboard to downloadsFolder

is sufficient.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 15 juin 2019  17:39:07

Last edited by Yvan Koenig (2019-06-15 09:45:49 am)

Offline

 

#4 2019-06-15 02:14:14 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 802

Re: Change default download folder in Safari

Yvan Koenig wrote:

Hello KniazidisR

I'm puzzled.
There is no folder com.apple.Safari in the folder Containers of my Home folder.



Maybe damaged. Try reset Safari to defaults.

NOTE: In fact, this plist file is useless, because even after changing the value of the keys, Safari does not read them, and when exiting, it replaces what is changed by the user. The UI scripting example above is working fine.

It is OK only for retrieving information for all current Safari settings

Last edited by KniazidisR (2019-06-15 02:28:58 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#5 2019-06-15 02:35:56 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3685

Re: Change default download folder in Safari

No, it's not damaged.
I have exactly the same items on three different volumes used separately with different versions of the System.

Maybe these items appeared with Mojave which I can't install.


Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 15 juin 2019  22:34:17

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)