The “Append Selected Text”/“Title Append” internal script is a renaming script that can be very useful. For example, say I have a web receipt document open, and I want to add the date or amount to the name. Just select the date or amount, and do Append Selected Text.
But if the document has an extension, it fails. For example, if the document is “Some web receipt.html”, and I’ve selected “$46.32” in the document, Append Selected Text changes the name to “Some web receipt.html $46.32”. What we actually want is “Some web receipt $46.32.html”
The fix is to find the filename extension and insert the selected text before the extension.
-- Append To Title (filename extension bug fixed)
-- Created by Christian Grunenberg on Aug 10 2009.
-- Copyright (c) 2009. All rights reserved.
set defaultDelimiters to AppleScript's text item delimiters
tell application id "com.devon-technologies.thinkpro2"
try
if not (exists think window 1) then error "No window is open."
if not (exists content record) then error "Please open a document."
set theText to selected text of think window 1 as string
if theText is missing value or theText is "" then error "No text is selected."
set oldName to name of content record
set newName to oldName & " " & theText --will use this if there is no extension
if oldName contains "." then
set AppleScript's text item delimiters to "."
set nameExtension to the last text item of oldName
set AppleScript's text item delimiters to defaultDelimiters
if nameExtension does not contain " " then
--Insert text before extension
set newName to ((characters 1 through -((length of nameExtension) + 2) of oldName) as string) & " " & theText & "." & nameExtension
end if
end if
set name of content record to newName
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
end try
end tell