Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Workflow or Applescript to Rename basename from contents of self

Hello Everybody,

I want to merge physical SAT flash cards into an Evernote Peek notebook for my girlfriend on her iPad mini. I have a box of 500 cards and a ScanSnap that'll duplex scan the cards pretty nicely. The front of each card is a word, and the back is the definition/part of speech.


Evernote sees the basename of the files I upload to it as the vocab word/hint and the content of the file as the definition/answer.


My first thought was to use the OCR from ABBYY then use Automator "Extract PDF text" and make a plain text file with the same basename for each card, but the scansnap makes the filename a date and time string. So I need a script that will open the files individually and return a string from the first line delimited by the return stroke or spaces. Then rename the file by that string, close the file and keep instancing through the files.


Can anyone help with the script? I'm not too handy with the scripting these days. If not, I'll see if I can decipher some tutorials on teh Google.


Thank you in advance,

Derek

OS X Mountain Lion (10.8.2)

Posted on Mar 7, 2013 1:11 PM

Reply
Question marked as Best reply

Posted on Mar 7, 2013 1:43 PM

Simple enough in principle:


set theFiles to choose file with multiple selections allowed


repeat with thisFile in theFiles

set theName to first paragraph of (read thisFile)

tell application "System Events"

set ext to name extension of thisFile

set name of thisFile to theName & "." & ext

end tell

end repeat

13 replies
Question marked as Best reply

Mar 7, 2013 1:43 PM in response to aboveamenity

Simple enough in principle:


set theFiles to choose file with multiple selections allowed


repeat with thisFile in theFiles

set theName to first paragraph of (read thisFile)

tell application "System Events"

set ext to name extension of thisFile

set name of thisFile to theName & "." & ext

end tell

end repeat

Mar 7, 2013 3:09 PM in response to aboveamenity

Hmph. I usually put my money on RedMenace.


I didn't realize that this was already going to be part of an automator workflow. The Extract PDF Text action returns a list of the text files, so there's no need to ask for them. just run the repeat loop on the action input:


on run {input, parameters}


repeat with thisFile in input

set theName to first paragraph of (read thisFile)

tell application "System Events"

set ext to name extension of thisFile

set name of thisFile to theName & "." & ext

end tell

end repeat


return input

end run

May 4, 2014 5:01 PM in response to aboveamenity

sooo close!


Your renaming scripts works great for text files, but if I extract .rtf, then the first paragraph is this or similar: \rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360


And I want to end up with .rtf.


I'd be happy for either applescript or automator results.

I guess automator would be best, because then I could use the pdf extraction script but extract to rtf and continue from there.

May 4, 2014 7:25 PM in response to sedaray

sedaray wrote:


...renaming scripts works great for text files, but if I extract .rtf, then the first paragraph is this or similar: \rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360


You can use textutil to read as a txt file


Change:


settheNametofirstparagraphof (readthisFile)


to:


settheNametofirstparagraphof (do shell script "textutil -convert txt -stdout " & quoted formofPOSIX pathofthisFile)


Dec 1, 2014 8:16 AM in response to Strax

Instead of the Run Applescript Workflow, use this Run Shell Script Workflow:

(Note: Select the Rich Text option in the Extract PDF Text Workflow)


User uploaded file



for f in "$@"

do

path=${f%/*}

newname=$( textutil -convert txt -stdout "$f" | head -n1 )

newname=${newname//\// - } # replace '/' with ' - '

mv "${f%.*}.pdf" "$path/$newname.pdf"

rm "$f"

done

Jul 6, 2015 6:16 AM in response to Tony T1

Hi Tony T1,


Thanks for your help with this problem!


I noticed that your workflow only works if the original pdf to be processed is on the desktop.

I modified it to work with pdfs in any location by storing the path of the original pdf in a variable and recalling it before executing a modified version of your shell script. (However this version of the workflow only processes files one at a time)


Anyone, please let me know if you see any errors, or avenues of improvement!



Here's the modified script I used:


path=${2%/*}

newname=$( textutil -convert txt -stdout "$1" | head -n1 )

newname=${newname//\// - } # replace '/' with ' - '

mv "$2" "$path/$newname.pdf"

rm "$1"


Here's the workflow:

User uploaded file

Oct 6, 2015 4:33 AM in response to aboveamenity

Hi there,


I realize this is a old post, but I'm trying to do that as well. The shell script of jmtennant works wonderfully for me, except that I can only do one file at a time. When selecting more PDF to rename, no files get renamed.


It's probably a very simple trick to make sure it does allow multi-selection, but I am really a beginner with automator.😊

Workflow or Applescript to Rename basename from contents of self

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.