Shell script Safari defaults for Catalina

I'm looking to change the default location for Safari's LastRootDirectory preferece (the default folder path that is used when opening a window in Safari to select a file). This is on an older machine with Catalina and Safari 15.6

The following works directly in the Terminal, and also from a do shell script command in Applescript:

defaults write com.apple.Safari NSNavLastRootDirectory /Volumes/DATA/upload

But it does not work correctly when run from KM.

The preference file that is being targetted (and reflects the changes when run from the terminal or Applescript) in Catalina is here:

~/Library/Containers/com.apple.Safari/Data/Library/Preferences/com.apple.Safari.plist

When run from KM it does not touch that file but instead creates a new com.apple.Safari.plist in the ~Library/Preferences folder instead.

So I've tried targetting the file by using the full path, and variations of it, in KM but it always reports a Could Not Write Domain error. For example:

/usr/bin/defaults write ~/Library/Containers/com.apple.Safari/Data/Library/Preferences/com.apple.Safari.plist NSNavLastRootDirectory /Volumes/DATA/upload

I've tried reading through as many of the other Shell Script questions as possible for any clues but I can't really get my head around what it is I need to change for it to be able to run from KM.

Any help greatly appreciated.

This is probably related to Safari being a sandboxed app, but I thought that would break the shell and AppleScript solutions, too—it's my (possibly wrong) understanding that only the owning app can make changes within its container. But your experiment seems to show otherwise ... perhaps give Keyboard Maestro full disk access and see what happens?

Or what happens if you put the AppleScript command into a Keyboard Maestro macro? Does that work, or does it fail just like the shell script?

-rob.

This.

You should see an unchecked entry for "Keyboard Maestro" in System Preferences -> Security & Privacy -> Full Disk Access, added on the first time your macro ran. Check that, quit and relaunch KM, and everything should work (you don't need to quit the KM Engine.)

Same issue happens when running the as an Applescript from KM

Already had full disk access, so unfortunately it's not that.

Have another try, using:

defaults write com.apple.Safari NSNavLastRootDirectory /Volumes/DATA/upload

Tested with macOS 10.15.7, Safari 15.6.1:

...which you can see worked.

If it doesn't for you, try unchecking KM's FDA, quitting and relaunching both Editor and Engine, checking FDA, quit-and-relaunch both again in an attempt to reset the tcc entry.

Thanks for the suggestions, none worked but it did give me an idea, one so simple and so basic it resulted in much swearing, hair pulling and face slapping on my part when it worked.

In the end the solution was.... RESTART.

Yes, I forgot the golden rule. Shame on me :roll_eyes:

3 Likes