defaults ans special characters

Hello,

Writing this in the Script Editor:

do shell script "defaults read com.apple.screencapture name"

It correctly works except with special characters (é è ç à).

For example: Copie d’écran
The previous command displays: Copie d’\351cran

A solution exists?

Thanks.

What is name here? It is some variable you define before do shell script? If that is true, try to quote and to wrap this variable (because keyword name is reserved word of AppleScript):


set |name| to "last-selection"

do shell script "defaults read com.apple.screencapture" & space & quoted form of |name|

Thanks.

name is a key.

set val_screencapturename to "Capture"
defaults write com.apple.screencapture name " & quoted form of val_screencapturename

When I run:

do shell script "defaults read com.apple.screencapture name"

The result is “Capture”

If I replace “Capture” by “Capture d’écran”, when I run defaults read…, the “é” is replaced by \351

If I open the file with Xcode, the name is correct, “Capture d’écran”.
The defaults command doesn’t correctly display the special characters.

Can you run the following as is?


set val_screencapturename to quoted form of "Capture d'écran"

do shell script "defaults read com.apple.screencapture" & space & val_screencapturename

NOTE: in my defaults I don’t see any “Capture” key (I run Catalina), but only

“last-analytics-stamp”
“last-messagetrace-stamp”
“last-selection”
“last-selection-display”

Now I go at work.

It’s a hidden function to change the name of the screenshots. :wink:

set val_screencapturename to "Capture"
do shell script "defaults write com.apple.screencapture name " & quoted form of val_screencapturename
do shell script "defaults read com.apple.screencapture name"

Result: Capture

set val_screencapturename to "Capture d'écran"
do shell script "defaults write com.apple.screencapture name " & quoted form of val_screencapturename
do shell script "defaults read com.apple.screencapture name"

Result: Capture d’\351cran

… I will want: Capture d’écran not Capture d’\351cran

Well, I tested your French expression with do shell script and with Terminal directly. In the both cases the output was interpreted expression and not what you want. So. My conclusion is: the defaults read command (not do shell script command) fails with non UTF-8 characters, and here you can’t do nothing.

Fails only defaults read command. defaults write command works properly
But you can use AsObjC code below to read this key (name) directly from the plist file:


use framework "Foundation"
use scripting additions

set val_screencapturename to "Capture d'écran"

-- WRITE VALUE TO KEY "name"
do shell script "defaults write com.apple.screencapture name " & quoted form of val_screencapturename

-- READ THE CONTENTS AN EXISTING PROPERTY LIST INTO EDITABLE DICTIONARY
set pListFileAlias to alias "HARD_DISK:Users:123:Library:Preferences:com.apple.screencapture.plist"
set pListFilePath to POSIX path of pListFileAlias
set pListFilePath to current application's NSString's stringWithString:pListFilePath
set pListFilePath to pListFilePath's stringByExpandingTildeInPath()
set sourceDictionary to current application's NSMutableDictionary's dictionaryWithContentsOfFile:pListFilePath

-- READ VALUE FOR KEY "name"
set aValue to (sourceDictionary's objectForKey:"name") as text

You’re doing it the hard (and unreliable – it’s what’s in memory, not what’s in the file that counts) way. Try this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set val_screencapturename to "Capture d'écran"
do shell script "defaults write com.apple.screencapture name " & quoted form of val_screencapturename
--do shell script "defaults read com.apple.screencapture name"
set defaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.apple.screencapture"
set theResult to (defaults's valueForKey:"name") as text
--> "Capture décran"

Thanks, Shane. I like when the scripts becomes little and self-sufficient.

Hello,

Thanks a lot! Great! :):cool: