The command "grep -c mystring" will count how many occurrences of mystring are in the standard input. The command "grep -c mystring file file2" should give you the counts in each file. Using a little more text processing, those two results can be added together to get your result. Is that enough to get you started?
The command "cat file1 file2 | grep -c mystring" should get you what you want. Just make sure you use the full pathnames for the files.
Here is the code you probably want: (however your words were to do nothing if the value=1, so that's what my code does. You probably misspoke.)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property author : "@CRLF"
--Import KM variables HERE:
set kminstance to system attribute "KMINSTANCE"
tell application id "com.stairways.keyboardmaestro.engine"
set path1 to getvariable "localFilePath1" instance kminstance
set path2 to getvariable "localFilePath2" instance kminstance
set theSearchString to getvariable "localSearchString" instance kminstance
end tell
--This block is for debugging in Script Editor. It does nothing when run in Execute an AppleScript action
if name of current application is not "osascript" then
set path1 to "test document1.txt"
set path2 to "test document2.txt"
set theSearchString to ¬
"your search string"
end if
set contents1 to contentsOfFileAtPath(path1)
set Contents2 to contentsOfFileAtPath(path2)
--The Definition of "count found in" from KM Engine's Scripting Dictionary
(*
count found in
count found in (verb)Search a string for a string, returning the number of matches (from Keyboard Maestro Engine Suite)
FUNCTION SYNTAX
set theResult to count found in text ¬
for text ¬
regex boolean ¬
case sensitive boolean ¬
process tokens boolean
RESULT
integerthe number of matches.
*)
set {count1, count2} to {0, 0}
tell application id "com.stairways.keyboardmaestro.engine"
set count1 to count found in contents1 ¬
for theSearchString without regex
set count2 to count found in Contents2 ¬
for theSearchString without regex
end tell
--OUTPUT THE SUM
return count1 + count2
on contentsOfFileAtPath(thePath)
set {theContents, theError} to current application's NSString's stringWithContentsOfFile:thePath usedEncoding:(current application's NSUTF8StringEncoding) |error|:(reference)
if theError is not missing value then error theError's localizedDescription as text
return theContents as text
end contentsOfFileAtPath
To debug just paste it into Script Editor and change the variables in the debug block
@Airy Thank you! I modified the shell script a little bit to this:
cat file_1 file_2 | grep -c "$(pbpaste)"
While every time I try the script in iTerm with my three hypotheses (no, one, two occurrences) everything is fine, I get weird results when I use the same script in Keyboard Maestro.
With no occurrence, I get an error Task failed. Macro cancelled...
With one occurrence, I get no notification.
With two occurrences, I get notified a message telling me the string was written one time.
Tried to run the script alone in Keyboard Maestro. The value of VarName changes with one or two occurrences. Not when there is no occurence.
Not weird at all. That's what you told me to do. I advised you that you probably misspoke and you probably meant "=1". So just change that in my Switch action.
I tried your script after adapting the file paths. Got the following errors: one about read file. The other about the JavaScript for automation script. I unfortunately can't say more these two errors.
For fun, here's the no-scripting-required "easy mode" version. All it does is "For Each" for a substring matching the System Clipboard contents "For Each" file in a list, adding 1 to a counter for each match: