Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nedanator

macrumors newbie
Original poster
Apr 11, 2008
28
0
Derby, UK
You may have guessed, I am trying to use applescript. But its not working.

I am trying to make an script that will do the following.

> Watch the Safari history folder.
> Whenever a file is added it will bring up a dialog box with one button. "Close"
eventually that will be changed to only bring up the dialog box when a specific keyword is found. but for the time being, I want it to come up every time a new file is added.

I used the 'Add new file' apple example script. that worked.
I edited it a bit to say a different statement in the box and only have one button. It worked. but now, for some reason, whenever a new file is added it waits for about 15 seconds and then flashes safari.

Grrrrr!! Why is this so?
 

Nedanator

macrumors newbie
Original poster
Apr 11, 2008
28
0
Derby, UK
property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_file
after receiving
added_items
try

set alert_message to ("Keyword Alert!:" & return & return) as Unicode text
if the item_count is greater than 0 then
set alert_message to alert_message & (the item_count as text) & " Your activity has been logged. "
else
set alert_message to alert_message & "Store Staff have been notified. "
end if
display dialog the alert_message buttons {"Close"} default button 1 with icon 1 giving up after dialog_timeout
set the user_choice to the button returned of the result

end try
end adding folder items to
 

xUKHCx

Administrator emeritus
Jan 15, 2006
12,583
9
The Kop
It doesn't work because you do not define item_count so it fails out from there.

This seems to work on my computer

Code:
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 & (the item_count as text) & " Your activity has been logged. "
		else
			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

When testing something like this I knock out the "on adding folder items to this_file after receiving added_items" and the try lines by adding "--" (no quotes in front of them. This way the script editor will tell you that something went wrong and give clues as to what is wrong To get around the folder action part I added a choose file command. So when I was testing the script it actually looked like this.

Code:
--on adding folder items to this_file after receiving added_items
--	try
set added_items to choose file with multiple selections allowed
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 & (the item_count as text) & " Your activity has been logged. "
else
	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

Also open up "event log history" (found under window) this will tell you want the script sees and is doing.


What are you actually trying to do because the script seems to give the impression you are trying to monitor staff. You might want to look into managed accounts with parental accounts where you can restrict things and record logs.
 

Nedanator

macrumors newbie
Original poster
Apr 11, 2008
28
0
Derby, UK
Thanks very much.

Eventually it would (hopefully) monitor the history file of safari and check the file names against a blacklist and a greylist and only pop up a dialog box when a file names contains a word from the blacklist etc etc. It would then send a message to two other computer saying that an alert has been triggered and the name of the computer in question.

For the time being though, is there any way to get the dialog box to pop up quicker?

(yes I am new to applescript)

Also, do you know of any good applescript learning sites? All the ones I have looked are just one massive page of text that I don't want to read purely because it is just one massive block of text.

And thank you again.
 

xUKHCx

Administrator emeritus
Jan 15, 2006
12,583
9
The Kop
It pops up as soon as it is called therefore I don't really think it is possible to speed it up.

In terms of websites I learnt my muddling through, using google, asking for help, dissecting other peoples code, reading the dictionary for the application (File - Open Dictionary).

http://macscripter.net/ seems to give good examples (from a cursory look).

I still think that you may want to look into a managed account.
 

lancestraz

macrumors 6502a
Nov 27, 2005
898
0
RI
I don't think using folder actions is the best way to go. I would use a script that constantly ran in the background and monitored the folder.

Using a managed account would really be easier...

Edit: You could also use openDNS as content filter. Just sign up (free) and configure it to block adult content.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.