Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hm_2019

macrumors newbie
Original poster
Jan 16, 2019
8
1
Hi,

I have been looking through tutorials and forums looking for a way to create an automation that will locate the PDF name in the subfolder and rename the parent folder with the same name.

The automation will need to be able to work with batches of files and the variable data of the unique folder and PDF names.

This is my first attempt at using Automation, i've never come across a need for it. It seems like there are a lot of options, but I can't seem to find the correct action order to rename the folder from the file name.

Working on a MacBook Pro / El Capitan

Thank you.

Just realized the screen shots didn't attach to request. There are now screenshots of the original and what is needed.

Original:
original folder naming.png


Needed:
required folder naming.png
 

Attachments

  • image.png
    image.png
    24.6 KB · Views: 247
Last edited:
Hi and welcome to the forum. It would be nice if you could post your Automator workflow eg the actions it uses. Below is one of many possible solutions to your problem or it would be a starting point for you to build on. Open Script Editor and paste :

Applescript example:

Code:
-- Choose a file ex: 987654321.pdf
set aFile to choose file
-- get extension
tell application "System Events" to set fileNameExt to aFile's name extension
-- get basename without the extension
set baseFileName to do shell script "basename -s ." & fileNameExt & space & quoted form of POSIX path of aFile
tell application "System Events"
    --name of container of aFile's container
    -- rename the parent folder
    set name of container of aFile's container to baseFileName
end tell

Result/Replies( NOT to be pasted in Script Editor) :

Code:
tell application "Script Editor"
    choose file
        --> alias "VMWareYosemite:Users:kryten:Documents:GP1234567:PDF review:987654321.pdf"
end tell
tell application "System Events"
    get name extension of alias "VMWareYosemite:Users:kryten:Documents:GP1234567:PDF review:987654321.pdf"
        --> "pdf"
end tell
tell current application
    do shell script "basename -s .pdf '/Users/kryten/Documents/GP1234567/PDF review/987654321.pdf'"
        --> "987654321"
end tell
tell application "System Events"
    set name of container of container of alias "VMWareYosemite:Users:kryten:Documents:GP1234567:PDF review:987654321.pdf" to "987654321"
end tell
 
Last edited:
Hi, thank you for the offer to help. I really thought I could figure this out on my own with some research but I just keep getting lost in my attempts. The code you provided was very helpful to understand what I was asking from AppleScripts but there still seems to be a part missing from my understanding.

I copied the script in Script Editor and also in Automator both ending up with the same results.

When I run the script I am prompted to navigate to the pdf location inside the folder structure before the parent folder will rename.

The action that I am hoping to achieve would be to drag the original folder into an Automator App on my desktop that would automatically rename it, without the navigating. In addition to this I am hoping to be able to rename a whole batch of folders at the same time.

Screen Shot 2019-01-18 at 1.02.04 AM.png

Screen Shot 2019-01-18 at 1.03.06 AM.png
Screen Shot 2019-01-18 at 1.04.23 AM 1.png
 
Based on your screenshot that the parent folder has a sub folder "PDF review" with a PDF file in it.

Automator :

  • Create application with the following actions :
  • Run AppleScript : replace code with:
Code:
on run {theFolders, parameters}
    (* Your script goes here *)
    repeat with i from 1 to number of items in theFolders
        set parentFolder to item i of theFolders
        tell application "System Events"
            if kind of parentFolder is "Folder" then
                set {fileNameExt, filePosixPath} to {name extension, POSIX path} of (first item of folder "PDF review" of parentFolder whose name extension is "pdf" or kind is "Portable Document Format (PDF)")
                set name of parentFolder to my baseFileName(fileNameExt, filePosixPath)
            end if
        end tell
    end repeat
    --return input
end run

on baseFileName(fileExt, filePath)
    do shell script "basename -s ." & fileExt & space & quoted form of filePath
end baseFileName
  • save application
Drop your parent folder(test,987654321) onto the application.
 
Oh wow :) thank you so much! This is working perfectly and will save us so much time! I really appreciate your help.
 
  • Like
Reactions: superscape
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.