Search for a folder using applescript

applescript

I am trying to make a folder to go on a server, that people can add photos to and then the script sends them to the correct place, however I'm having trouble with the search part.

As you can see below in my code, the part where it finds where to send the folder to is commented out, because I have no idea what the syntax is for it.

Any help would be greatly appreciated.

global theWatchedFolder
set theWatchedFolder to choose folder
on idle
tell application "Finder"
set theDetectedItems to every item of theWatchedFolder
repeat with aDetectedItem in theDetectedItems
    set jobNumber to display dialog "Please enter the job number for this photo." buttons {"Submit", "Cancel"}
    display dialog "File detected: " & jobNumber
    --tell finder
    -- search for jobNumber in (path to desktop)
    --set jobFolder to top search result
    --end tell
    --set colourFolder to jobfolder & /colour
    move aDetectedItem to the desktop --move to colourFolder
end repeat
end tell
if theDetectedItems is not {} then
activate
display dialog "test move complete"
end if
return 1
end idle

Also, I am concerned that if this script is on the server, watching a folder on the server, then it won't create a pop-up for anyone who adds a file to the folder on the server. Hopefully I am wrong but if someone could confirm this one way or the other then that would be awesome. Thanks 🙂

Best Answer

For local files/folders

you can use spotlight's command line tools. In Terminal

$ mdfind -onlyin ~/ "kMDItemDisplayName == 'xyz*' && kMDItemKind == 'Folder'"

will give you all folders whose name starts with xyz with the search begining from your home folder ~/. Remove * in xyz* above to find an exact match. For more information on mdfind read the man page using the command man mdfind in Terminal app.

You can use it in your script like this...

set res to (do shell script "mdfind -onlyin ~/ \"kMDItemDisplayName == 'xyz' && kMDItemKind == 'Folder'\"")    
set fList to (every paragraph of res) as list    
log "Count:" & (get count of fList)

For remote files

that spotlight doesn't know about, you can use the good old UNIX find command.

$ find ~/ -iname "xyz" -type d

You can use it in your script the same way as mdfind but it may be a bit slower.

Hope that helped.