Can you simplify this?

I have a portion of my applescript that is accessing the PC server. The access time is REALLY slow, as in it might take 15-20 seconds for this simple task. Can anyone tell me a better way to achieve this that might result in faster response time?
Unfortunately, I am running Mavericks here and the network access time is TERRIBLE.

		tell application "Finder"
			set MyJobFolder to (name of 1st folder of folder "G1:Art Department 2:Work Files2:" whose name begins with JobNum) as string
			set myDestFile to "G1:Art Department 2:Work Files2:" & MyJobFolder & ":" & JobNum & "proof.indd"
		end tell

the slow part is the set MyJobFolder instruction


Hi dklucznik,

Not sure but I’m thinking that the every element reference form with filter looks for all the items that begins with the string and then gets the first one in your script. You might want to use unix ‘find’ to speed things up. It’s been a while, but I think it was something like this:

find -name theNameAndRegEx theFolder

Then get the first item. Still trying to remember how to do this.


It’s probably not just network access: the Finder is notoriously slow at this kind of thing.

You have a couple of options. One is a shell script, using find or ls. Another is to use an ASObjC-based script library. You can read here about script libraries:

So the library code would look like this:

use framework "Foundation"

on firstFolderIn:posixPath beginningWith:thePrefix
	set theNSURL to current application's NSURL's fileURLWithPath:posixPath -- make an NSURL
	set theNSFileManager to current application's NSFileManager's defaultManager() -- get the file manager
	-- get list of all files
	set fileNSArray to theNSFileManager's contentsOfDirectoryAtURL:theNSURL includingPropertiesForKeys:{current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
	-- filter list with predicate
	set theNSPredicate to current application's NSPredicate's predicateWithFormat_("lastPathComponent BEGINSWITH %@", thePrefix)
	set fileNSArray to (fileNSArray's filteredArrayUsingPredicate:theNSPredicate) as list
	-- check list for directories
	repeat with anNSURL in fileNSArray
		set {wasFound, isDir} to (anNSURL's getResourceValue:(reference) forKey:(current application's NSURLIsDirectoryKey) |error|:(missing value))
		if isDir then -- check it's not just a package
			set {wasFound, isPackage} to (anNSURL's getResourceValue:(reference) forKey:(current application's NSURLIsPackageKey) |error|:(missing value))
			if not isPackage then
				return anNSURL's |path|() as text
			end if
		end if
	end repeat
	return missing value
end firstFolderIn:beginningWith:

And your script could call it like this:

set thePath to "G1:Art Department 2:Work Files2:"
set jobNum to 123
tell script "<name of lib>"
      set theFolderPath to  firstFolderIn:(POSIX path of thePath) beginningWith:(jobNum as text)
end tell

That should be considerably faster.

I think this mdfind version may work as well, but I haven’t the time to work out any kinks.

set {theHFSpath, jobnum} to {"G1:Art Department 2:Work Files2:", "1234"}

(((do shell script "mdfind -onlyin " & (theHFSpath & space)'s POSIX path & (("kMDItemKind == Folder") & " && " & ("kMDItemFSName" & " == " & (jobnum & "*")'s quoted form))'s quoted form)'s paragraphs)'s item 1) as POSIX file as text

The biggest “kink” is that it requires the folder to be indexed by Spotlight, and given that the OP is talking about a networked PC server, that may well not be the case.

It also does a recursive search, which could still make it sluggish.

  • there are still bugs in spotlight on network volumes that have huge delays or stops updating after a while.

For best Mac OS X compatibility I would choose Shane’s solutions because of the distinction between packages and folders. I love the find command because of it’s performance but uses standard C lib and UFS were the amount of file types are very limited. In UFS and C lib a package is just a directory.