Rename with Prefix and/or Suffix

There are two scripts that ship with DEVONthink: Scripts > Rename > Add Prefix…, and Scripts > Rename > Add Suffix…. I frequently want to add both prefixes and suffixes, so I’ve consolidated the script into the following script to Add Prefix and/or Suffix to selected items. Thanks to Criss for the originals.

Tip: I use the renaming scripts with TextExpander, which makes adding custom date-time prefixes quite easy. The script below could also be slightly modified to be used inside a TextExpander (or other expansion utility) snippet, or Keyboard Maestro, etc.

-- Add Prefix and / or Suffix to selected items
-- created 20131102 by korm
-- Based on Create Prefix and Create Suffix Scripts
-- Created by Christian Grunenberg Sat May 15 2004.
-- Copyright (c) 2004-2009. All rights reserved.
-- Based on (c) 2001 Apple, Inc.

tell application id "com.devon-technologies.thinkpro2"
		set this_selection to the selection
		if this_selection is {} then error "Please select some contents."
		set prefix to ""
		set suffix to ""
		set {p_click, prefix} to {button returned, text returned} of (display dialog "Enter Prefix (plus Space if Needed)" default answer "" buttons {"Skip Prefix", "OK", "Cancel Script"} default button 3 cancel button 3 with title "Add Prefix and/or Suffix")
		set {s_click, suffix} to {button returned, text returned} of (display dialog "Enter Suffix" default answer "" buttons {"Skip Suffix", "OK", "Cancel Script"} default button 3 cancel button 3 with title "Add Prefix and/or Suffix")
		repeat with this_item in this_selection
			set current_name to the name of this_item
			set new_item_name to prefix & current_name & suffix
			set the name of this_item to new_item_name
		end repeat
	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

It would be ideal to have the dialog in one panel, but that requires either a custom scripting addition (OSAX), which you probably do not have, or building the app in AppleScript Studio, which is daunting for small tasks. Hence, the two step dialog in this script.