Saturday, January 19, 2019

#1 2019-01-06 04:39:29 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 596
Website

Find replace query having emojis

Find and replace seems to hang or ignore requests if there are special characters (emojis) involved.

Applescript:

set T to "????年底巨惠????????????
999 祝你过得愉快"

set fin to 999
set rep to 15

set res to my find_rep(T, fin, rep)

on find_rep(T, fin, rep)
   #considering case, diacriticals, hyphens, punctuation and white space
       set tid to text item delimiters
       set text item delimiters to fin
       set T to text items of T
       set text item delimiters to rep
       set T to T as string
       set text item delimiters to tid
   #end considering
   return T
end find_rep

Offline

 

#2 2019-01-06 04:57:56 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5516

Re: Find replace query having emojis

The problem stems from the fact that you are setting text item delimiters to integers, not strings. Change your script like this:

Applescript:

set fin to "999"
set rep to "15"


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2019-01-06 09:11:35 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 596
Website

Re: Find replace query having emojis

Excellent, you were right:)
Using code in Automator changes always a bit... Is there a way to discern integers from reals? 
I'd need just parsed reals, (or not rounded numbers ) from the input text

Applescript:

set input to "????2.3 年底巨惠????????????\n999 祝你过得愉快"
#target number: 999
#change target number to 15

set allW to (get words of input)
set numLs to {}
repeat with a in allW
   try
       set a to (a as number) #Automator service plugins aren't able to discern numbers from reals and integers
       copy a to end of numLs
   end try
end repeat
if (get length of numLs) = 1 then
   set trgNr to (numLs as text)
else
   set trgNr to ""
end if
set res to my find_rep(input, trgNr, "15")

on find_rep(T, fin, rep)
   set tid to text item delimiters
   set text item delimiters to fin
   set T to text items of T
   set text item delimiters to rep
   set T to T as string
   set text item delimiters to tid
   return T
end find_rep

Last edited by Joy (2019-01-06 09:34:20 am)

Offline

 

#4 2019-01-06 10:42:36 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11539
Website

Re: Find replace query having emojis

An more versatile alternative is the Foundation method

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target 
                                        withString:(NSString *)replacement 
                                           options:(NSStringCompareOptions)options 
                                             range:(NSRange)searchRange;

because it allows also regular expression search.

Applescript:

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

property |⌘| : a reference to current application

on foundationReplace(theText, searchPattern, replaceText, usingRegularExpression)
   local searchOptions
   set cocoaString to |⌘|'s NSString's stringWithString:theText
   set searchOptions to |⌘|'s NSCaseInsensitiveSearch
   if usingRegularExpression then
       set searchOptions to searchOptions + (|⌘|'s NSRegularExpressionSearch as integer)
   end if
   return (cocoaString's stringByReplacingOccurrencesOfString:searchPattern withString:replaceText options:searchOptions range:{location:0, |length|:count theText}) as text
end foundationReplace

You can replace literal text

Applescript:

set input to "????2.3 年底巨惠????????????
999 祝你过得愉快"


set rep to "15"
set theResult to foundationReplace(input, "2.3", rep, false)

or a regular expression pattern

Applescript:

set input to "????2.3 年底巨惠????????????
999 祝你过得愉快"


set rep to "15"
set theResult to foundationReplace(input, "\\d+\\.\\d+", rep, true)

Last edited by StefanK (2019-01-06 05:27:15 pm)


regards

Stefan

Offline

 

#5 2019-01-06 05:11:30 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5516

Re: Find replace query having emojis

StefanK wrote:

An more versatile alternative



But you should probably default to NSCaseInsensitiveSearch if you want to replace the standard AS equivalent.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2019-01-06 05:27:51 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11539
Website

Re: Find replace query having emojis

Good point, Shane, thanks, I edited the post


regards

Stefan

Offline

 

#7 2019-01-06 10:13:40 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5516

Re: Find replace query having emojis

Joy wrote:

Is there a way to discern integers from reals? 
I'd need just parsed reals, (or not rounded numbers ) from the input text



You could use a regular expression, or a scanner like this:

Applescript:

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

set theScanner to current application's NSScanner's scannerWithString:"????2.3 年底巨惠????????????
999 祝你过得愉快"

set set1 to current application's NSCharacterSet's characterSetWithCharactersInString:"0123456789-+"
set set2 to current application's NSCharacterSet's characterSetWithCharactersInString:"0123456789-+."
set integerStrings to {}
repeat
   set theResult to theScanner's scanUpToCharactersFromSet:set1 intoString:(missing value)
   set {theResult, theNum} to theScanner's scanCharactersFromSet:set2 intoString:(reference)
   if theResult then
       if not (theNum's containsString:".") then set end of integerStrings to theNum as text
   else
       exit repeat
   end if
end repeat
return integerStrings

Last edited by Shane Stanley (2019-01-06 11:50:14 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)