Having Issues Renaming Files That Are Moved From A New to Old Folder

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:

1-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.

2-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.

3-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

This very first instruction is wrong

set theFile to ":Users:david:Desktop:Beautiful Cats:Junior the Cat-1" as alias

The disk name is missing at the beginning.

I guess that Hazel returned the POSIX path :
“/Users/david/Desktop/Beautiful Cats/Junior the Cat-1”
and that you converted it by yourself.

Better use :

set theFile to ((path to desktop as text) &"Beautiful Cats:Junior the Cat-1") as alias
--> alias "Macintosh HD:Users:david:Desktop:Beautiful Cats:Junior the Cat-1"

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 16 décembre 2019 09:33:42

I see, you use for loop counter variable index, which is reserved word. You should use some other variable name.

Then, the 1st if… then block do nothing, because it does the same on true and on false.

set text item delimiters to “.” here do nothing. It only destroys AppleScript’s text item delimiters default value of your Mac, which is “”.

If oldFolder is empty, no need else, just do return:

If oldFolder is "" then return
-- The rest code

Thanks to everyone who took the time to comment on this post. But I got it working by using a totally different approach. Here it is:

set sourceFolder to (((path to desktop folder) as text) & "Beautiful Cats:Junior the Cat-1") as alias
set inputAttributes to "@juniorcat"

# Find an existing folder to add to
tell application "Finder" to set parentFolder to quoted form of POSIX path of ((container of sourceFolder) as text)
set spotlight1 to "mdfind -onlyin " & parentFolder & " 'kMDItemDateAdded <= $time.today && kMDItemContentType == public.folder && kMDItemFinderComment ==  " & inputAttributes & "'"
set destinationFolder to (do shell script spotlight1)

if destinationFolder is not "" then
    # Get a list of added items from sourceFolder
    set spotlight2 to "mdfind -onlyin " & quoted form of POSIX path of sourceFolder & " 'kMDItemDateAdded >= $time.today && kMDItemKind = *image'"
    set movedImages to paragraphs of (do shell script spotlight2)

    # Move and rename the items selected from the above shell script
    tell application "Finder"
        set destinationFolder to destinationFolder as POSIX file as alias -- coerce from POSIX
        set baseName to name of destinationFolder
        repeat with anItem in movedImages
            set anItem to anItem as POSIX file as alias -- coerce from POSIX
            set theExtension to name extension of anItem
            if theExtension is not "" then set theExtension to "." & theExtension
            set movedFile to (move anItem to destinationFolder) -- get a reference to the moved file
            set newName to my (getUniqueName for baseName & theExtension from destinationFolder)
            set name of movedFile to newName
        end repeat
    end tell
end if

# Check someName against items in someFolder, adding a suffix as needed to get a unique name
to getUniqueName for someName from someFolder
    set {separator, counter} to {" ", 0} -- starting suffix - 1 
    set leadingZeros to 0 -- maximum leading zeros (1-6), or 0 for none

    set here to -(offset of "." in ((reverse of text items of someName) as text)) - 1 -- split extension at last period
    set theName to text 1 thru here of someName
    if here is -1 then -- no extension
        set theExtension to ""
    else
        set theExtension to text (here + 1) thru -1 of someName
    end if

    set newName to theName & theExtension
    tell application "System Events" to tell (get name of items of folder (someFolder as text) whose visible is true)
        repeat while it contains newName
            set counter to counter + 1
            if leadingZeros > 0 then set counter to text -(leadingZeros + 1) thru -1 of ("000000" & counter)
            set newName to theName & separator & counter & theExtension
        end repeat
    end tell

    return newName
end getUniqueName