on run
tell application “Finder”
set selectedItems to selection
if selectedItems is {} then
display dialog “No file selected. Please select a file first.” buttons {“OK”} default button “OK”
return
end if
end tell
– Prompt user for the keyword to search for the nearest target folder
set folderName to text returned of (display dialog “Enter the name of the target folder:” default answer “”)
– Use mdfind to locate folders named based on user input
tell application “System Events”
set foundFolders to paragraphs of (do shell script “mdfind 'kMDItemKind == "Folder" && kMDItemFSName == "” & folderName & “"'”)
end tell
if foundFolders is {} then
display dialog “No '” & folderName & “’ folder found.” buttons {“OK”} default button “OK”
return
end if
– Loop through each selected file and find its closest folder
repeat with selectedItem in selectedItems
set selectedItemPath to POSIX path of (selectedItem as alias)
set longestCommonPrefix to 0
set closestFolder to missing value
– Compare each found folder path to the selected item path
repeat with aFolder in foundFolders
set commonPrefixLength to my findCommonPrefixLength(selectedItemPath, aFolder)
if commonPrefixLength > longestCommonPrefix then
set longestCommonPrefix to commonPrefixLength
set closestFolder to aFolder
end if
end repeat
– Check if we found a folder
if closestFolder is not missing value then
tell application “Finder”
set selectedItemAlias to selectedItem as alias
set posixPathOfItem to POSIX path of selectedItemAlias
– Retrieve the file name safely
set fileName to name of (selectedItemAlias)
set destinationPath to closestFolder & “/” & fileName
– Handle filename conflict by appending a unique number
set counter to 1
set baseName to fileName
set ext to name extension of selectedItemAlias
if ext is not “” then
set baseName to text 1 thru ((length of fileName) - (length of ext) - 1) of fileName
end if
– Loop to avoid overwriting files in the destination folder
repeat while (do shell script "test -e " & quoted form of (closestFolder & “/” & fileName) & “; echo $?”) is “0”
set destinationPath to closestFolder & “/” & baseName & “-” & counter & “.” & ext
set counter to counter + 1
end repeat
– Move the file to the closest folder
do shell script "mv " & quoted form of posixPathOfItem & " " & quoted form of destinationPath
display notification “File '” & fileName & “’ moved successfully to the nearest folder.”
end tell
else
display dialog “No closest '” & folderName & "’ folder found for " & fileName buttons {“OK”} default button “OK”
end if
end repeat
end run
– Function to find the length of the longest common prefix
on findCommonPrefixLength(path1, path2)
set path1Components to my splitString(path1, “/”)
set path2Components to my splitString(path2, “/”)
if (length of path1Components) < (length of path2Components) then
set minLength to length of path1Components
else
set minLength to length of path2Components
end if
set indexFile to 0
repeat with i from 1 to minLength
if item i of path1Components is not item i of path2Components then
return indexFile
end if
set indexFile to indexFile + 1
end repeat
return indexFile
end findCommonPrefixLength
– Function to split a string by a delimiter
on splitString(theString, theDelimiter)
set AppleScript’s text item delimiters to theDelimiter
set theArray to every text item of theString
set AppleScript’s text item delimiters to “”
return theArray
end splitString
This is more generic code to move to nearest target folder relative to source location. Kindly review and see if it is useful and how it can be improved further