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 proven to myself again that I am not a programmer. I have found numerous posts with similar questions but I can’t seem to wrap my head around it all to make anything fit for my needs.

Thank you in advance!

I am trying to rename groups of PDFs from a .csv file.

Working on a MacBook Pro / El Capitan

The original .pdf name is long and has more characters than the .csv reference.
The .csv file has many unneeded columns of information.
I don’t want to have to alter the .csv or original .pdf name.

The Folder Action:
move the .csv and .pdfs into the folder

Reference .csv column header:
Export.csv, column A, GP Number, GP1234567

Match to original PDF name:
GP1234567-B-ST344005-2018-11-1-752.pdf

Reference .csv column header:

Export.csv, column H, Code, 987654321

Rename PDF: 987654321.pdf

Screen Shot 2019-02-10 at 4.03.36 AM.png
Screen Shot 2019-02-10 at 4.06.45 AM.png
 

Toutou

macrumors 65816
Jan 6, 2015
1,079
1,573
Prague, Czech Republic
Alright, try this.
Take the following code:
Code:
require 'csv'
require 'fileutils'

codes = {}

CSV.foreach("Export.csv", {headers: true}) do |row|
  codes[row[0]] = row[7]
end

files = Dir.glob "*.pdf"

unless File.directory? "result"
  FileUtils.mkdir "result"
end

files.each do |file|
  gp_number = file.split("-")[0]

  unless code = codes[gp_number]
    puts "#{gp_number} from \"#{file}\" not found in Export.csv, skipping"
    next
  end

  puts "#{gp_number} found, copying to result/#{code}.pdf"
  FileUtils.cp file, "result/#{code}.pdf"
end

puts "\nAll done!"

and put it into a file called rename.rb
It has to be a plain text file, i.e. not ".doc" or ".rtf". TextEdit can be set to plain text mode if you're not sure.
edit: or just rename the attached file from .txt to .rb

The .rb extension is used by the Ruby programming language, which is what the code is written in. Ruby comes preinstalled in macOS, albeit a pretty old version.

Put the file in a folder together with your Export.csv (the name is hardcoded in the script, you can change that if needed) and with your pdf files, like this:
1.png

Then open Terminal. Type cd, then a space, then a path to the folder with your files. You can type the path manually or just drag and drop the folder into the terminal window.
2.png
Press Enter, the terminal thingie will jump into the folder and await further instructions.
Type ruby rename.rb
This tells the terminal to run the file as Ruby code.
After the script finishes, there should be a new folder result right next to the files, with the renamed COPIES inside. 3.png
 

Attachments

  • rename.txt
    522 bytes · Views: 314
Last edited:

hm_2019

macrumors newbie
Original poster
Jan 16, 2019
8
1
Hi Toutou, This is working, thank you for your help.
Next step: move this to a method that won't need the terminal.
When I drag the 'Review PDFs' folder into the Automator App on my desktop, there is an error.
Would you be able to tell me what I'm missing?
Screen Shot 2019-02-11 at 3.57.39 PM.png
 

Toutou

macrumors 65816
Jan 6, 2015
1,079
1,573
Prague, Czech Republic
Okay, I've never even touched Automator, but try this: (works for me)

Start with Run AppleScript, put this into the field:
Code:
return POSIX path of (path to me)
This is needed because the Ruby script would, by default, run in your home directory, instead of where the .app file is.

Then Run Shell Script (/usr/bin/ruby, set Pass input to as arguments), with this content:
Code:
require 'csv'
require 'fileutils'

script_dir = Dir.new ARGV[0]
parent_dir = File.expand_path "..", script_dir
Dir.chdir parent_dir

codes = {}

CSV.foreach("Export.csv", {headers: true}) do |row|
  codes[row[0]] = row[7]
end

files = Dir.glob "*.pdf"

unless File.directory? "result"
  FileUtils.mkdir "result"
end

files.each do |file|
  gp_number = file.split("-")[0]

  unless code = codes[gp_number]
    puts "#{gp_number} from \"#{file}\" not found in Export.csv, skipping"
    next
  end

  puts "#{gp_number} found, copying to result/#{code}.pdf"
  FileUtils.cp file, "result/#{code}.pdf"
end

puts "\nAll done!"

Notice the new lines, there's three of them, just after the requires.
The first line creates a Ruby Dir object from the path returned by the AppleScript (the .app directory).
The second line gets the parent directory (the actual directory your files are in).
The third line changes the current working directory to the one from the second line.
The rest of the script stays the same.

Maybe add Display Notification to know when it's done.

Put the .app right next to the original .rb file and use it the same way.
 

hm_2019

macrumors newbie
Original poster
Jan 16, 2019
8
1
I am having no luck. What am I doing differently than your run test?
Screen Shot 2019-02-12 at 2.57.04 PM.png
 

Toutou

macrumors 65816
Jan 6, 2015
1,079
1,573
Prague, Czech Republic
First, the Apple Script part: this is enough, you don't need to specify the whole on run handler.
as.png

Second, double check the Ruby part. It looks like you've been doing some tinkering with my code (the ARGV.each do on your previous screenshot), and you might have left an end keyword right at the, well, end. At least it's what the error message is telling — it was expecting the script to end, but encountered an end keyword Try erasing everything, then just copy and paste my code.
 
Last edited:

sir_naggs

macrumors newbie
Mar 20, 2020
4
2
Alright, try this.
Take the following code:
Code:
require 'csv'
require 'fileutils'

codes = {}

CSV.foreach("Export.csv", {headers: true}) do |row|
  codes[row[0]] = row[7]
end

files = Dir.glob "*.pdf"

unless File.directory? "result"
  FileUtils.mkdir "result"
end

files.each do |file|
  gp_number = file.split("-")[0]

  unless code = codes[gp_number]
    puts "#{gp_number} from \"#{file}\" not found in Export.csv, skipping"
    next
  end

  puts "#{gp_number} found, copying to result/#{code}.pdf"
  FileUtils.cp file, "result/#{code}.pdf"
end

puts "\nAll done!"

and put it into a file called rename.rb
It has to be a plain text file, i.e. not ".doc" or ".rtf". TextEdit can be set to plain text mode if you're not sure.
edit: or just rename the attached file from .txt to .rb

The .rb extension is used by the Ruby programming language, which is what the code is written in. Ruby comes preinstalled in macOS, albeit a pretty old version.

Put the file in a folder together with your Export.csv (the name is hardcoded in the script, you can change that if needed) and with your pdf files, like this:
View attachment 821110

Then open Terminal. Type cd, then a space, then a path to the folder with your files. You can type the path manually or just drag and drop the folder into the terminal window.
View attachment 821112
Press Enter, the terminal thingie will jump into the folder and await further instructions.
Type ruby rename.rb
This tells the terminal to run the file as Ruby code.
After the script finishes, there should be a new folder result right next to the files, with the renamed COPIES inside. View attachment 821113


Hey Toutou,

I'm trying to use your ruby script to convert some file names. Does it work if I change the .pdf to .jpg in the code? I tried doing this but always get the same output in terminal "test.jpg from "test.jpg" not found in data.csv, skipping"

Triple checked the name in my .csv file but just can't figure out what I'm doing wrong here. Tried half a dozen different scripts in automator and terminal, and even though they run successfully they never work. Feel like there is something I am missing about the csv. saved from excel as UTF-8 Any thoughts?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.