Automator Find Text and Alert

I am new to Automator and AppleScript. I need to create a simple application where I can drop txt files onto, the app would then search the document for the word "Failed". Then show a dialogue showing the result of the find. Please can someone point me in the right direction! Thank you in advance.

Mac OS X (10.6.7)

Posted on May 20, 2011 1:10 PM

Reply
11 replies

May 20, 2011 3:04 PM in response to andyfromsaint joseph

A real barebones implementation of what you asked for:

on opendroppedFiles

repeat with aFile in the droppedFiles

set result to do shell script "grep Failed " & quoted form of POSIX path of aFile


display dialogresult

end repeat

end open



open this with AppleScript Editor and save as an application. Then drop the txt files on it. There is no error checking or any other niceties. If you post more information we might be able to enhance the script.


BTW these enquires would be best posted to OS X Technologies, that is were AppleScript is normally discussed. I've asked the hosts to move this over there.


regards

May 20, 2011 5:13 PM in response to Frank Caggiano

Wow thank you for help and generosity of the code. What this little script needs to do is read an Apple Battery Diagnostic logs that are formated as txt files,and look for the word "failed". If it finds it move it to a folder named FailedBatteries on my desktop. If it doesn't find the word failed in the log.txt then do nothing with the file and display a dialogue that says something like KGB. I work for a school district and I am tasked with the duty of running battery diagnostics and replacing batteries on about 8,000 macbooks, I will have a team of summer workers armed with the diagnostic on USB drives running the diagnostic on every machine, turning in the generated logs to me! So I am extremely grateful for any assistance!!! In the end I need to end up with a folder full of logs that have failed the battery diagnostic test.

May 20, 2011 6:06 PM in response to andyfromsaint joseph

Did you try the code I posted? Did it find the line with Failed on it?


Do you need to have the complete log file moved to FailedBatteries folder or just the line with the word failed in it? In your first post you seemed to indicate that you wanted the line that Failed was on displayed.


Also could you load a copy of one of the log files so I could take a look at it?


regards

May 20, 2011 7:13 PM in response to Frank Caggiano

First off thank you again for the response. I edited one of the failed battery log files to omitt the word "failed" and replaced with "good" , dragged it onto the copy and pasted script (saved as an application) on the Desktop, it gave the same dialogue "The command exited with a non-zero status". I attempted this with an original Failed log and it gave the same dialog. Eventually I would like to end up with essentially an icon on my desktop that I can drop txt files onto and have that app look for the word failed. If it finds the word failed move the entire file to a folder named FailedBatteries. From that point I can open the files one by one and copy the Battery Validation Code to the Apple GSX website for battery replacement. again I am so greatful for your tme and willingness to help me.



Pasted text of log file:



Tool Notebook Battery and Adapter Diagnostic

Version 3T119

Date 14/10/2009 18:53:13

mlb-serial-number 4H621F37VZ0

rom-version MB11.88Z.0061.B03.0610121324

smc-version 1.4F12

product-code MacBook (13-inch)

serial-number 4H6230BBVTH

max-capacity 0

design-capacity 5020

reserve 200

capacity 0

is-fully-charged FALSE

#charge-cycles 138

design-cycle-count 300

is-cycles-consumed FALSE

manufacturer DP

device-name ASMB013

smart-battery-serial-number DP-ASMB013-34BF

pack-serial-number n/a

amperage 0

voltage 0

is-charger-connected TRUE

is-charging FALSE

cell-count 3

cell-1-voltage 235

cell-2-voltage 1250

cell-3-voltage 2498

error-condition Permanent Battery Failure

status 0x4AD0

batt-temp-sens1 n/a

batt-temp-sens2 n/a

sleep-cap-drain-per-hour n/a

#secs-no-chrg-abs-temp n/a

#secs-no-chrg-grd-temp n/a

#secs-lmt-chrg-abs-temp n/a

#secs-lmt-chrg-grd-temp n/a

#secs-lmt-chrg-blw-temp n/a

max-cell-voltage-imbalance 15

pfstatus 0x4110

firmware-version 0102

hardware-revision 0300

cell-revision 0100

pack-lot-code 0001

ac-adapter-watt 60

ac-adapter-rev 0

ac-adapter-serial 269D4

ac-adapter-fc BA

is-ac-led-controlable FALSE

Battery Serial Number DP-ASMB013-34BF

Percent Full n/a

Charger Connected TRUE (60W 269D4)

Battery Charging FALSE

Percent Battery FCC 0%

Battery Self Status Bad

Battery Not Found Battery found

Result Problem found with a failed battery

Battery Validation Code NBD-75B1770F-02

Warranty Status 2

May 21, 2011 5:45 AM in response to andyfromsaint joseph

Ah the word "Failed" (which is what you said you were looking for in your first message) isn't in the log file. There's Failure and failed (lower case f) but no Failed.


