I need my script to cross reference files names with a list. But how?

I need my script to cross reference file names with a list of keywords. If any of the words on the list are present it must pop up a dialog box.

So far, my script will pop up the dialog box when ever a new item is added to safari’s history folder. Which is good, thats what I wanted. But now I need it to cross reference the file names with a blacklist.

How should I go about this?

So far my code looks like this:

on adding folder items to this_file after receiving added_items
	try
		set alert_message to ("Keyword Alert!:") & return & return as Unicode text
		set item_count to count added_items
		if the item_count is greater than 0 then
			set alert_message to alert_message & "Your activity has been logged. "
			set alert_message to alert_message & "     Store Staff have been notified. "
		end if
		set the user_choice to the button returned of (display dialog the alert_message buttons {"Close"} default button 1 with icon 1 giving up after 30)
	end try
end adding folder items to

my list, which only contains three words as this is the test list is;
Hasselhoff
westfield
bootle

though the finished list will have in excess of 200 words.

Dan

Hi Dan,

try this


property KeyWords : {"Hasselhoff", "westfield", "bootle"}

on adding folder items to this_file after receiving added_items
	repeat with oneItem in added_items
		if checkKeyword(name of (info for oneItem)) then
			set alert_message to "Keyword Alert!:" & return & return & "Your activity has been logged" & return & "Store Staff have been notified. "
			display dialog the alert_message buttons {"Close"} default button 1 with icon 1 giving up after 30
		end if
	end repeat
end adding folder items to

on checkKeyword(N)
	repeat with i in KeyWords
		if N contains i then return true
	end repeat
	return false
end checkKeyword


Hi dan, Stefan.
I have posted this as I was working on it anyway…

The script reads a txt file, you can add each keyword to the file one per line.

The script gets the name of the added items.
Because i do not now if the files you are black listing have more than one word to them, the script will break down the name and check each word.

It only displays once if any of the batch of added items are in the black list.
It uses safari to display the dialog, which stops the use of it further until the dialog is ‘Closed’ or timed out
I have added a quit safari line at the end, but commented out for now. ( would need some more work if you wanted to do that)

property File_list : paragraphs of (read alias "Macintosh HD:Users:userNamee:Desktop:foo.txt")
property alert_message : ("Keyword Alert!:") & return & return & "Your activity has been logged. " & return & "     Store Staff have been notified. "

property debug : false
set alert to false

on adding folder items to this_file after receiving added_items
	try
		if debug then say 1
		repeat with i from 1 to number of items in added_items
			if debug then say 2
			tell application "Finder" to set this_item to words of (displayed name of (item i of added_items as alias) as string)
			if debug then say 3
			repeat with z from 1 to number of items in this_item
				if debug then say 4
				set this_item_word to item z of this_item
				if this_item_word is in File_list then
					if debug then say 5
					set alert to true
				end if
			end repeat
		end repeat
		
		if alert then my alert_(alert_message)
		if debug then say 7
		
	end try
end adding folder items to

(* use Safari to display then they can not proceed in safari until they click *)
on alert_(alert_message)
	if debug then say 6
	tell application "Safari"
		
		set the user_choice to the button returned of (display dialog the alert_message buttons {"Close"} default button 1 with icon 1 giving up after 30)
		---tell application "Safari" to quit
	end tell
	
end alert_

*edit * – > By the way what is stopping the user from turning on ‘Private Browsing’

Well, can I first say thank you very much for both your scripts.
I’ve not tried either of them yet though.

This is the situation.
On Friday’s Saturday’s and Sunday’s my shop is full of yobs typing in bad words into google. we have put a filter on, but we can’t keep up with the new words.

For example. I put a key logger on all the machines the other day, and this way we managed to catch a new word. ‘Meatspin’ this is not suitable for a shop.

So now meatspin is both, on the blacklist and in the filter.

The reason I have not put any thought into Private browsing is because they are only in the shop for a few minutes and chances are, they have a PC at home and wont know about private browsing.

Eventually I want the script to do the following.

Constantly watch the folder
Check each newly added file name against the blacklist
Check each newly added file name against the greylist
Display a dialog box when the conditions are met quoting “Keyword Alert! Your activity has been logged. Store staff have been notified.”
Create a log of all keywords that have triggered alerts
Send a message to two checkout machines quoting “Keyword Alert! ‘Meatspin’ White MacBook 3” for example.

Does that sound plausible?

Again, thank you for the time you have spent. It is much appreciated.

Dan

Yes to both of these,

I would have a look at Growl, which is a screen Notification app.
It has an option to send messages over a network to other Macs.

As Above I suggest using Growl to send the message to the other Macs.
Now the problem I see, is the way the Growl’s Network Notifications work once set up. Is any message displayed on the local Mac (the ones the kids mess), will be Mirrored on the screens of the remote Macs (Macs of the Staff )

Not really what you want, It would be best if the Staff only got the message containing the keyword info.

