Can't rename moved items with embedded Applescript

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

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 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:
Code: Select all
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
kenexzo
 
Posts: 2
Joined: Sun Dec 15, 2019 6:49 pm

-1728 is errAENoSuchObject. My guess is that no file exists at that path but I'm not entirely sure. I also suggest putting in log points in the script to see how far it gets and what the values are of different variables.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Hey thank you so much for taking the time to respond. I finally got the script working in the Applescript editor. It does what I want by moving all the files of the new folder into the old and then renaming only the files that have been added to that folder. You can see the script here:

Code: Select all
set theFile 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 theFile) 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 theFile
   set spotlight2 to "mdfind -onlyin " & quoted form of POSIX path of theFile & " '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, or 0 to always add a suffix starting at 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
   
   if counter < 1 then
      set counter to 1
      if leadingZeros > 0 then set counter to text -(leadingZeros + 1) thru -1 of ("000000" & counter)
      set newName to theName & separator & counter & theExtension
   else
      set newName to theName & theExtension
   end if
   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


As you can see, it works perfectly in the Applescript editor. I am now having issues embedding it into Hazel. If you try on your end, you can see that the Hazel editor is not accepting the script for some reason. Could it be because the script has a handler added?

It will be great if the future version of Hazel will do this automatically. I hope you can use this script to add this functionality to Hazel that way users do not have to write scripts to move content of duplicate folders into older existing folders.

Please let me know if you are able to embed it in Hazel so I can do it on my end as well.
kenexzo
 
Posts: 2
Joined: Sun Dec 15, 2019 6:49 pm

I'm still not entirely clear on what you are doing that the move action doesn't do with the rename option. As for the script, if it is embedded, you can't have handlers in there as it is implicitly inside a handler itself. You will need to use an external script in that case.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support