Sunday, July 5, 2020

#1 2020-05-12 01:35:59 am

wladdy
Member
Registered: 2012-03-02
Posts: 27

GNU SED behaves differently in AppleScript and in Terminal

I need a quick and robust way in AppleScript to turn a string into lowercase, taking into account unicode accented characters.

Running MacOS Catalina 10.15.4, I installed gnu-sed in order to have more flexibility manipulating text strings.

In Terminal, the following command turns 'ÉTÉS' into 'Étés'.

echo ÉTÉS | gsed -E 's_^(.)(.*)$_\U\1\L\2_'

However in AppleScript, the same command returns 'ÉtÉs'. The second 'É' did not switch case.

Applescript:

set cmd to "echo ÉTÉS | /usr/local/Cellar/gnu-sed/4.8/bin/gsed -E 's_^(.)(.*)$_\\U\\1\\L\\2_'"
set str2 to do shell script cmd

No matter if going from LC to UC or the reverse, all accented characters seem to keep their original case when the command is ran in AppleScript whereas they change case when the command is ran on Terminal.

Any help to understand and solve this issue would be greatly appreciated. Thanks in advance. W.

PS: another irritation is that AppleScript won't take simply 'gsed', like Terminal does, but requires the full address to the command. I can live with that if I must, but should there be a better way I am all ears...


Filed under: sed, unicode, catalina, GNU

Offline

 

#2 2020-05-12 02:03:26 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5233

Re: GNU SED behaves differently in AppleScript and in Terminal

Hi.

I can't help with GNU sed, but you can do this with ASObjC, which is probably a tad faster:

Applescript:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"

set str to "ÉTÉS"
set str2 to ((current application's class "NSString"'s stringWithString:(str))'s capitalizedString()) as text


NG

Offline

 

#3 2020-05-12 08:17:37 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 532

Re: GNU SED behaves differently in AppleScript and in Terminal

wladdy wrote:

another irritation is that AppleScript won't take simply 'gsed', like Terminal does, but requires the full address to the command. I can live with that if I must, but should there be a better way I am all ears...



I don't know a better way to handle this but, FWIW, Apple's explanation is:

My command works fine in Terminal, but when I try to use it in do shell script, I get an error about “command not found.” What’s going on?

There are two possibilities. First, do shell script always uses /bin/sh to interpret your command, not your default shell, which Terminal uses. (To find out what your default shell is, say echo $SHELL in Terminal.) While some commands are the same between shells, others are not, and you may have used one of them. If you write your do shell script scripts in Terminal first, always use sh. You can start sh by typing /bin/sh; type exit to get back to your normal shell.

Second, when you use just a command name instead of a complete path, the shell uses a list of directories (known as your PATH) to try and find the complete path to the command. For security and portability reasons, do shell script ignores the configuration files that an interactive shell would read, so you don’t get the customizations you would have in Terminal. Use the full path to the command, for example, /sbin/ifconfig instead of just ifconfig. To find the full path in Terminal, say which command-name, for example, which ifconfig; to see the list of places do shell script will search, say do shell script "echo $PATH".

(This answer glosses over a few details — see Gory Details if you care.)


https://developer.apple.com/library/arc … CH1-TNTAG1

Last edited by peavine (2020-05-12 09:22:17 am)


2018 Mac mini - macOS Catalina

Offline

 

#4 2020-05-12 01:34:45 pm

wladdy
Member
Registered: 2012-03-02
Posts: 27

Re: GNU SED behaves differently in AppleScript and in Terminal

Thank you very much. Both your answers open interesting paths.

Nigel, can you recommend a good starting point to learn ASObjC?

I am not a programmer and I am not interested in writing full applications, be they in Swift or in C. I don't fully understand what are Cocoa and Xcode.
I am just familiar with AS and with shell scripting. My goals is to understand macOS better in order to automate my workflows with more power and flexibility.

Shane Stanley wrote what appears to be the only book on ASObjC but it dates to El Capitan (>4y) and I fear that most of the examples and many of the concepts will be obsolete by now.

I'd be interested in anything: books, tutorials, online courses.

Thanks again for your help so far, and any more tips if you have the time. W.


Filed under: shell;asobjc

Offline

 

#5 2020-05-12 02:55:34 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5233

Re: GNU SED behaves differently in AppleScript and in Terminal

wladdy wrote:

Shane Stanley wrote what appears to be the only book on ASObjC but it dates to El Capitan (>4y) and I fear that most of the examples and many of the concepts will be obsolete by now.


Not at all. It covers the basics and the "must know" stuff, all of which (I think) is still relevant. The few things it doesn't cover (one or two frameworks and classes) you can add to your knowledge later if you need them, either by doing your own research in the developer documentation (which Shane's book will empower you to do) or by asking here.  smile


