5

firstly please forgive my lack of knowledge of Unix - that's why I'm here after all. I've created an AppleScript which searches for files on a Mac and have greatly improved the speed by using the following command:

set MySubfolderTextList to do shell script "find " & MyDirectory & " -type d"

In plain Unix that would be find MyDirectory -type d

This gives me a list of every subfolder recursively starting from the top level of the folder called MyDirectory. Now the speed of this is amazing compared to the old routine that I made with regular AppleScript but the thing is when I run this script on a folder with thousands of subdirectories, I get a little beach ball for a number of seconds or minutes indicating that either something is happening or the system has crashed. I was hoping that I could get some feedback from Unix on the progress of the searching, otherwise my clients will freak out that the program has crashed.

2 Answers 2

3

Not really. Consider that find doesn't know what is in the directory tree, but finds out by reading recursively through all directory listing. (sorry for the pun.) For example there could be two first-level directories, one of which has 1 file, and another that has 1000000 files.

Something like copying the tree would be different, since a program could first search the tree to find out the number and size of all files, and use that to estimate completion. That can be done since file sizes are stored explicitly, there's no need to read a whole file to find its size. But as far as I know, no common filesystem saves information about the number of files in a tree. (Not that it would be impossible, but even if some system does that, find would have to be modified to use the information.)

Now, as for the beach ball and impression of freezing, are you running the subprocess syncronously such that the UI is blocked while it runs? That's usually a bad idea, and you might want to search for ways to run the external program in the background, while showing a nice "Still searching..." message. With an option to abort the search if the user gets impatient.

4
  • Good idea, thanks. I can make a progress bar saying "searching in progress". But my goal would have be to get some feedback from the Unix command - not the total number of files but how many it has processed so far. So I could put that in the progress bar. Like "3375 folders processed and counting..."
    – Dr Scripto
    May 26, 2016 at 10:06
  • Hmh? Find outputs the filenames as it goes, so you can just count the names it prints. You need to be able to read the output as it comes, of course, instead of waiting for find to complete. That has more to do with your programming environment than the particular subprocess you run so perhaps I answered the wrong question.
    – ilkkachu
    May 26, 2016 at 10:24
  • Every sane modern file system stores directory entry counts, and at least GNU find does take advantage of this by default. You would still have to enumerate the directory to find out if there are any subdirectories, though.
    – user
    May 26, 2016 at 12:42
  • Do you mean the link count optimization (man find, from -noleaf: "...after it has statted 2 fewer subdirectories than the directory's link count, it knows that the rest of the entries in the directory are non-directories")? That's different from the total number of files in the directory. But in any case, I did mean a recursive count of files.
    – ilkkachu
    May 26, 2016 at 13:19
2

You can use the pv command if you have it to count lines. Eg

 find ~ -type d | pv -l -F '%b dirs found' >/tmp/output
4
  • Could you please explain the syntax of that? For example does the ">/tmp/output" mean it is directing the results to a file? I'm just wondering how I would paraphrase this to get AppleScript to receive this stream of data after running the command.
    – Dr Scripto
    May 26, 2016 at 12:17
  • Yes, ">..." redirects the list of files to a file, because pv will write to standard-error, and this is usually the same as standard-output, so it is usual to pipe or redirect stdout into another program or file. Note, there is a stackexchange site specifically for mac osx.
    – meuh
    May 26, 2016 at 13:39
  • Thanks meuh, I will take what you've suggested and post it over there on the Mac OSX forum. I kind of understand these two alternatives (Find and Find with piping to a file) but I need to find out how AppleScript can access this info while the command is still being executed... or whether this is impossible.
    – Dr Scripto
    May 27, 2016 at 2:10
  • I found a similar topic here:stackoverflow.com/questions/10526255/…
    – Dr Scripto
    May 27, 2016 at 7:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .