Script to edit a file name

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Script to edit a file name Mon Sep 30, 2019 8:42 am • by Dave61
Intro
I wrote this script for a specific purpose but the basic structure could be used to do anything where you need to run a script on a file.

I am no script expert; this is only my fourth or fifth script and it is by far the most complex. There was liberal use of the internet, a gradual build-up, use of Display Dialog commands to show progress, and much trial and error. In total it took about 4 hours, including writing this up.

If I can do it you can.

I should add that it can probably be made slicker by eliminating some interim steps, for example creating the variable newName, but this suited my step by step approach.

Background
I have a load of files where I want to insert a hyphen as a separator. By eye it is easy to see where it should be, but there is no programmatic way to do identify the right place. Obviously I could do it manually, but that gets old very quickly.

Solution
I started marking the files to be adjusted by using a set of tags /1, /2, … /n, but I needed a separate rule in Hazel for each tag, which is rather clunky. So this morning I set out to write an embedded Applescript that could take /n as an input and do the work instead.

This is the Hazel test. It will recognise any tag in the form /number
Image

This is the script (code for copying below)
Image

Here is the test set up in "before" state
Image

Here it is after running
Image
Note that the /tag that called it has been removed too, otherwise the rule will keep triggering

The command line tool "Tag" can be found here: https://github.com/jdberry/tag

The embedded code is:
Code: Select all
tell application "Finder"
   #set up variables
   set theName to name of (info for theFile)
   set theCount to item 1 of inputAttributes
   set thePos to 0
   
   #prep the tids
   set astid to AppleScript's text item delimiters
   set AppleScript's text item delimiters to " "
   
   #find the Nth space   
   if (count theName's text items) > theCount then
      set thePos to (count text from text item 1 to text item theCount of theName) + 1
   end if
   
   #restore tids
   set AppleScript's text item delimiters to astid
   
   #act on the result   
   if thePos is greater than 0 then
      #rename the file
      set theName1 to text 1 thru thePos of theName
      set theName2 to text thePos thru length of theName
      set the name of theFile to theName1 & "-" & theName2
      
      #prep the shell command   
      set theTag to "/" & theCount
      set newName to theName1 & "-" & theName2
      set cmd to "/usr/local/bin/Tag -r" & space & quote & theTag & quote & space & quote & newName & quote
      
      #do the shell command
      do shell script cmd
      
   end if
   
end tell
Dave61
 
Posts: 113
Joined: Tue Jul 10, 2012 4:56 pm

Return to Tips & Tricks - DO NOT POST QUESTIONS