Page 1 of 1

Applescript that halts hazel rule when an proces is running

PostPosted: Mon May 29, 2017 4:41 pm
by spawnreaper
Hello,

I am trying to make an apple script that will delay hazel executing progress until a process is no longer active in activity monitor.

The situation is that I am running drop folder to shrink videos with HandBrakeCLI. Problem is that the destination folder wants to copy it even though the file is nog finished. I have tried to put a "last modified" line in there to prevent this type of behaviour but it does not work.
I have noticed that the HandBrakeCLi proces appears as soon as its converting and closes when its done.
For some reason the script i am trying is not working. Bit of a novice with apple script.

I found two scripts that i modified but they don't run when I embed them in the rule.
Code: Select all
on is_running(appName)
    tell application "System Events" to (HandBrakeCLI) contains appName
end is_running

set safRunning to is_running("HandBrakeCLI")
if safRunning then
    delay 240
    end tell
    return "Running"
else
    return "Not running"
end if


and this

Code: Select all
tell application "System Events"
   -- is it running?
   if true (application process “HandBrakeCLI” exists) then
delay 240
then repeat
if false
end tell


hope someone could help me out.

Re: Applescript that halts hazel rule when an proces is runn

PostPosted: Tue May 30, 2017 11:20 am
by Mr_Noodle
While the specifics of the script is a bit outside of Hazel support, note that Hazel won't wait forever for a script to finish. At some point it will time out so this is probably not a good approach.

What condition did you use with date last modified? Also, what program is triggering Handbrake?

Re: Applescript that halts hazel rule when an proces is runn

PostPosted: Tue May 30, 2017 4:09 pm
by spawnreaper
The application "Drop folder" has a watch and destination folder. Large video file goes in, smaller version comes out.
The "out" folder is the problem. If a files is placed in the "watch folder it starts the "HandBreakeCLI" process witch does the actual converting. It creates a file in the "out" folder and fills it. Problem is that as soon as Hazel finds the file it starts to move it to my sickrage compleet watch folder. It does not notice that the file is corrupt. The original file is deleted and I need to download it again.
I found in the forum that other people also have this problem and resolved it with a "not modified in the last 5 min" condition.
For some reason that does not work. The "last modified date" remains equal to the "creation date" for some reason.

So I thought it might be a good idea to make the rule dependant on the converting process in stead of the modify date.
Seemed cleaner.
Have you got any idea I might try?

Re: Applescript that halts hazel rule when an proces is runn

PostPosted: Tue May 30, 2017 4:33 pm
by spawnreaper
Just an small edition. The problem of moving incompleet files is also a problem when I copy a file to another hazel monitored folder instead of moving it. Copying takes time, and Hazel does not seem to allow that time. A move is fast enough for hazel not to have that problem.

Re: Applescript that halts hazel rule when an proces is runn

PostPosted: Wed May 31, 2017 11:27 am
by Mr_Noodle
Can you have Drop Folder attach a tag to the file when done? Also, can you tell me a bit about the source and destination folders? What kind of filesystem are they? Are any network drives?

Re: Applescript that halts hazel rule when an proces is runn

PostPosted: Thu Jun 01, 2017 11:30 am
by spawnreaper
I user osx Sierra.
The drop folder folders are locally.
I can't find any options to add anything to the name in form of a tag or anything. I can just say what HandBrake preset it should use for the conversion.
When a job is picked up from the input folder it is placed in a sub folder named "queue" there a job number is temporarily added. The job is processed. during that an empty file with the original name and extension is added to the output folder. The file is gradually filled as the converting process progresses.

Re: Applescript that halts hazel rule when an proces is runn

PostPosted: Thu Jun 01, 2017 11:36 am
by spawnreaper
maybe, I am just spitballing here, could hazel check if a folder in another watch folder is empty? then I could just let it check if the source folder is empty because that would mean that the process is compleet.
any idea how to do that?

Re: Applescript that halts hazel rule when an proces is runn

PostPosted: Fri Jun 02, 2017 10:39 am
by Mr_Noodle
A script would be needed, unless the source folder is "close by" the destination. For example, if the source and destination folders are under a common folder, you can try something like:
Code: Select all
If (all) are met
    If (all) are met for (the enclosing folder)
        If (all) are met for (any of its sub-files/folders)
            Name is <source folder name>
            Sub-file/folder count is 0

Look up "nested conditions" in the help on how to do the above. That would check if there's a folder under the parent folder with a certain name and no items in it.