thank you both for your replies. Shane you were correct the moment I saw your post I realised the mistake I had made. I have not worked with finder like this before so I will heed your advice. As what I am looking for is a search list that I can compare an email subject with and that I do not change my folder construction that often. I am going to work on creating a index/list of all my folders which I can update as required. That will avoid having to use finder on each occasion do you think that makes sense?
McUsr, assuming creating a list of all existing folders in a separate operation works then am I correct in thinking that in your script the âsearchtermâ would be the subject from the email and âdirectortytosearchinâ becomes the list?
Possibly. See how long something like mdfind takes, or you can even use my ASObjC Runner and its enumerate folder command. They might be fast enough to avoid having to think about whether your index is up-to-date.
Once again my thanks I have progressed a little, and if you have time I still need some advice.
Shane I have not used mdfind and my research did not help much so I have gone back to trying to create a list. Have managed to copy it to the clip board but so far have not been able to create a document I could open and check against . Time may get me there my real request is that while I have now managed a list which will have all the folder names as I am only going to compare (subject maybe sender) with the folder name I need an index so that I will then put the mail in the correct folder. Any suggestions please.
This by the way is the scrip that I use to create list.
tell application "Finder"
set folderpath to (choose folder) -- sets file path to folders selected
set thefolders to every folder of entire contents of folderpath
repeat with objItem in thefolders
set ParentFolder to container of objItem -- sets the parent folder
set Foldername to name of objItem as text -- sets the folder name as text
--The dialog box shows, Path to Folder, Parent Folder and Folder name
display dialog "Path to Folder " & objItem & return & "Parent Folder of Folder " & ParentFolder & return & "Name of Folder " & Foldername
end repeat
end tell
Youâre going to need to save the data in a file, and there are lots of ways. Probably the simplest would be to get just the full paths to the folders, and save them as a text file, one per paragraph. Then you can read in the paragraphs of the file, and loop through looking for an entry that ends with the required name.
But depending on the number of folders involved, this may end up slower than mdfind or enumerate folder â AppleScript can get slow with very long lists.
Mdfind searches the disk as fast as Spotlight does. This alleviates the need for creating a list, that may quickly come out of sync anyway. I have adopted the handler to your script, please try this.
Try it with a subject you have, and one you donât have. If it takes more than a whisker when using a subject you have for sure, try to search for that term with spotlight: ( kind:folder your_subject), if spotlight doesnât give you the answer right away (possibly among many others), then something may be corrupted with spotlight.
Then you should empty hardware caches and restart your computer, before rebuilding the spotlight index. Onyx is great for emptying caches. The rest you will find by google.
set folderpath to quoted form of (POSIX path of (choose folder)) -- sets file path to folders selected
set subject to "Multimedia"
set objItem to my findFolderOnly(subject, folderpath)
if objItem is not {} then
tell application "System Events" to tell disk item objItem to set {ParentFolder, Foldername} to {POSIX path of container of it, name of it}
display dialog "Path to Folder:" & objItem & return & "Parent Folder of Folder " & ParentFolder & return & "Name of Folder: " & Foldername
else
display dialog "Not found"
end if
on findFolderOnly(searchterm, directoryToSearchIn)
set searchterm to "\"*" & searchterm & "*\""
try
return first paragraph of (do shell script "mdfind -onlyin " & directoryToSearchIn & " 'kMDItemFSName == " & searchterm & " && ( kMDItemContentType == \"public.folder\" || kMDItemFSSize == 0 )' ")
on error
return {}
end try
end findFolderOnly
Cannot thank you guys enough for all your help but I cannot get it to work correctly. I think it has something to do with spaces in the starting folder name. At Desktop I have the following folders
A New Test Folder
ApplescriptTest.
Test 2
sub2
Sub1
Test1
When I set the subject to âsub2â I get not found and this is what I get
tell application âAppleScript Editorâ
choose folder
end tell
tell application âAppleScript Editorâ
choose folder
â alias âMacintosh HD:Users:mitch:Desktop:A New Test Folder:â
end tell
tell current application
do shell script "mdfind -onlyin /Users/mitch/Desktop/A New Test Folder/ âkMDItemFSName == "sub2" && ( kMDItemContentType == "public.folder" || kMDItemFSSize == 0 )â "
â error âFailed to create query for âNew Test Folder/ kMDItemFSName == "sub2" && ( kMDItemContentType == "public.folder" || kMDItemFSSize == 0 )â.â number 1
end tell
tell application âAppleScript Editorâ
display dialog âNot foundâ
â {button returned:âOKâ}
end tell
Result:
{button returned:âOKâ}
I set up the folders from Applesxript Test in Desktop
ApplescriptTest.
Test 2
sub2
Sub1
Test1
and it works this is the result.
tell application âAppleScript Editorâ
choose folder
end tell
tell current application
do shell script "mdfind -onlyin /Users/mitch/Desktop/ApplescriptTest/ âkMDItemFSName == "sub2" && ( kMDItemContentType == "public.folder" || kMDItemFSSize == 0 )â "
end tell
tell application âSystem Eventsâ
get POSIX path of container of disk item â/Users/mitch/Desktop/ApplescriptTest/Test 2/sub2â
get name of disk item â/Users/mitch/Desktop/ApplescriptTest/Test 2/sub2â
end tell
tell application âAppleScript Editorâ
display dialog âPath to Folder:/Users/mitch/Desktop/ApplescriptTest/Test 2/sub2
Parent Folder of Folder /Users/mitch/Desktop/ApplescriptTest/Test 2
Name of Folder: sub2â
end tell
Result:
{button returned:âOKâ}
I thought it may have been the number of levels âsub2â is 4 down from desktop in the failed example and only 3 down in the one that worked.
I then removed the spaces in the folder I started with and it works. It does not seem to matter if the subject has spaces just the starting point.
Having said that I tried looking for a file under âDocumentsâ (no spaces) on my hard drive that did not work.
Sorry to ask again but can you help I guess I have to pad the name of the starting folder somehow.
I have added the missing âquoted form ofâ in the example above. I recreated your folder structure, and tested it, it worked fine. I am sorry for the trouble, the spaces in filenames was something that slipped by me, when I wrote the example.
It appears that the problem with external drives is again a space. the name of the first one I tried it on âMy Passportâ when I went to one with out spaces it worked again.
You can see the difference âonlyinâ the name of the volume âMy Passportâ but the failed query shows only âPassportâ
Had not see your last post have made that correction and it worked on the example thanks so much. It is amazingly quick.
I have three questions if you would not mind.
The search term appears to be case sensitive can I change that even if it is an option for the user.
I notice that the handler returns all the folders that start with the search term although tit is set to accept the first paragraph. Can I put a repeat loop in the handler so I can select the place I want?
Can I make the search term specific again even as a user option?
I have a long way to go to get my eamils to use this ruotine but cannot thank you enough for your assistance.
Question 1
Nope, I think mdfind by itself is case sensitive, so that is a no no. if your search term consist of 5 letters, then weâd have 120 possible permutations. If you need case insensitivity dearly, then weâd have to generate a list by finder.
I have tried to combine your handler wit your previous suggestions but somehow I am not passing either the âresulttypeâ or âsearchpredicateâ correctly. I have forced them both to one of the options within the handler that works. when i use display dialog to see what they are at the handler they both appear to be correct. to make sure no spelling mistakes I copied the items from the handler to the âchoose listâ. Obviously doing something wrong. I have tried both as strings and text and that did not help.
If I set the searhpredicate within the handler I then get a problem with resulttype. If I set both it works so I think it is the way I am passing the data?
First of all, the choose from list, returns a list, so the searchPredicate, and the resultType parameters wonât get evaluated as text, and therefore fails.
Thanks for making me aware of this, Iâll enforce the handler with your findings. In the mean time, use the handler like this:
set objItem to my findFolderOnly(resultType as text, searchPredicate as text, subject, folderpath)
The line that sets the parent folder, and folder name also needs to be fixed, to something like this:
tell application "System Events" to tell disk item objItem to set {ParentFolder, Foldername} to {POSIX path of container of it, name of it}
I see that you have changed the handler, it would be nice if you didnât, or gave a notice when you did, as people may think that it is the original one, when it isnât. (I think of the removal of âset quoted form ofâ, mainly, I understand the need for the display dialog for debugging purposes, (and so do others).
First my apologies for the change, other than the addition of display dialog(s) I commented out the quoted path as you had already told me to put that in the first script and I figured that any reader would see that as it had not been deleted. Anyway as I said I apologise and will make sure in future if I post the script again there is a reference to any change(s) I made.
Made your first change and it seems to work, have not run an extensive test yet as I wanted to get my apology underway. however your second change is as I thought you had it in the originally handler and is in the last scrip I sent to you,am I missing something.
As I have already made a mistake I would like ask if you are OK with me changing "directorytosearchin to folderpath. If you do not agree I will change the start of the routine so the variables that are being passed are the same names. I understand directory to be more of a UNIX term as opposed to folder in Applescript.
Thank you it is important to me that given the time and effort you have put into helping me that I do not plagiarize your work or fail to acknowledge what you have done.
thanks again I am sure I will be back as I work through my project.
It is not about plagiarizing, it is really about knowing which version is which.
I am far more concerned about something I have written still works how I intended it to do.
Plagiarizing, now that would be if you stripped out the copyright notice, and said you had made it, maybe after changing the variable names, that is something entirely different.
Canât we just drop this, and go on with something more productive?
Hopefully you will help me with a new issue. As I think you know I am trying ultimately to selected a path based on the result of the search.
One thing I had to change was to allow a display of all the search results if I had called for a resulttype of âAllâ or Every" I did that by inserting an if then logic test.
if resultType as text = "All" or resultType as text = "Every" then
display dialog "This is the list Returned from the handler " & MyFolderList
set myActiveFolder to choose from list MyFolderList
else
tell application "System Events" to tell disk item objItem to set {ParentFolder, Foldername} to {POSIX path of container of it, name of it}
display dialog "Path to Folder:" & objItem & return & "Parent Folder of Folder " & ParentFolder & return & "Name of Folder: " & Foldername
end if
In my test case two folders met the criteria of an exact match and both were displayed from the âdisplay dialogâ command however the choose list only shows the first of the two. I think it may have something to do with delimiters but cannot figure out what.