PDA

View Full Version : Baa! Applescript! Grr! Why wont you work?!




Nedanator
Apr 15, 2008, 06:13 AM
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?



xUKHCx
Apr 15, 2008, 06:14 AM
A copy of the script code would be helpful.

Nedanator
Apr 15, 2008, 06:16 AM
That is a good point, I shall go away and find it and then come back.

Nedanator
Apr 15, 2008, 06:19 AM
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
Apr 15, 2008, 10:24 AM
It doesn't work because you do not define item_count so it fails out from there.

This seems to work on my computer

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.

--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
Apr 15, 2008, 11:23 AM
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
Apr 15, 2008, 11:34 AM
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
Apr 15, 2008, 12:35 PM
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 (http://www.opendns.com/) as content filter. Just sign up (free) and configure it to block adult content.