Page 1 of 1

Delay for First 10 mins, timer

PostPosted: Wed Dec 19, 2012 9:00 am
by ST2814
Hi
Firstly, excellent software, I have been using it for past 3 days, it has made my life so much easier!

Now, I am trying to develop like a security system on my macbook.

So the idea is like one more post in this forum but with a delay-

firstly, as the mac boots,
1 Hazel waits for 10 minutes to apply any rules to a folder X
2 Rule - If a file called "off" is present in folder X (and it has already been 10 minutes since bootup) then-
3 Action- Run a apple script which will lock the computer and say a scary message.

I can do no 2 and 3 but I don't know how to make Hazel wait for 10 minutes and then check the file name in folder X.

May be - last modified time or date added can help, don't know ?

Any help, much appreciated.

Thanks
S

Re: Delay for First 10 mins, timer

PostPosted: Wed Dec 19, 2012 11:00 am
by Mr_Noodle
How about "date added is not in the last 10 minutes"?

Re: Delay for First 10 mins, timer

PostPosted: Wed Dec 19, 2012 12:33 pm
by ST2814
Mr_Noodle wrote:How about "date added is not in the last 10 minutes"?


That won't help as the file "off" is already in the folder

The idea is that if after 10 minutes of boot up, Hazel still finds "off" as it is in folder X then run the script,
else,
if I change off to "on" or something within that first 10 minutes then does nothing.

Thanks

Re: Delay for First 10 mins, timer

PostPosted: Wed Dec 19, 2012 1:11 pm
by a_freyer
Similar security system idea, but different overall approach. See my post here regarding a security system with Hazel.

[EDIT - Wow, that was my first post on these forums!]

Re: Delay for First 10 mins, timer

PostPosted: Wed Dec 19, 2012 1:18 pm
by ST2814
a_freyer wrote:Similar security system idea, but different overall approach. See my post here regarding a security system with Hazel.

[EDIT - Wow, that was my first post on these forums!]


Hi Mate, yeah I saw that thanks, which gave me the spark to work on this :)

the only difference is that yours tiggers only when you change the file name but I actually want it to tigger anyway if the file is not changed in first 10 minutes of boot up.

Any suggestion much appreciated
Thanks!

Re: Delay for First 10 mins, timer

PostPosted: Wed Dec 19, 2012 1:44 pm
by a_freyer
If you are only waiting 10 minutes after boot (not login, or otherwise) then you can do this:

Code: Select all
if (all) of the following conditions are met for (the file or folder being matched)
     passes shell script (embedded script)
     ... your other matching criteria ...

Do the following to the matched file or folder:
     ...
 


Embedded line:
Code: Select all
/usr/bin/uptime | grep -Eoce "up [0-9]{1} min" -e "sec"


This quick and dirty little script tests the output of uptime (stats since last boot) by looking for a 1-digit number between "up " and " min" or, alternately the word "sec". The return is either 1 (for an uptime of 1-9 mins or 0-59 secs) or 0 otherwise.

However, note that Hazel returns a MATCH for a zero output, and that's what we have here - we want Hazel to continue testing other conditions only when the system has been booted for more then 10 minutes.

Re: Delay for First 10 mins, timer

PostPosted: Thu Dec 20, 2012 12:42 pm
by ST2814
a_freyer wrote:If you are only waiting 10 minutes after boot (not login, or otherwise) then you can do this:

Code: Select all
if (all) of the following conditions are met for (the file or folder being matched)
     passes shell script (embedded script)
     ... your other matching criteria ...

Do the following to the matched file or folder:
     ...
 


Embedded line:
Code: Select all
/usr/bin/uptime | grep -Eoce "up [0-9]{1} min" -e "sec"


This quick and dirty little script tests the output of uptime (stats since last boot) by looking for a 1-digit number between "up " and " min" or, alternately the word "sec". The return is either 1 (for an uptime of 1-9 mins or 0-59 secs) or 0 otherwise.

However, note that Hazel returns a MATCH for a zero output, and that's what we have here - we want Hazel to continue testing other conditions only when the system has been booted for more then 10 minutes.


Brilliant Idea! I finally sorted it today.

