Change Filename with Clipboard-Text 2019 Version

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

Moderator: Mr_Noodle

Hi All,

I would like to use Hazel to manually rename files when sent to a monitored folder.

Here are my core objectives:

    1 - Name files based on text manually entered. Retain existing name, as current code does and add a new colour label when processed
    2 - Use cached text (if available) to save time when renaming multiple images of the same event
    3 - Option to clear the cache and be re-presented with option to enter new name as in 1 above
    4 - Option to Quit and Exit. Used when file has been sent by mistake - This would then add Failed_01 to the start of the file and retain the original name. Hazel will then move the file based on a rule
    5 - Upon selecting quit the code should also change the colour label to a different colour. This is to enable Hazel to process file in the same way as 4 above
    6 - Confirmation Screen(s) before any changes are committed
    7 - Code closes ready for the next run.

    The code I have created goes some way to achieving the points above.
    The files sent also have a range of existing tags and a unique color label (not the same colour, as used in the rename code), which seems to make a difference to the way the code works. I'd like to keep these, although it is not a show stopper if I can't.

I took the base code ( see link below ) from the post created on Sat Jan 22, 2011 12:55 am by dhy8386 to build upon.

The code has been setup in Hazel, as per the instructions by dhy8386 and works with a quirk. Upon a file being dropped into the monitored folder the code displays a screen to enter new details or change the file name. Upon entering 'OK' it shows the screen display screen again, upon submitting 'OK' for the second time the file is processed and the color label is applied.

The two attempts to make the change are not a bad thing, as I would like a confirmation option before committing any changes.

This is the output from the log:

019-04-30 21:53:07.813 hazelworker[37944] Running worker (v4.3.5) for folder with identifier: 16777223-8602086334.
2019-04-30 21:53:07.815 hazelworker[37944] ###main load address: 0x10e949000
2019-04-30 21:53:07.815 hazelworker[37944] ###Noodle load address: 0x10ea69000
2019-04-30 21:53:07.815 hazelworker[37944] ###CK load address: 0x10ea2d000
2019-04-30 21:53:07.843 hazelworker[37944] Processing folder Advance Hazel Rename
2019-04-30 21:53:09.855 hazelworker[37944] Main Stage One image ID_FPR0_53176 .jpg: Rule Advance Rename Testing matched.
2019-04-30 21:53:15.476 hazelworker[37944] [Error] AppleScript failed: Error executing AppleScript on file /Users/me/Advance Hazel Rename/Main Stage One image ID_FPR0_53176 .jpg.
2019-04-30 21:53:15.476 hazelworker[37944] OSAScript error: {
NSLocalizedDescription = "Finder got an error: Can\U2019t set alias \"/Users/me/Advance Hazel Rename/Main Stage One image ID_FPR0_53176 .jpg\" to 6.";
NSLocalizedFailureReason = "Can\U2019t set alias \"/Users/me/Advance Hazel Rename/Main Stage One image ID_FPR0_53176 .jpg\" to 6.";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,10010 \"Finder\"]>";
OSAScriptErrorAppNameKey = Finder;
OSAScriptErrorBriefMessageKey = "Can\U2019t set alias \"/Users/me/Advance Hazel Rename/Main Stage One image ID_FPR0_53176 .jpg\" to 6.";
OSAScriptErrorMessageKey = "Finder got an error: Can\U2019t set alias \"/Users/me/Advance Hazel Rename/Main Stage One image ID_FPR0_53176 .jpg\" to 6.";
OSAScriptErrorNumberKey = "-10006";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
2019-04-30 21:53:17.488 hazelworker[37944] New Main Stage One image ID_FPR0_53176 .jpg: Rule Advance Rename Testing matched.
2019-04-30 21:53:21.115 hazelworker[37944] New Main Stage One image ID_FPR0_53176 .jpg: Rule New name Green matched.
2019-04-30 21:53:21.153 hazelworker[37944] [File Event] File moved: New Main Stage One image ID_FPR0_53176 .jpg moved from folder /Users/me/Advance Hazel Rename to folder /Users/me/Hazel Renamed.
2019-04-30 21:53:25.181 hazelworker[37944] Done processing folder Advance Hazel Rename


https://www.noodlesoft.com/forums/viewtopic.php?f=4&t=921#

I have attempted my own coding - I have zero coding skills. The code works after a fashion, but I am sure someone can help me get this to where I'd like it to be, in a more intelligent manner.

Code: Select all
tell application "Finder" to activate
tell application "Finder"
   
   display dialog "Add New Details to the current file?" buttons {"Continue", "Quit", "Clear Cache"} default button 3
   
   
   if the button returned of the result is "Continue" then
      
      -- action for 1st button goes here
      
      set currentName to name of theFile
      set the clipboard to «class ktxt» of ((the clipboard as text) as record) # remove styles from the clipboard
      set theCaption to the clipboard
      
      
      set name of theFile to text returned of (display dialog "New Name Details:" default answer theCaption & currentName)
      
      # None = 0 | Orange = 1 | Red = 2 | Yellow = 3 | Blue = 4 | Purple = 5 | Green = 6 | Gray = 7
      
      tell application "Finder"
         set label index of theFile to 6
      end tell
      delay 2 # might give Hazel a chance to move the file if input is matched
      return input
      
      
   else if the button returned of the result is "Quit" then
      
      -- action for 2nd button goes here
      
      tell application "Finder"
         set label index of theFile to 4
      end tell
      
      # set currentName to name of theFile & Failed_ #Unable to get this to work!
      
      return input
      
      tell application "Add New Details:" to quit
      
   else
      
      -- action for 3rd button goes here
      
      tell application "System Events" to set the clipboard to ""
      
      return input
      
      tell application "Add New Details:" to quit
      
   end if
   
end tell


Any assistance on refining this to a fully working version would be truly appreciated.
Stephen
 
Posts: 7
Joined: Sun Jan 22, 2017 9:34 am

This is a bit outside the support I give but I do notice you have a delay there. That won't work since Hazel is waiting for the script to return so it's not doing anything while this is happening.

Also, it appears the error is happening with the " set label index of theFile to 6" file. I'm wondering if it's because you are doing a "tell" to Finder inside a "tell" to Finder. You probably don't need to do the individual "tells" since it's all under the umbrella of a bigger one.

Outside of that, I suggest trying running the script in Script Editor to help debug it.
Mr_Noodle
Site Admin
 
Posts: 11236
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Hi Mr. Noodle, thanks for your guidance. I am making some progress with this and will post my finding here when I have an update.

I think I am at the end of my scripting skill, so will post what I have that works, or if I manage to find a resolution I will post the final version for others who might find the code useful.
Stephen
 
Posts: 7
Joined: Sun Jan 22, 2017 9:34 am


Return to Support