Ok so this is a complex script that I hope someone out-there can help me with because I have spent the entire day trying to figure this out.
Lets say I collect cat images from Instagram and I have many folders of different cats on my computer. I identify each of them by adding a comment in the macOS "get info" section of each folder with the Instagram account name of each cat. For example: @juniorcat
I use Hazel (noodlesoft.com) to sort my folders automatically but it does not know how to merge the contents of the new folder with the old folder. So I have to make an embedded Applescript.
This script I put together is a combination of many others I found on the Internet and it does the following:
It searches the parent directory of the new folder to see if there is an old folder with the same "get info" comment of @juniorcat that exist. It does this by searching for all folders in that directory that have NOT been added today.
If there is an old folder that does have @juniorcat in the "get info" comments section, then the script will move all the images from the new folder, to the old folder.
The script will then sequentially rename all of the images that have been moved to the old folder with the name of that folder and with the lowest number available. For example: Junior the Cat 5.jpg, Junior the Cat 6.jpg, etc.
I got 1 and 2 working correctly, but I am having issues with getting the script to rename ONLY the files I move to the old folder and not the entire contents of that folder.
I get the following error whenever I try:
error "Can’t get name of \"/Users/david/Desktop/Beautiful Cats/Junior the Cat/test7.jpg\"." number -1728 from name of "/Users/david/Desktop/Beautiful Cats/Junior the Cat/test7.jpg"
Here is the script:
set theFile to ":Users:david:Desktop:Beautiful Cats:Junior the Cat-1" as alias
--This is the Hazel matched folder that is renamed with a "-1" at the end because a duplicate name exist.
set inputAttributes to "@juniorcat"
--The comment I add to identify the Instagram Account of each folder.
--Embedded Hazel Script starts here
set parentFolder to POSIX file (do shell script "dirname " & quoted form of POSIX path of (theFile))
set parentPath to quoted form of POSIX path of parentFolder
set spotlight1 to "mdfind -onlyin " & parentPath & " 'kMDItemDateAdded <= $time.today && kMDItemContentType == public.folder && kMDItemFinderComment == " & inputAttributes & "'"
set oldFolder to (do shell script spotlight1)
-- The above shell script is a spotlight search of folders that contain the same comment and have NOT been added today. This is to get the old existing folder.
-- Move Files Script
if (oldFolder = "") then
else
set targetFolder to (POSIX file oldFolder) as alias
tell application "Finder"
move (entire contents of theFile) to the targetFolder
end tell
-- Select the Images added
set targetPosix to quoted form of POSIX path of targetFolder
set command2 to "mdfind -onlyin " & targetPosix & " 'kMDItemDateAdded >= $time.today && kMDItemKind = *image'"
set movedImages to paragraphs of (do shell script command2)
--Rename all the images selected from the above shell script
set text item delimiters to "."
tell application "Finder"
set all_files to every item of movedImages
-- If you replace "movedImages" with "targetFolder", it will name all of the files in that folder. I only want to rename the files I move into this folder.
set new_name to name of folder targetFolder
repeat with index from 1 to the count of all_files
set this_file to item index of all_files
set {itemName, itemExtension} to {name, name extension} of this_file
if index is less than 10 then
set index_prefix to " "
else
set index_prefix to " "
end if
if itemExtension is "" then
set file_extension to ""
else
set file_extension to "." & itemExtension
end if
set the name of this_file to new_name & index_prefix & index & file_extension as string
end repeat
end tell
end if
theFile
is a folder path of the source files? Why are you getting all the files but then performing another search to determine what to rename? Are you planning on using different separators?