Now This is Cool,:slight_smile:
It took me a while to work it out.

But using the EPPC:// protocol, you can send the Notifications directly to any of the Staff Macs, without them showing on the
Macs the kids mess with.

As far as my tests go, You do not even need to set-up Growl’s Network Notifications options, because it all works
through Apple Remote Events

Steps.
1, install and Activate Growl. (set to run at Login) on all Macs.
2, In Sharing PrefPane on the Staff Macs, Turn On Apple Remote Events
3, The example Script Below shows how the Notifications can be sent to Only The Staff Macs using Growl and EPPC.
This Script would be on the Macs the kids mess with.

  a, Replace the  [b]shortUserName[/b] in the script below with the Short User Name of the logged in user of a
     receiving Staff Mac.

  b, Look in the Sharing PrefPane, of the Staff Mac. Under the Computer Name, You will see the text: 
     "[b][i]Computers on your local network can access your computer at: [/i][/b] " 
     The name at the end of this is the computers Host Name.
     Replace the [b]LocalHostName.local[/b] in the script below with the Host Name.

  c, When you first compile the Script it Should Ask you to Authenticate the short User Name on Remote Staff Mac.
       Do so BUT make sure you check the 'Save in Keychain' button.

      When the script runs in future it will look to the Keychain for the password.  (you may also be asked later to
       Allow Access to the Keychain, Do so using [b]Allow Always[/b]


tell application "GrowlHelperApp" of machine "eppc://shortUserName@LocalHostName.local"
	set appName to "Computer Namer"
	set notificationName to "Name"
	set notifs to {notificationName}
	set MacName to computer name of (system info)
	
	register as application appName all notifications notifs default notifications notifs
	notify with name notificationName title "COMPUTER NAME" description MacName application name appName
end tell

*Note: Depending on if You use a Single log in or individual log in for each Staff member, I suspect a try block and error handler or a repeat loop and a try block can be set up to work its way through each user name and try and send the message to each of them. Therefore getting the Message out and not failing if a Staff user is not logged on

`

The Remote Apple Event stuff is interesting, but this particular usage seems like a bad idea for security reasons. It seems like this would end up storing the username and password for the staff user account(s) in keychains to which the kids might have very easy access. As the original poster wrote, most of them might not know anything about Mac OS X (and thus keychains), but if one knowledgeable yob does happen by, he could easily “own” the staff accounts (via other Remote Apple Events, ssh, or other means).

I would guess that browsing through keychain entries is any early item on the check list of Mac OS X savvy folks inclined towards mischief. Allowing a person in the know to bypass this attempt at logging via private browsing (see previous post) is one thing. Allowing a person in the know to impersonate staff (at least to the computers) is an entirely different sort of potential problem.

Even if the machine is supposed to be locked down into some sort of “kiosk mode” (e.g. parental controls that limit the allowed applications), I would not feel comfortable implementing a solution that stored (in any way, including keychains) the password for a privileged account in the data of a “public” account (though at least keychains are encrypted on disk).

One thought to avoid this problem is to have the “kids’ computer(s)” share home folders over the network. Then have a (trusted; staff-only) monitoring computer mount the home folders from the “kids’ computer(s)” and do the history scanning there. You might not be able to use Folder Actions to trigger the checks, but at least the trust would flow in the appropriate direction (trusting the trusted (the monitor machine knows the kids’ account credentials and uses them to mounts kids’ machine(s)) instead of trusting the untrusted (kids’ machine(s) knowing staff credentials and using them to send Remote Apple Events)). Remote Apple Events could still be used to display the “Your activity has been logged. Store staff have been notified.” message on the kids’ system (if the monitoring system has the credentials for the kids’ account, why not use it for mounting the share as well as sending scary messages).

This is not a complete solution or analysis, just some off the cuff thoughts. My main advice is to be extremely wary of storing the credentials of one type of account within reach of another type of account (be especially wary when the storing class is less trustworthy than the stored class). You should note that this criterion does not exempt my “mount the kids’ home folder(s)” idea from wariness (only, and only possibly, especial wariness).

They still need the Admin Password to see Passwords, But good point.
If you ignore the keychain stuff in my post above.
You can put the user name and password in a Run only App/script

Example:


tell application "GrowlHelperApp" of machine "eppc://shortUserName:password@LocalHostName.local"

Just tested this, and it works. This AFAIK should be safe.

Again, thank you all.

But I am having a problem with the keywords. It does what its supposed to do, and that is brilliant. But, it will only bring up the dialog box once. for example

Google search>bootle>dialog box

2nd time

Google search>bootle>nothing

Code:

property KeyWords : {"Hasselhoff", "westfield", "bootle", "apple", "bebo", "myspace", "hello", "honeycut", "floor", "bootle 1", "bootle 2", "bootle 25", "bootle+26", "ken woods", "phil woods", "eleanor", "anna", "keith", "adrian smith"}

on adding folder items to this_file after receiving added_items
	repeat with oneItem in added_items
		if checkKeyword(name of (info for oneItem)) then
			set alert_message to "Keyword Alert!:" & return & return & "Your activity has been logged" & return & "Store Staff have been notified. "
			display dialog the alert_message buttons {"Close"} default button 1 with icon 1 giving up after 30
		end if
	end repeat
end adding folder items to

on checkKeyword(N)
	repeat with i in KeyWords
		if N contains i then return true
		tell application "GrowlHelperApp" of machine "eppc://shortUserName:password@LocalHostName.local"
			set appName to "Computer Name"
			set notificationName to "Name"
			set notifs to {notificationName}
			set MacName to computer name of (system info)
			
			register as application appName all notifications notifs default notifications notifs
			notify with name notificationName title "COMPUTER NAME" description MacName application name appName
		end tell
	end repeat
	return false
	
end checkKeyword

How can I make the box pop up if the same keyword is entered again?

This is not the only problem.

The second problem is that the message dose not pop up very quickly, I know this is being very picky, but is there anyway of making it a bit faster?

Cheers, Dan

I have used some of my script, since it is easier for me to adjust my code:)

The script pick out all words from the file name, and displays a separate Growl for each. (do you want just the full file name displayed)

The Main slow down was the Display Dialog, Nothing can happen until its clicked.
I have put it after the growl one.

It May be worth using a separate Growl message for the kids instead as it still holds up the next Growl Message.
As Shown in the second Message.

Remember you can make the messages stay open longer.
Also you can lock the system Prefs.

property KeyWords : {"Hasselhoff", "westfield", "bootle", "apple", "bebo", "myspace", "hello", "honeycut", "floor", "bootle 1", "bootle 2", "bootle 25", "bootle+26", "ken woods", "phil woods", "eleanor", "anna", "keith", "adrian smith"}
property alert_message : ("Keyword Alert!:") & return & return & "Your activity has been logged. " & return & " Store Staff have been notified. "

on adding folder items to this_file after receiving added_items
	
	
	repeat with i from 1 to number of items in added_items
		
		tell application "Finder" to set this_item to words of (displayed name of (item i of added_items as alias) as string)
		
		repeat with z from 1 to number of items in this_item
			
			set this_item_word to item z of this_item
			if this_item_word is in KeyWords then
				
				my checkKeyword(this_item_word)
				display dialog the alert_message buttons {"Close"} default button 1 with icon 1 giving up after 30
			end if
		end repeat
	end repeat
	
end adding folder items to

on checkKeyword(N)
	--repeat with i in KeyWords
	
	   tell application "GrowlHelperApp" of machine "eppc://shortUserName:password@LocalHostName.local"@sunPic-Standalone.local"
		set appName to "KEY WORD ALERT "
		set notificationName to "KEY WORD ALERT "
		set notifs to {notificationName}
		set MacName to computer name of (system info)
		
		register as application appName all notifications notifs default notifications notifs
		notify with name notificationName title ("KEY WORD ALERT for:" & MacName) description N application name appName
	end tell
	--end repeat
	
	
end checkKeyword
property KeyWords : {"Hasselhoff", "westfield", "bootle", "apple", "bebo", "myspace", "hello", "honeycut", "floor", "bootle 1", "bootle 2", "bootle 25", "bootle+26", "ken woods", "phil woods", "eleanor", "anna", "keith", "adrian smith"}
property alert_message : ("Keyword Alert!:") & return & return & "Your activity has been logged. " & return & " Store Staff have been notified. "

on adding folder items to this_file after receiving added_items
	
	
	repeat with i from 1 to number of items in added_items
		
		tell application "Finder" to set this_item to words of (displayed name of (item i of added_items as alias) as string)
		
		repeat with z from 1 to number of items in this_item
			
			set this_item_word to item z of this_item
			if this_item_word is in KeyWords then
				
				my checkKeyword(this_item_word, alert_message)
			end if
		end repeat
	end repeat
	
end adding folder items to

on checkKeyword(N, alert_message)
	--repeat with i in KeyWords
	
	 tell application "GrowlHelperApp" of machine "eppc://shortUserName:password@LocalHostName.local"@sunPic-Standalone.local"
		set appName to "KEY WORD ALERT "
		set notificationName to "KEY WORD ALERT "
		set notifs to {notificationName}
		set MacName to computer name of (system info)
		
		register as application appName all notifications notifs default notifications notifs
		notify with name notificationName title ("KEY WORD ALERT for:" & MacName) description N application name appName
	end tell
	tell application "GrowlHelperApp"
		set appName to "KEY WORD ALERT "
		set notificationName to "KEY WORD ALERT "
		set notifs to {notificationName}
		set MacName to computer name of (system info)
		
		register as application appName all notifications notifs default notifications notifs
		notify with name notificationName title ("KEY WORD ALERT!") description alert_message application name appName
	end tell
	--end repeat
	
	
end checkKeyword