Previous  |  Main  |  Next
AppleScript Logo

Rename Files of Path List

Script for: BBEdit
Copyright: Matthias Steffens, use at your own risk!
Description: If one or more file paths (e.g. obtained by the "Open Special" script)
are selected in the front window of BBEdit, starting this script once
will cause the script to remember the original file names. Then have fun
renaming the files within BBEdit and with the convenience of grep etc!

When you're done, select exactly(!) the same paths and start this script
again - all files will be renamed accordingly!

Note: The "Grep Rename" script provides another solution that lets
you batch rename files with grep.
Necessary: None
Download: RenameFilesOfPathList_v1.2.hqx (for BBEdit 6.x)
RenameFilesOfPathList_v1.1.hqx (for BBEdit 5.x)
History: v1.2  -  works now with BBEdit 6.x (but not with BBEdit 5.x anymore -- use v1.1 together with BBEdit 5.x)
v1.1  -  code was re-written, which resulted in the following enhancements:
  • removed requirement for "ACME Script Widgets 2.5" scripting addition
  • transformed global variables into locals in order to clarify the code
v1.0  -  original script posted at this site
The Script:
property OrigFileList : 0
property NumberOrigPaths : 0

global Die


set Die to false

if OrigFileList is 0 then
	my GetOrigPaths()
else
	my GetNewPaths()
end if


on GetOrigPaths()
	set {Die, OrigPaths, NumberOrigPaths} to my MakePaths()
	if not Die then
		set {Die, fileList} to ResolvePaths(OrigPaths, NumberOrigPaths)
		if not Die then
			set OrigFileList to fileList -- an alias list
			display dialog "You can now rename the files, then start this script again." with icon note buttons "OK" default button 1
		end if
	end if
end GetOrigPaths


on GetNewPaths()
	display dialog "Have you renamed your files and *exactly* selected the same paths " & ¬
		"again?" & return & return & "(Press \"No, forget about it!\" to discard any " & ¬
		"remembered information.)" with icon caution buttons {"Cancel", "No, forget about it!", "Yes, rename files!"} default button 3
	if button returned of result is not "Cancel" then
		if button returned of result is "Yes, rename files!" then
			set {Die, NewPaths, NumberNewPaths} to my MakePaths()
			if not Die then
				if NumberNewPaths ≠ NumberOrigPaths then
					display dialog "You have to select exactly the same paths again in order to use this script!" with icon caution buttons "OK" default button 1
				else
					set ct to my RenamePaths(NewPaths, NumberNewPaths)
					set OrigFileList to 0
					display dialog "Number of files renamed:    " & ct with icon note buttons "OK" default button 1
				end if
			end if
		else -- user pressed "No, forget about it!"
			set OrigFileList to 0 -- discard any remembered information
		end if
	end if
end GetNewPaths


on ResolvePaths(thePaths, NoOfPaths)
	set fileList to {}
	try
		tell application "Finder"
			repeat with i from 1 to NoOfPaths
				set theFile to item i of thePaths as alias
				copy theFile to end of fileList
			end repeat
		end tell
	on error
		display dialog "Couldn't resolve all paths!" & return & return & ¬
			"You have to select the paths properly! Then start the script again." with icon caution buttons "OK" default button 1
		set Die to true
	end try
	return {Die, fileList}
end ResolvePaths


on MakePaths()
	tell application "BBEdit 6.0"
		activate
		if not (exists text window 1) then -- BBEdit 5.x: "if not (exists window 1) then"
			display dialog "No open document!" with icon caution buttons "Oops" default button 1
			set Die to true
			set {PathsToProcess, NoOfPaths} to {{}, 0}
		else
			set TheItems to selection of text window 1 as string -- BBEdit 5.x: "set TheItems to selected text"
		end if
	end tell
	
	if not Die then
		if TheItems is "" then -- nothing selected
			display dialog "You have to select one or more file paths in order to use this " & ¬
				"script!" with icon caution buttons "OK" default button 1
			set Die to true
			set {PathsToProcess, NoOfPaths} to {{}, 0}
		else
			-- split paragraphs:
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {return}
			set TheItems to text items of TheItems
			set PathsToProcess to {}
			repeat with theItem in TheItems
				if theItem as string is not "" then
					copy theItem as string to end of PathsToProcess
				end if
			end repeat
			set AppleScript's text item delimiters to oldDelims
			-- end split paragraphs
			
			-- instead of the "split paragraphs"-code above you might want to use the "ACME Script Widgets 2.5"
			-- scripting addition, which handles the problem within one line:
			-- set PathsToProcess to tokenize TheItems with delimiters {return} without null tokens -- uses ACME Script Widgets 2.5
			
			set NoOfPaths to count PathsToProcess
		end if
	end if
	return {Die, PathsToProcess, NoOfPaths}
end MakePaths


on RenamePaths(NewPaths, NumberNewPaths)
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	tell application "Finder"
		set ct to 0
		repeat with i from 1 to NumberNewPaths
			set NewFileName to last text item of item i of NewPaths
			if (NewFileName ≠ (name of item i of OrigFileList)) then
				set name of item i of OrigFileList to NewFileName
				set ct to ct + 1
			end if
		end repeat
	end tell
	set AppleScript's text item delimiters to oldDelims
	return ct
end RenamePaths

 


Contact: Matthias Steffens  |  Previous  |  Main  |  Next  |  Last Updated: 24-Sep-00