Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm scanning my mail and end up with two versions: front & back.

Files are named: scanf 01.pdf & scanb 01.pdf

I'm scanning about 40 pages a day, so it ends up to quite some lists.

Now, with automator, I've done the following:

  1. Get Selected Finder Items # "scanf 01.pdf" & "scanb 01.pdf"
  2. Combine PDF Pages
  3. Move Finder Items # to Desktop
  4. Name Single Item # so that its not a random name, but "Scan.pdf"
  5. Add Date or Time # "Scan 2013-01-07.pdf"
  6. Make Sequential # "Scan 2013-01-07 01.pdf" (is at least the idea)
  7. Label Finder Items # Adds a red label, so I didnt see it yet

Step 6 is not working, as the sequence starts over every time I select two new files. So it says 'name already exists', after that it stops.

Questions:

  1. How can I make step 6 work?
  2. Is there something that automatically merges files, so that Scanf 01.pdf is merged with Scanb 01.pdf and that Scanf 02.pdf is merged with Scanb 02.pdf, and so on. As my current automator setup requires me to select the two files.
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

I didn't really test this, but you could use a shell script instead.

#!/bin/bash

join="/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"
date=$(date '+%Y-%m-%d')
last=$(ls ~/Desktop/$date*.pdf 2> /dev/null | tail -n1 | sed -E 's/.* 0*(.*)\.pdf$/\1/g' || 0)

cd ~/Documents/Scanned/
for f in scanf*.pdf; do
    ((last++))
    output="$HOME/Desktop/$date $(printf %03d $last).pdf"
    python "$join" -o "$output" "$f" "${f/scanf/scanb}"
    osascript -e 'on run {f}
tell app "Finder" to set label index of (posix file f as alias) to 2
end' "$output"
done
share|improve this answer
    
Thanks a bunch! It works!! –  Maurice Kroon Jan 7 '13 at 21:50
add comment

The reason you're running into unexpected "file already exists" errors is that the Make Sequential action only works for single iterations of the workflow. If you run the workflow twice, it will fail the second time, because the counter resets and it tries to start the numbering at 1. So if you already have an item named Scan 2013-01-07 01.pdf, it tries to make another named that and fails.

The workaround is to use a script to do the renaming for you, which can detect if there are already duplicate files and start over.

In place of the Make Sequential action, add a Run Shell Script action, with the shell set to /usr/bin/python, the input to as arguments, and paste in this script:

#!/usr/bin/python

import sys
import os

decimalPadding = 2 # pad to x decimal places
returnFiles = ''

for filePath in sys.argv[1:]:
    pathNoExt, extension = os.path.splitext(filePath)
    counter = 1
    newPath = pathNoExt + ' %0*d' % (decimalPadding, counter) + extension

    #     Increment counter until we find a file that does not exist
    while os.path.exists(newPath):
        counter += 1
        newPath = pathNoExt + ' %0*d' % (decimalPadding, counter) + extension
    os.rename(filePath, newPath)
    returnFiles += newPath + '\n'

print returnFiles #pass the renamed files for other automator actions

That should get you the results you desire.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.