I'll look at this some more in the next day or so. In the meantime if you change the Failed in the code I posted to failed and re-save the script as an application it should display the line with the failed text.


Is it possible that the failure could be from other then a battery? And would you want to handle that differently?

Also it would be possible to pull the Battery Validation code out of the file, that might make your life easier rather than having to go through all the files and manually removing it. It might be possible to automate the whole thing out to the website.


regards

May 21, 2011 4:53 PM in response to andyfromsaint joseph

OK well still here so I guess I might as well finish this 😁


As with the previous script copy the text into the AppleScript editor and save as an application. Let me know how it goes,


on opendroppedFiles

-- Set destination Folder on Desktop

set destDir to (path to desktop) as text

set destDir to alias (destDir & "FailedBatteries:")


-- Set string to search for in log files

set grepString to "failed"


-- For each file dropped on us look for the grepString

-- If found move the file to the destDir

repeat with aFile in the droppedFiles

try

set ret to do shell script "grep -i " & grepString & " " & quoted form of POSIX path of aFile


tell application "Finder"

moveaFiletodestDir

end tell


on error msg number num

if num ≠ 1 then

display dialog "Unknow errror: " & msg & " Number:" & num & " in file:" & aFile

else

display dialog "KGB: " & grepString & " not found in file:" & return & aFile

end if

end try

end repeat

end open



regards

May 23, 2011 5:31 AM in response to Frank Caggiano

I am sorry for my tardy response. This is exactly what I needed. If you still feel like tinkering with it, we could extract the Battery Validation Code and serial-number and place in a text file as such:


serial-number 4H6230BBVTH

Battery Validation Code NBD-75B1770F-02




Again thank you so much for all of your generosity. Also are there any books you can refer me to for learning AppleScript and shell scripting?

May 23, 2011 7:51 AM in response to andyfromsaint joseph

Glad to help.


The two lines with the serial number and validation code would you want them to all go into one file or into separate files and would you still want the complete log file moved into the FailedBateries directory?



For learning AppleScript there are a ton of resources both online and in print. Two books I have found useful are AppleScript the Definitive Guide by O'Reilly and Learn AppleScript by Apress.


Shell scripting is a bit tougher. The standard shell you get in OS X is bash which is based on the Bourne shell so any book covering that should be helpful. Also if you open a terminal window (/Applications/Utilities/Terminal) you can get to the Unix man pages for the different commands and even the shell (in the terminal type man grep for example to see what the grep command does, man bash will give information - lots of it- on the shell)


regards

May 23, 2011 1:08 PM in response to andyfromsaint joseph

Ok this is starting to get a bit unwieldy to post to the list so I also put a copy in my dropbox folder at http://dl.dropbox.com/u/13002668/DropFile.scpt


In addition to what it did before this will save the serial number and validation code to a file 'FailedBatterySerialNumbers.txt' on your Desktop. I added the name of the file that the serial number came from. Figured you might want to go back and reference the original file.


The file looks like:


Untitled 2.txt

serial-number 4H6230BBVTH

Battery Validation Code NBD-75B1770F-02



Untitled 2.txt

serial-number 4H6230BBVTH

Battery Validation Code NBD-75B1770F-02



enjoy, regards



on opendroppedFiles


-- Set destination Folder on Desktop

set destDir to (path to desktop) as text

set destDir to alias (destDir & "FailedBatteries:")



-- Set string to search for in log files

set grepString to "failed"



-- For each file dropped on us look for the grepString


-- If found move the file to the destDir

repeat with aFile in the droppedFiles

try

set ret to do shell script "grep -i " & grepString & " " & quoted form of POSIX path of aFile


tell application "Finder" to moveaFiletodestDir



saveData(aFile)


on error msg number num

if num ≠ 1 then

display dialog "Unknow errror: " & msg & " Number:" & num & " in file:" & aFile

else

display dialog "KGB:" & grepString & " not found in file:" & return & aFile

end if

end try

end repeat

end open


on saveData(aFile)


try

set values to do shell script "grep -E '^serial-number|^Battery Validation Code' " & quoted form of POSIX path of aFile

on error msg number num

if num ≠ 1 then

display dialog "saveData:unknow errror: " & msg & " Number:" & num & " in file:" & aFile

else

display dialog "saveData: string not found in file:" & return & aFile

return

end if

end try


tell application "Finder" to set aFile to (name of aFile)


set fileName to (path to desktop as text) & "FailedBatterySerialNumbers.txt"

try

set fnum to open for accessfileName with write permission

on error msg number num

close accessfilefileName

display dialog "File error:" & msg & " number:" & num

return

end try


set ourEof to get eof of fnum

write (aFile & return & values & return & return) to fnum starting at ourEof + 1

close accessfilefileName


return


end saveData

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator Find Text and Alert

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.