AppleScript in condition, rules matches but nothing happen

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

OSX 10.10
Hazel: 3.3.1 (1200)

For a items in a folder I want achieve this logical behavior in Hazel:

Don't apply rule actions until I quit iTunes

I have a rule with this "Passes AppleScript" condition:

Code: Select all
if application "iTunes" is running then
   return false
else
   return true
end if


Easy, if iTunes is running Hazel will avoid apply actions to files. Ok, but what will do Hazel with these files when quit iTunes? nothing??.

I think that Hazel understand that files have been processed by the rules correctly although actions have not been applied (return false). Exists a law that say "if a file matches the same rule it matched last time, it will not re-run its actions", maybe this law apply here?

If instead I apply this Applescript code:

Code: Select all
if application "iTunes" is not running then
   return true
end if


In this code Hazel don't have "return false" and when iTunes is active the script give an error (normal). Maybe this error causes that Hazel re-evaluate the situation in next Hazel cycle for all files? If in the next Hazel cycle iTunes is closed the rules will apply actions on files as expected?

What is the clean way to do that Hazel re-evaluate automatically all files against rules when iTunes is not launched?

Note1: If i pause / un-pause monitored folder Hazel re-evaluate all files against rules again and all works, but is not a solution due to I must do it manually every time.

Note2: I had wait more than 20 minutes in every test to look if Hazel do something in their cycles.

A screencast with an example: https://dl.dropboxusercontent.com/u/122 ... -44-28.mp4

Log of this screencast:
Code: Select all
 Marcador - 31/10/2014 10:43:25
2014-10-31 10:45:01.886 HazelHelper[9196] DEBUG: Deploy folders: (
    "<ComNoodlesoft_HazelConcreteFolder: 0x7f8f15b26390> \"Applescript\""
)
2014-10-31 10:45:01.887 HazelHelper[9196] DEBUG: Thread 0x7f8f13c29690: FOLDERS SYNCED: (
    "<ComNoodlesoft_HazelConcreteFolder: 0x7f8f13f7f030> \"Applescript\""
)
2014-10-31 10:45:03.346 HazelHelper[9196] DEBUG: Thread 0x7f8f13c29690: Run worker for folder: /Users/edenexposito/Desktop/Applescript
2014-10-31 10:45:03.376 hazelworker[52596] ###main load address: 0x10a2a3000
2014-10-31 10:45:03.377 hazelworker[52596] ###Noodle load address: 0x10a3b2000
2014-10-31 10:45:03.377 hazelworker[52596] ###CK load address: 0x10a37a000
2014-10-31 10:45:03.390 hazelworker[52596] DEBUG: Program is licensed.
2014-10-31 10:45:03.396 hazelworker[52596] DEBUG: Unexpected type for Mail download URL: (null)
2014-10-31 10:45:03.404 hazelworker[52596] DEBUG: Could not find entry for default_directory in Chrome preference file.
2014-10-31 10:45:03.410 hazelworker[52596] Processing folder Applescript (forced)
2014-10-31 10:45:03.410 hazelworker[52596] DEBUG: Initialized
2014-10-31 10:45:03.410 hazelworker[52596] DEBUG: Pausing to wait for things to settle down.
2014-10-31 10:45:05.510 hazelworker[52596] DEBUG: Processing directories: (
    "/Users/edenexposito/Desktop/Applescript"
)
2014-10-31 10:45:05.512 HazelHelper[9196] DEBUG: Checking events for path /Users/edenexposito/Desktop/Applescript, folder <ComNoodlesoft_HazelConcreteFolder: 0x7f8f15b26390> "Applescript"
2014-10-31 10:45:05.513 hazelworker[52596] DEBUG: Received file event: {
    date = "2014-10-31 09:45:03 +0000";
    path = "<ComNoodlesoft_NoodlePathSet: 0x7fa6ea502f20> - (\n    \"/Users/edenexposito/Desktop/Applescript\"\n)";
}
2014-10-31 10:45:05.513 hazelworker[52596] DEBUG: About to process directory /Users/edenexposito/Desktop/Applescript
2014-10-31 10:45:05.514 hazelworker[52596] DEBUG: .DS_Store: File is hidden/invisible. Skipping.
2014-10-31 10:45:05.538 hazelworker[52596] DEBUG: documento.pdf: Did not match any rules.
2014-10-31 10:45:05.538 hazelworker[52596] DEBUG: Directory /Users/edenexposito/Desktop/Applescript processed in 0.024997 seconds
2014-10-31 10:45:05.538 HazelHelper[9196] DEBUG: Checking events for path /Users/edenexposito/Desktop/Applescript, folder <ComNoodlesoft_HazelConcreteFolder: 0x7f8f15b26390> "Applescript"
2014-10-31 10:45:05.539 hazelworker[52596] DEBUG: Sending metrics to scheduler. Next scheduled run:
2014-10-31 10:45:05.540 HazelHelper[9196] DEBUG: Received metrics for folder /Users/edenexposito/Desktop/Applescript: {
    directoryDepth = 0;
    requestedSchedulingTime = "";
    triggerPaths = "<ComNoodlesoft_NoodlePathSet: 0x7f8f13d9f660> - (\n)";
    unavailablePaths = "{(\n)}";
}
2014-10-31 10:45:05.540 HazelHelper[9196] DEBUG: Timer scheduled for folder /Users/edenexposito/Desktop/Applescript at
2014-10-31 10:45:05.540 hazelworker[52596] Done processing folder Applescript
2014-10-31 10:45:05.544 HazelHelper[9196] DEBUG: Thread 0x7f8f13c29690: Task removed: [52596]
 