Your embedded needed a little modification:
Since Hazel works with not 0 output but 0 exit code on shell script which means when the grep actually found something so I just added -v to your line for grep to alternate the result.
Means find nothing for first 10 minutes and then find 1 count which result in successful match for Hazel.

/usr/bin/uptime | grep -v -Eoce "up [0-9]{1} min" -e "sec"

So here we sorted the rule to not - match for first 10 minutes, but then the second problem:
Something has to tigger Hazel after 10 minutes to actually scan Folder X again to now find a match.

So I used the logonhook in mac to run another shell script which just sleeps for first 10 minutes and then
modifies "off" file date-modified attribute just after 10 minutes.
This kicks in Hazel to scan the folder again and boom, a match is found!

And full security system runs then.

So FYI the sequence of events is as follows-

When I want the Security System to Lock my Macbook:
-System Boots
- Hazel does routine scan on reboot to folder X -- Result: No Match, as Grep did not find a match
- Hazel Sleeps
- In the mean time, Logonhook kicked in a shell script which is sleeping for 10 minutes
- In 10th minute , the sleeping script touches file "off" in folder X and modifies it's date to current time.
- This kicks in Hazel to rescan Folder X, and this time Match is found as grep condition satisfied
- Which kicks in the mega script:-

Mega script -
-Run your security system script, which will take pictures and snapshots and network info and save them to dropbox, then,
-I added some script to change my account password.
-Set Volume to 6 (Full)
-Say a creative message- something like, you don't seems to be "Your name" therefore I am locking this system. Please Contact "your name"
-Lock the account

When I want the Security System to not run
- I just change the file name from off to on in the first 10 minutes.

When I want the Security System to run on demand remotely
- I just change the file name remotely because it's on dropbox :)

Thanks for all your help! :)

Re: Delay for First 10 mins, timer

PostPosted: Thu Dec 20, 2012 1:04 pm
by a_freyer
It seems like you're not afraid at all of coding. Believe it or not, this entire security system would be VERY simple to write as a standalone cocoa app. If you haven't already tried, it would be a fantastic learning experience for you to begin to dive into objective c.

I am a huge advocate of learning by doing, and objective c is awesome and fun to code with. Look into NSWorkspaceNotifications such as NSWorkspaceSessionDidBecomeActiveNotification on the apple developer documentation reference site.

Re: Delay for First 10 mins, timer

PostPosted: Thu Dec 20, 2012 1:06 pm
by a_freyer
ST2814 wrote:Since Hazel works with not 0 output but 0 exit code on shell script which means when the grep actually found something so I just added -v to your line for grep to alternate the result.
Means find nothing for first 10 minutes and then find 1 count which result in successful match for Hazel.


Also, just for reference, in lieu of inverting the output of grep, you could also force exit:

Code: Select all
exit $(/usr/bin/uptime | grep -Eoce "up [0-9]{1} min" -e "sec")

Re: Delay for First 10 mins, timer

PostPosted: Thu Dec 20, 2012 1:18 pm
by ST2814
a_freyer wrote:It seems like you're not afraid at all of coding. Believe it or not, this entire security system would be VERY simple to write as a standalone cocoa app. If you haven't already tried, it would be a fantastic learning experience for you to begin to dive into objective c.

I am a huge advocate of learning by doing, and objective c is awesome and fun to code with. Look into NSWorkspaceNotifications such as NSWorkspaceSessionDidBecomeActiveNotification on the apple developer documentation reference site.


I was thinking about it actually. I have written quite a few scripts and utilities in windows before, may be it's time to dive into cocoa!

Thanks :)

Re: Delay for First 10 mins, timer

PostPosted: Thu Dec 20, 2012 1:20 pm
by ST2814
a_freyer wrote:
ST2814 wrote:Since Hazel works with not 0 output but 0 exit code on shell script which means when the grep actually found something so I just added -v to your line for grep to alternate the result.
Means find nothing for first 10 minutes and then find 1 count which result in successful match for Hazel.


Also, just for reference, in lieu of inverting the output of grep, you could also force exit:

Code: Select all
exit $(/usr/bin/uptime | grep -Eoce "up [0-9]{1} min" -e "sec")


Oh great. Thanks :)