Tell if application is running before deleting a file

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

Moderator: Mr_Noodle

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
morkafur
 
Posts: 15
Joined: Mon Mar 19, 2012 12:54 pm

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.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Just what I needed to know....Thanks Mr. Noodle! :)

Appreciate your reply.

- m
morkafur
 
Posts: 15
Joined: Mon Mar 19, 2012 12:54 pm

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>"
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Thanks. I was hoping someone would've replied before me since my AppleScript-fu is not very strong.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support