Page 1 of 1

Tell if application is running before deleting a file

PostPosted: Mon Mar 19, 2012 1:01 pm
by morkafur
Lightroom 4 has a cache file called Previews.lrdata that keeps growing and growing.

I would like to set up a Hazel rule to delete this file periodically. The problem is I can't delete the file if Lightroom (LR) is running or, since this is a cache file, deleting it while LR is running will crash LR.

I found the following code snippet function, but I'm not sure how I would integrate this into a rule inside Hazel.

Hazel seems to wants to have "Passes AppleScript" as the condition so I somehow need to negate the passing when LR is running and return "false" to the rule. IOW, I don't want to delete the file if LR is running.

display dialog appIsRunning("Adobe Photoshop Lightroom 4")

on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning

(This does seems to work when run from the AppleScript editor.)

----

Would appreciate any suggestions.

Thanks in advance,

-- m

Re: Tell if application is running before deleting a file

PostPosted: Mon Mar 19, 2012 5:56 pm
by Mr_Noodle
Looking at it, I think the only line you need is the "tell application..." one. The rest is superfluous.

I think this is more what you are looking for:
Code: Select all
tell application "System Events" to set isRunning to (name of processes) contains "<some app>"

return not isRunning

You need to substitute the name of the app for <some app>. Also, since true means to match the file, in this case, you want to return the opposite.

Re: Tell if application is running before deleting a file

PostPosted: Tue Mar 20, 2012 6:13 am
by morkafur
Just what I needed to know....Thanks Mr. Noodle! :)

Appreciate your reply.

- m

Re: Tell if application is running before deleting a file

PostPosted: Tue Mar 20, 2012 12:35 pm
by a_freyer
Potentially worth noting we can make the script shorter still:

Code: Select all
tell application "System Events" to return (name of processes) contains "<some app>"

Re: Tell if application is running before deleting a file

PostPosted: Tue Mar 20, 2012 3:53 pm
by Mr_Noodle
Thanks. I was hoping someone would've replied before me since my AppleScript-fu is not very strong.