Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...