Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

AppleScript/Automator: renaming PDF with extracted text content of this PDF

I have a lot of PDFs that I want to rename. The new name should be a text item of this PDF. I wan t to create this as a folder action to drop PDF in and get them renamed. I've com so far with Automator and AppleScript:

The whole text form the PDF is extracted to a new (temporary) file on desktop "PDF2TXT.rtf" (= variable theFileContents), done with the Automator action to extract text from PDF.

Here comes the code that separates the new name out of this text:

on run {theFileContents}

set od to AppleScript's text item delimiters
set theFileContentsText to read theFileContents as text
set myProjectNo to ""
set theDelimiter to "Projectno. "
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theArray to last text item of theFileContentsText
set theDelimiter to " "
set AppleScript's text item delimiters to theDelimiter
set myProjectNo to first text item of theArray
return myProjectNo
set AppleScript's text item delimiters to oldDelimiters

end run

What I need now is the rest to rename the input PDF to "myProjectNo".

Happy for any suggestion. Thanks in advance.

q3player

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this :

tell application "Finder" to set name of foo to bar

the script to save as droplet (= application) :

on run -- you can run the applet it will prompt for files
    open {choose file of type "pdf"}
end run
on open (thesefiles) -- you cn drop files on this applet
    repeat with theFileContents in thesefiles
        -- set od to AppleScript's text item delimiters -- duplicate : same as oldDelimiters
        set theFileContentsText to read theFileContents as text
        set oldDelimiters to AppleScript's text item delimiters
        set AppleScript's text item delimiters to "Projectno. "
        set theArray to last text item of theFileContentsText
        set AppleScript's text item delimiters to space
        set myProjectNo to first text item of theArray
        -- return myProjectNo -- and never come back again ;-)
        set AppleScript's text item delimiters to oldDelimiters -- this can't go after the return ( = never called)…
        set newPDFname to "Project No" & space & myProjectNo & ".pdf"
        tell application "Finder"
            display dialog "You really want to renamme this file to " & return & newPDFname & " ?"
            set the name of thisFile to newPDFname
        end tell
    end repeat
end open

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...