frimann

Q: Renaming process with applescript/automator

Hi

 

I need an automater workflow or an applescript that will watch a folder called “Finished videos”. When a video file (.mkv or .mp4) gets added to this folder, it should do the following:

 

  1. Create a new folder in the “Finished videos” folder
  2. Rename the new folder identical to the file that was added (minus the extension)
  3. Copy the added file into this new folder
  4. Delete the added file so it ONLY exists in the new renamed folder.

 

Alternatively if it is easier, it would be ok if the new folder gets created in a different “destination” folder. As long as the video file ends up in a folder named idental to itself.

 

No prompts should be shown during the process. It needs to run automataically.

 

Is this even possible? Hope someone can help :-)

 

Kind regards

Jesper

MacBook Pro with Retina display, OS X El Capitan (10.11.4)

Posted on Sep 13, 2016 5:51 AM

Close

Q: Renaming process with applescript/automator

  • All replies
  • Helpful answers

  • by Camelot,

    Camelot Camelot Sep 13, 2016 9:28 AM in response to frimann
    Level 8 (47,285 points)
    Mac OS X
    Sep 13, 2016 9:28 AM in response to frimann

    This is trivial, and actually easier than your workflow implies.

     

    It requires 2 steps, not 4:

     

    1: create a folder with the same name (minus extension) of the file

    2: move the file into the new folder

     

    The trickiest part is extracting the base name of the file (minus the extension), but this should do the trick:

     

    on adding folder items to theFolder after receiving theNewItems

      repeat with eachItem in theNewItems

      set theBaseName to my getBaseNameOf(eachItem)

      tell application "Finder"

      set theNewFolder to make new folder at theFolder with properties {name:theBaseName}

      move eachItem to theNewFolder

      end tell

      end repeat

    end adding folder items to

     

    on getBaseNameOf(thisItem)

      tell application "Finder"

      set ext to name extension of thisItem

      set extLength to number of characters in ext

      set n to characters 1 through -(extLength + 2) of (get name of thisItem) as text

      end tell

      return n

    end getBaseNameOf

  • by frimann,

    frimann frimann Sep 14, 2016 4:45 AM in response to Camelot
    Level 1 (4 points)
    Mac OS X
    Sep 14, 2016 4:45 AM in response to Camelot

    Thanks a lot Camelot for the quick reply

    I'm kind of newbie to this. Could you tell me how to run this script on a given folder? Will you use automator for this or?

    Hope you will help me out here so I can click the "This solved my question" button

     

    Kind regards

    Jesper

  • by Camelot,Solvedanswer

    Camelot Camelot Sep 19, 2016 6:12 AM in response to frimann
    Level 8 (47,285 points)
    Mac OS X
    Sep 19, 2016 6:12 AM in response to frimann

    Since you asked (or, at least, I inferred you were asking for) a Folder Action, you should save this script in your Folder Action Scripts folder in ~/Library/Scripts/

     

    Then, find the folder that you want it to watch, ctrl-click the folder and choose Folder Actions Setup from the Services menu.

     

    From here you can see the folders that have Actions assigned (it may be a very short list). Select your folder and then click the + button below the list of active scripts on the right-hand-side. You should see a list of installed Folder Actions Scripts, including the one you just saved. Select it, and you're done. Dropping a file into this folder should now trigger the script.

     

    A thought did just occur to me, though, that the script might need a little more sanity checking. Since the script creates a new folder to move the file into, the script could re-fire as the new folder looks like a new item that needs to be filtered/moved. An additional check to make sure the dropped file is, indeed, a file that needs to get moved would be a good idea. This updated version ensures the script only triggers on documents, not folders:

     

    on adding folder items to theFolder after receiving theNewItems

      repeat with eachItem in theNewItems

      tell application "Finder"

      if class of eachItem is document file then

      set theBaseName to my getBaseNameOf(eachItem)

      set theNewFolder to make new folder at theFolder with properties {name:theBaseName}

      move eachItem to theNewFolder

      end if

      end tell

      end repeat

    end adding folder items to

     

    on getBaseNameOf(thisItem)

      tell application "Finder"

      set ext to name extension of thisItem

      set extLength to number of characters in ext

      set n to characters 1 through -(extLength + 2) of (get name of thisItem) as text

      end tell

      return n

    end getBaseNameOf

  • by frimann,

    frimann frimann Sep 19, 2016 12:56 AM in response to Camelot
    Level 1 (4 points)
    Mac OS X
    Sep 19, 2016 12:56 AM in response to Camelot

    Thanks again for taking the time to help me out here Camelot :-)

    ****, It's SO close. Your first script fires, but does what you expected and sees the folder as an item and therefore repeats the action.

     

    Your last script unfortunately doesn't do anything :-( I suspect it might be in the if statement it goes wrong,

     

    but I could really use your help to correct it. I hope you will help me one last time here. I would really appreciate it :-)

     

    Kind regards

    Jesper

  • by frimann,Helpful

    frimann frimann Sep 19, 2016 6:12 AM in response to Camelot
    Level 1 (4 points)
    Mac OS X
    Sep 19, 2016 6:12 AM in response to Camelot

    I figured out the problem. This works:

     

    on adding folder items to theFolder after receiving theNewItems

      repeat with eachItem in theNewItems

      tell application "System Events"

      set itemInfo to properties of disk item ((eachItem) as string)

      if class of itemInfo is not folder then

      tell application "Finder"

      set p to theFolder as alias

      --set theBaseName to name of (info for eachItem) as string

      --make new folder at p with properties {name:"New Folder"}

      set theBaseName to my getBaseNameOf(eachItem) as string

     

      set theNewFolder to make new folder at p with properties {name:theBaseName}

      move eachItem to theNewFolder

      --display dialog theBaseName buttons "OK" default button "OK"

      end tell

      end if

      end tell

     

      end repeat

    end adding folder items to

     

    on getBaseNameOf(thisItem)

      tell application "Finder"

      set ext to name extension of thisItem

      set extLength to number of characters in ext

      set n to characters 1 through -(extLength + 2) of (get name of thisItem) as text

      end tell

      return n

    end getBaseNameOf


    Thanks a lot Camelot :-)


    Ps. Sorry, couldn't figure out how to format the text into applescript syntax :-(