Applescript - automatically renaming the full text attachment. Contribution and question!

Hi all.  

A common feature with EN’s competitors is that they can automatically rename an attachment when it is imported, often to something like “author_year”.  I’ve written my first large AppleScript to do just that.  I attach it below on the condition that no one laughs too hard at it.  It’s beyond clunky.  Select a record in EN, run the script, and watch the magic happen.  It will also need fine-tuning to work for anyone but me, but should be a good start.  Please note I have zero time to refining or explaining it, but please use and abuse as works for you.

I hope someone can answer one question I have with it, however.  Even though I have 

   set  myRecord  to   retrieve  “selected” records in myFile

   repeat   with  myRecordID  in  myRecord

it won’t repeat the renaming process for each selected record if there is more than one.  It works fine for one of them and then just stops.  I’ve tried both “selected” records and “highlighted” records.  In each, AppleScript reports having multiple records in “myRecord”.    Can anyone help me figure out what’s up?  Towards that, what is the difference between “selected” and “highlighted”?

I hope someone finds this useful.

– Verion of 121026

– Renames the attached full document of the selected EndNote record to the form “Jones_1996_114”, where 114 was the record number

– It is horribly ugly and specific to the author, but should be easily adjustable for others.

tell application “EndNote X6”

activate

set myFile to first document

set myRecord to retrieve"selected" records in myFile

repeat with myRecordID in myRecord

–Figure out the current name of the attachment 

set myPDF to field"File Attachments" of record myRecordID

tell me

set myPDF2 to replace_chars(myPDF, “%20”, " ")

set oldDelimiters to AppleScript’s text item delimiters

set AppleScript’s text item delimiters to"/"

set currentName to last text item of myPDF2–We need this to know what to rename

end tell

–Figure out the pieces of the attachment’s new name 

set theAuthor to first word of ( field"Author" of record myRecordID)

set theYear to field"Year" of record myRecordID

tell me – Can’t get this directly from EndNote like author, year, etc

set AppleScript’s text item delimiters to {"""}

set theRecNo to fourth text item of myRecordID

end tell

tell me

set newFileName to theAuthor & “_” & theYear & “_” & theRecNo & “.pdf” as text

end tell

set newFileAttach to"/Users/ghoetker/Dropbox/GPH_test_PDF_storage/" & newFileName as text

set newFileAttach2 to ( the POSIX path of newFileAttach)

set field"File Attachments" of record myRecordID to newFileAttach

–The next section points the record at the soon to be newly named attachment

tell me

set newFileName to theAuthor & “_” & theYear & “_” & theRecNo & “.pdf” as text

end tell

set newFileAttach to"/Users/ghoetker/Dropbox/GPH_test_PDF_storage/" & newFileName as text

set newFileAttach2 to ( the POSIX path of newFileAttach)

set field"File Attachments" of recordmyRecordIDtonewFileAttach-- So the record will point at the renamed attachment

end repeat

end tell

tell application “Finder” – Rename the actual file

set folderPath to ( path to home folder as text) & “Dropbox:GPH_test_PDF_storage” as alias

set name of _file_currentName of folderPath to newFileName

end tell

–A necessary function

on replace_chars(this_text, search_string, replacement_string)

set AppleScript’s text item delimiters to the search_string

set the item_list to every text item of this_text

set AppleScript’s text item delimiters to the replacement_string

set this_text to the item_list as string

set AppleScript’s text item delimiters to""

return this_text

end replace_chars