Find and replace seems to hang or ignore requests if there are special characters (emojis) involved.
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
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
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
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
set input to "????2.3 年底巨惠????????????
999 祝你过得愉快"
set rep to "15"
set theResult to foundationReplace(input, "2.3", rep, false)
or a regular expression pattern
set input to "????2.3 年底巨惠????????????
999 祝你过得愉快"
set rep to "15"
set theResult to foundationReplace(input, "\\d+\\.\\d+", rep, true)
You could use a regular expression, or a scanner like this:
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