It is Past Time to Convert AppleScript That Use Satimage.osax
You may run across some AppleScripts in this Forum that were published prior to release of macOS Mojave that made good use of the Satimage.osax Scripting Addition for AppleScript.
Satimage was one of the most popular and power AppleScript tools. Today, in 2019, it is no longer advisable to use Satimage because:
It won't run in Mojave+
It is 32-bit, which won't run in Catalina+
No updates are expected at this time.
One of the most powerful features of Satimage is RegEx (Regular Expressions). Today, the best RegEx tool is a great Script Library written by world-renown Shane Stanley. You can get it at RegexAndStuffLib at MacOSXautomation.com .
If you need help converting any scripts that used Satimage, let us know. Please post a new topic for each request.
Great KM RegEx Tools
Of course, don't forget that KM has some great Regular Expression tools (Actions), so you may not need any script.
Also note that Keyboard Maestro Engine’s AppleScript dictionary includes support for doing regex search and replace as well as exposing other Keyboard Maestro facilities like calculations and tokens.
tell application "Keyboard Maestro Engine"
search "3+4" for "(\\d+)" replace "%CalculateFormat%CALCULATE(\\1)%Currency%" with regex and process tokens
end tell
How to search regex match without replacing but saving only the search match to a variable?
Is there a way to do something like this using applescript?
tell application "Keyboard Maestro Engine"
set SearchResult to search "30 USD" for "(\\d+)"
end tell
NOTE – the difference between the two searches and their different pattern modifier switches.
set dataStr to "
30 USD
40 GBP
"
tell application "Keyboard Maestro Engine"
set searchResult01 to search dataStr for "(?ms).*?(\\d+).*" replace "$1" with regex
set searchResult02 to search dataStr for "(?-ms).*?(\\d+).*" replace "$1" with regex
end tell
(*
set theResult to search text ¬
for text ¬
replace text ¬
regex boolean ¬
case sensitive boolean ¬
process tokens boolean
*)
Unfortunately @peternlewis hasn't extended Keyboard Maestro's regular search to AppleScript as he has search/replace.
The search that is available ONLY returns a count of the items found – it does NOT return text...
See the command:
count found in
Please do make a feature request, so I'm not the only one who has.
NOTE – you don't have to give up the Satimage.osax for some time to come if you don't want to.
Mark Alldritt's mod and Rosetta probably give it another 3-5 years of life.
Nevertheless it is definitely time to think about replacing it.
on regex search once theString search pattern thePattern match case matchCase : false dot matches all dotMatchesAll : false anchors match lines anchorsMatchLines : true Unicode boundaries unicodeBoundaries : false replace template replaceTemplate : (missing value) capture groups captureGroup : 0 backwards search fromBack : false
set theOpts to (not matchCase) as integer
if dotMatchesAll then set theOpts to theOpts + 8
if anchorsMatchLines then set theOpts to theOpts + 16
if unicodeBoundaries then set theOpts to theOpts + 64
set {theRegex, theError} to NSRegularExpression's regularExpressionWithPattern:thePattern options:theOpts |error|:(reference)
if theRegex = missing value then error theError's localizedDescription() as text
set theString to NSString's stringWithString:theString
if replaceTemplate is missing value and class of captureGroup is integer then set replaceTemplate to "$" & captureGroup -- make template of capture group
if not fromBack then
set theMatch to theRegex's firstMatchInString:theString options:0 range:{0, theString's |length|()}
if theMatch is missing value then return missing value
return my resultFrom:theString captureGroups:captureGroup fromMatch:theMatch replaceTemplate:replaceTemplate regularExpression:theRegex
else
set theMatches to theRegex's matchesInString:theString options:0 range:{0, theString's |length|()}
if theMatches's |count|() = 0 then return missing value
set theMatch to theMatches's lastObject()
return my resultFrom:theString captureGroups:captureGroup fromMatch:theMatch replaceTemplate:replaceTemplate regularExpression:theRegex
end if
end regex search once
thanks for sharing that workaround, Peter. I thought for a moment you had a complete solution.
However, If there is more than one match, the result concatinates them:
tell application "Keyboard Maestro Engine"
set s to search "30 USD 20 second group" for "\\D" replace "" with regex
end tell
-->3020
This is NOT what we would expect, and really not useful.
We need a KME search command that returns a list of matches.
In fact, we need the result to be a list of lists:
One item for each match
Sub-items for each match:
Entire match
One item for each Capture Group
Here is an example using both the KM Engine, and a RegEx Script Lib:
Note that I have changed the RegEx pattern to be matched.
use AppleScript version "2.5"
use scripting additions
use framework "Foundation" -- Required for all ASObjC commands
--- REQUIRED SCRIPT LIBRARIES ---
use script "RegexAndStuffLib" -- by Shane Stanley
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set sourceStr to "30 USD 20 second group"
set reFind to "\\d+ (\\w+)"
-- Use KM Engine --
tell application "Keyboard Maestro Engine"
set matchStr to search sourceStr for reFind replace "" with regex
end tell
-->" group"
-- USE RegEx Script Library --
set matchList to regex search sourceStr ¬
search pattern reFind ¬
match case false ¬
dot matches all false ¬
anchors match lines false ¬
Unicode boundaries false ¬
capture groups {}
-->{{"30 USD", "USD"}, {"20 second", "second"}}
The results returned by the RegexAndStuffLib script lib is what would be most useful.