See this in log:
Image

Image
edenexposito
 
Posts: 35
Joined: Sat Nov 05, 2011 4:17 pm
Location: Palma de Mallorca - SPAIN

Note that Hazel doesn't know what you are doing in its script, so it has no idea that there is a dependency on iTunes' state. So, it can't wake up when iTunes does since it has no idea that it needs to do that. You'll probably have to make Hazel poll in such a case by adding a condition like "Date last matched is not in the last X minutes". It will be more resource-intensive if you do that, though.
Mr_Noodle
Site Admin
 
Posts: 11866
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Also, the next patch is going to make Hazel more lenient with AppleScripts in conditions. If you don't return anything explicitly, Hazel will interpret that as the script failing so no error will occur in that case.
Mr_Noodle
Site Admin
 
Posts: 11866
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

As you say:
Hazel doesn't know what you are doing in its script, so it has no idea that there is a dependency on iTunes' state


Totally correct. We must add "something" to create a "loop state" for Hazel. You propose add condition "Date last matched is not in the last 1 minutes", theoretically Hazel must check files against the rule every "1 minute".

This would be a perfect solution... if works. (or if I put correctly, maybe I'm doing something wrong)

I add your propuse like that:

Image

And then i force "run rules" with iTunes active. Looking at log:

Code: Select all
 Marcador - 31/10/2014 17:53:24
2014-10-31 17:53:28.052 HazelHelper[9196] DEBUG: Thread 0x7f8f13c29690: Run worker for folder: /Users/edenexposito/Desktop/Applescript
2014-10-31 17:53:28.079 hazelworker[57702] ###main load address: 0x109ea5000
2014-10-31 17:53:28.080 hazelworker[57702] ###Noodle load address: 0x109fb5000
2014-10-31 17:53:28.080 hazelworker[57702] ###CK load address: 0x109f7b000
2014-10-31 17:53:28.091 hazelworker[57702] DEBUG: Program is licensed.
2014-10-31 17:53:28.097 hazelworker[57702] DEBUG: Unexpected type for Mail download URL: (null)
2014-10-31 17:53:28.104 hazelworker[57702] DEBUG: Could not find entry for default_directory in Chrome preference file.
2014-10-31 17:53:28.110 hazelworker[57702] Processing folder Applescript (forced)
2014-10-31 17:53:28.110 hazelworker[57702] DEBUG: Initialized
2014-10-31 17:53:28.110 hazelworker[57702] DEBUG: Pausing to wait for things to settle down.
2014-10-31 17:53:30.209 hazelworker[57702] DEBUG: Processing directories: (
    "/Users/edenexposito/Desktop/Applescript"
)
2014-10-31 17:53:30.211 HazelHelper[9196] DEBUG: Checking events for path /Users/edenexposito/Desktop/Applescript, folder <ComNoodlesoft_HazelConcreteFolder: 0x7f8f13d3c0a0> "Applescript"
2014-10-31 17:53:30.212 hazelworker[57702] DEBUG: Received file event: {
    date = "2014-10-31 16:53:28 +0000";
    path = "<ComNoodlesoft_NoodlePathSet: 0x7f8a4342e560> - (\n    \"/Users/edenexposito/Desktop/Applescript\"\n)";
}
2014-10-31 17:53:30.212 hazelworker[57702] DEBUG: About to process directory /Users/edenexposito/Desktop/Applescript
2014-10-31 17:53:30.213 hazelworker[57702] DEBUG: .DS_Store: File is hidden/invisible. Skipping.
2014-10-31 17:53:30.237 hazelworker[57702] DEBUG: documento.pdf: Did not match any rules.
2014-10-31 17:53:30.237 hazelworker[57702] DEBUG: Directory /Users/edenexposito/Desktop/Applescript processed in 0.025202 seconds
2014-10-31 17:53:30.238 HazelHelper[9196] DEBUG: Checking events for path /Users/edenexposito/Desktop/Applescript, folder <ComNoodlesoft_HazelConcreteFolder: 0x7f8f13d3c0a0> "Applescript"
2014-10-31 17:53:30.238 hazelworker[57702] DEBUG: Sending metrics to scheduler. Next scheduled run:
2014-10-31 17:53:30.239 HazelHelper[9196] DEBUG: Received metrics for folder /Users/edenexposito/Desktop/Applescript: {
    directoryDepth = 0;
    requestedSchedulingTime = "";
    triggerPaths = "<ComNoodlesoft_NoodlePathSet: 0x7f8f13f79de0> - (\n)";
    unavailablePaths = "{(\n)}";
}
2014-10-31 17:53:30.239 HazelHelper[9196] DEBUG: Timer scheduled for folder /Users/edenexposito/Desktop/Applescript at
2014-10-31 17:53:30.239 hazelworker[57702] Done processing folder Applescript
2014-10-31 17:53:30.243 HazelHelper[9196] DEBUG: Thread 0x7f8f13c29690: Task removed: [57702]


Why requestedSchedulingTime = ""; now? It's supposed that Hazel must check rule again in a future. If not how is supposed that will Hazel check "Date last matched is not in the last 1 minutes"

I must avoid use "return false" in AppleScript to achieve that?
edenexposito
 
Posts: 35
Joined: Sat Nov 05, 2011 4:17 pm
Location: Palma de Mallorca - SPAIN

If there is no requested time, that means Hazel will sleep until a file event (an actual change in one of the files) happens. I believe the issue here is that even if Date last matched is not in the last minute, the other condition (the AppleScript) is seen as a static condition, meaning that no passage of time will change it since, to Hazel, it's a black box. So, no reason for Hazel to check again in a minute.

There might be an alternate way to hack around this. You could try adding a rule like:
Code: Select all
    If (all) met
        Date Last Matched is not in the last 1 minute
        Name is <some special file in that folder>
    Do
        Run shell script: touch "$1"


That will update the modification time on a file in that folder that you add for just this purpose. That should generate a file event every minute which would wake up Hazel in this case. Not very pretty but something that you might want to try out.

As for the future, I'd need to think about some way of scripts communicating a time when they would want to be re-evaluated.
Mr_Noodle
Site Admin
 
Posts: 11866
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:As for the future, I'd need to think about some way of scripts communicating a time when they would want to be re-evaluated.


This would be great.

Your hack "works" but with drawbacks, we loose an actual "modification date" due "touch command". And if we dont want remove the file from monitorized folder "Date last matched is not in the last 1 minutes" will degrade Hazel performance due to Hazel don't stop until we remove files from the monitorized folder.

Thanks for your response!.
edenexposito
 
Posts: 35
Joined: Sat Nov 05, 2011 4:17 pm
Location: Palma de Mallorca - SPAIN

Well, you would use a special file that isn't used for anything else so losing the modification time won't matter since that's the file's whole purpose. Yes, it does degrade performance but is the best can be done in this instance short of you triggering it yourself manually.
Mr_Noodle
Site Admin
 
Posts: 11866
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support