if directory doesn't contain extension recursively then quit app

This seems to work. The only problem is it is not recursive.

property Location : alias "Macintosh HD:Location"
tell application "System Events" to files of Location whose name extension = "ext"
if the result = {} then tell application "Example" to quit

Hello.

If I understand your problem right you want to check a folder for files that ends with an extension, and if there are no such files there, then you want an application to quit.

Open the script below, adjust its properties, then save it as a stay open application, and double click it.
It will run every 30 minutes, until you quit it. And it is visible in the dock. when you see that it works, you will want to download some scripts from Mac Os X Automation, that you will have to find your self, for setting bundle properties from within AppleScript editor, I believe one of them is called “set background only”, and that is the one you want to run. :slight_smile:

property fileLoc : alias "Macintosh HD:Location"
property fileExt : "ext"
property appName : "Transmission"

on idle
	tell application "System Events" to set curFiles to files of fileLoc whose name extension = fileExt
	if the curFiles = {} and running of application appName is true then tell application appName to quit
	return 1800
	-- seconds
end idle

Why not use Finder? It does not need recursion:

tell application "Finder"
	set theTest to (files of entire contents of thisFolder whose name extension = "ext")
end tell

if theTest = {} then tell application "Example" to quit

When you don’t want to use Finder, this does recurse:

property thisFolder : alias "some:folder:"

walkFolder(thisFolder)

on walkFolder(thisFolder)
	--
	-- process files in this folder
	tell application "System Events" to (files of thisFolder whose name extension = "ext")
	
	if the result = {} then
		tell application "Example" to quit
	else
		-- collect folders in current folder
		tell application "System Events" to set theFolders to folders of thisFolder
		--
		-- process folders in this folder
		repeat with thisFolder in theFolders
			walkFolder(thisFolder) -- go deeper
		end repeat
	end if
end walkFolder

Don’t use “Location” as a variable name with SE, it’s an application term there.

Hello.

Here is a new improved version of the first applet, that uses mdfind, (Spotlight, which is fairly fast), for finding any files with the extension in subfolders. :slight_smile:

property fileLoc : alias "Macintosh HD:Location"
property fileExt : "ext"
property appName : "Transmission"
property idleTime : 1800
on idle
	try
	set curFiles to paragraphs of (do shell script "mdfind  -onlyin " & quoted form of (POSIX path of fileLoc) & " ' kMDItemKind == * ' | grep '^.*\\." & fileExt & "$'")
on error
	if running of application appName is true then tell application appName to quit
	return idleTime
end try

	return idleTime
	-- seconds
end idle

Are you assuming that kMDItemkind contains the extension?
I think that is not correct. For one thing, it is localised: when a folder it is “Map” in dutch. It ‘works’ with PDFs, as those are “PDF (Portable Document Format)”. It does not work with ext = ‘scpt’, because its item kind is “Script”.
Then again, grep may understand that ‘scpt’ and ‘Script’ are the same thing. I know nothing, grep-wise.

As alastor933’s pointed out, the Finder has a built-in recursive reference form, but it’s rather slow. Another alternative would be the “find” shell command:

property Location : alias "Macintosh HD:Location"

set x to (do shell script "find -f " & quoted form of POSIX path of Location & " \\( -type f -name '*.ext' \\)")
if ((count x) is 0) and application "Example"'s running then tell application "Example" to quit

Compiling file references or paths as properties isn’t normally recommended as they’ll then only work on that machine, but of course it’s OK if that’s all they’ll ever be required to do.

Hello.

Of course the wildcard for KDItemKind returns everything, pretty much the same as issuing

The -onlyin option, sees to that the query is only performed on that folder and any subfolder of it.
.
So, it retuns all files, and lets grep see if any of this has the extension of ext, now I have seen some other posts by dj.custer, so I know that he comes from Unix land, and have deduced that the extension, is the textual representation.

And it is faster than anything else, until you use an ASObjC Spotlight query, with spades.

Edit

Finder was never fast, end entire contents was never a fast command, but now under Mavericks entire contents IMHO is pretty useless, as it “crashes Finder for nothing”. I think they have balanced the OS further in favor of the UI, that is at least my impression, mabe it is the increased usage of multithreading that gives the effect.

All in all, I think Mavericks is a huge step forward.

Find will by the way be faster in circumstances, where there are few folders, and no structure beneath (-depth=1), but it recurses, I believe mdfind, to just look up the files in the Spotlight database, which means that the folder in question must be indexed by Spoltight, and Spotlight must work. So far Spotlight has worked for me under Mavericks.

Aliases in properties.

I think that is a good thing, when you hand a script over to somebody not so experienced in AppleScript, as they can’t compile it, until they have gotten it right. So you catch the problems before run-time errors occurs. :slight_smile:

First of all. Thank you EVERYONE! Honestly, I won’t have more than a couple files in my Downloads folder at once. So I think that this would work beautifully. How would you use the Downloads folder as thisFolder? Property worked. But I was wondering if there was a easier way. Also I made a test folder with test.part in the downloads folder, and it still quit. Does that mean I need a does not equal sign, or that there was no recursion?

Again THANK YOU very much EVERYONE!

set thisFolder to (path to downloads folder)

tell application "Finder"
	set theTest to (count (files of entire contents of thisFolder whose name extension = "ext"))
end tell

if (theTest = 0) then tell application "Example" to quit