NG

Offline

 

#6 2020-05-14 02:33:23 pm

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

Re: GNU SED behaves differently in AppleScript and in Terminal

wladdy wrote:

However in AppleScript, the same command returns 'ÉtÉs'. The second 'É' did not switch case.

Applescript:

set cmd to "echo ÉTÉS | /usr/local/Cellar/gnu-sed/4.8/bin/gsed -E 's_^(.)(.*)$_\\U\\1\\L\\2_'"
set str2 to do shell script cmd


When installing gnu sed on Mac, the shortcut to gsed executable is created in the /usr/local/bin location. So, you can write script shortly:

Applescript:


set cmd to "echo ÉTÉS | /usr/local/bin/gsed -E 's_^(.)(.*)$_\\U\\1\\L\\2_'"
set str2 to do shell script cmd

But I drew attention to the original script not for the reason that it produces an incorrect result on my Mac, but because it produces an empty string as a result.

And with other starts of the original code, I generally get this error:

error "/usr/local/Cellar/gnu-sed/4.8/bin/gsed: line 1: syntax error near unexpected token `('
/usr/local/Cellar/gnu-sed/4.8/bin/gsed: line 1: `ÉTÉS -E s_^(.)(.*)$_\\U\\1\\L\\2_ ÉTÉS'" number 2


Can someone explain to me why this is happening? This oddity may be the key to unraveling the behavior of gnu sed with regex expressions.

Last edited by KniazidisR (2020-05-14 02:45:36 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2020-05-14 04:18:55 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 949

Re: GNU SED behaves differently in AppleScript and in Terminal

If you're going to use a shell command to change case, I would use TR, as it has a ready-made function for it.

Applescript:

do shell script "echo ÉTÉS | LANG='fr_FR.UTF-8' tr [:upper:] [:lower:] "

Offline

 

#8 2020-05-15 04:46:34 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 279

Re: GNU SED behaves differently in AppleScript and in Terminal

Its little confusing what OP want, first he say he like to have lowercase
and later he say he want capitalizedString.

I do not think I will beat Nigels version but...

but this is my python version.

Applescript:

do shell script "python -c \"print('ÉTÉS'.capitalize())\""
do shell script "python -c \"print('bonjour'.capitalize())\""
do shell script "python -c \"print('viva la France'.capitalize())\""
do shell script "python -c \"print('bon Appetit'.capitalize())\""

Applescript:

set aString to "bon Appetit!"

do shell script "python -c \"print('" & aString & "'.capitalize())\""

Last edited by Fredrik71 (2020-05-15 04:55:11 am)


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#9 2020-05-15 05:55:08 am

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

Re: GNU SED behaves differently in AppleScript and in Terminal

Nigel posted one of the settings available.
Below is the complete set as coded by Shane Stanley.

Applescript:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"

on changeCaseOfText(sourceText, caseIndicator)
   -- create a Cocoa string from the passed text, by calling the NSString class method stringWithString:
   set sourceString to current application's NSString's stringWithString:sourceText
   -- apply the indicated transformation to the Cocoa string
   if caseIndicator is 0 then
       set adjustedString to sourceString's uppercaseString()
   else if caseIndicator is 1 then
       set adjustedString to sourceString's lowercaseString()
   else
       set adjustedString to sourceString's capitalizedString()
   end if
   -- convert from Cocoa string to AppleScript string
   return (adjustedString as string)
end changeCaseOfText

my changeCaseOfText("Please enter no more than 4 comma-delineated tags.", -1) -- may be 0, 1 or any other value

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 15 mai 2020 13:55:02

Last edited by Yvan Koenig (2020-05-15 05:56:59 am)

Offline

 

#10 2020-05-16 06:57:05 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 279

Re: GNU SED behaves differently in AppleScript and in Terminal

Here is a version that use JavaScriptCore Framework command line jsc.
If you like to run simple JavaScript in command-line.

This code only change the first character in the string.

Applescript:

set aString to "bon appetit"
set jsString to "\"const name = " & quoted form of aString & "; print (name[0].toUpperCase() + name.slice(1))\""

-- Use command-line version of JavaScriptCore, you could find it from JavaScriptCore Framework
do shell script "/usr/local/bin/jsc -e " & jsString


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)