Simple string manipulation and file renaming

I have some file with time stamps at the beginning, as in 1.23foobar, 12.34barfoo, located in folder Macintosh HD : MyFolder. I want to rename the files so all end in “.ps”, and have the 1.23 and 12.34 time stamps stripped from their starts. Here’s my attempt:

set MyFolder to “Macintosh HD:MyFolder”
tell application “Finder”
set rawFileList to name of every file in folder MyFolder
display dialog "List = " & rawFileList – for debug

repeat with finderItem in rawFileList		
	set nameLength to count of characters in name of finderItem
	set remainingLength to 0
	if character 2 of alias finderItem is "." then
		set remainingLength to nameLength - 4
	else if character 3 of alias finderItem is "." then
		set remainingLength to nameLength - 5
	end if
	display dialog "Length = " & remainingLength   -- for debug
	
	--set newFinderItem to (last remainingLength characters of finderItem) & ".ps"
	--display dialog newFinderItem
	--rename finderItem to newFinderItem		
end repeat	

end tell

Note that I’ve comment out the actions I want to perform near the end of the script, as I get syntax error from the first one, and since I can’t even get the nameLength after the start of my repeat, as I “can’t get name of ‘foobar’” …and yet ‘foobar’ is the name I’m trying to get. If I strip the “name of” from that line, the error is “expected a reference”.

Hi,

There is mainly 2 ways to do this. First get a list of references or get a list of names (what you are doing). With your choice, you don’t need a reference until you rename the files. Something like this:

set MyFolder to “Macintosh HD:MyFolder:” – added colon because “MyFolder” is a folder
tell application “Finder”
set rawFileList to name of every file in folder MyFolder
display dialog "List = " & rawFileList – for debug

repeat with finderItem in rawFileList
	set nameLength to length of finderItem -- finderItem is a string and not a reference
	set remainingLength to 0
	if character 2 of finderItemReference is "." then
		set remainingLength to nameLength - 4
	else if character 3 of finderItemReference is "." then
		set remainingLength to nameLength - 5
	end if
	display dialog "Length = " & remainingLength -- for debug 
	set newFinderItem to (text -remainingLength thru -1 of finderItem) & ".ps"
	display dialog newFinderItem
	set finderItemReference to (MyFolder & finderItem) as alias -- create a reference
	set name of finderItemReference to newFinderItem
end repeat

end tell

I didn’t test it, but it looks right at a glance.

gl,

Try this:

property myFolder : "Macintosh HD:MyFolder:"

try
	tell application "Finder"
		activate
		get name of every file in myFolder
		
		repeat with thisItem in result
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "."
			set newName to (text items 2 through -1 of thisItem) as text
			set AppleScript's text item delimiters to ASTID
			
			get ((characters 3 through -1 of newName) as text) & ".ps"
			set name of file thisItem of myFolder to result
		end repeat
	end tell
on error errorMsg number errorNum
	display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try

Edit: Added colon to path (just to be proper).

Fixed it up a little with offset:

set MyFolder to “Macintosh HD:MyFolder:” – added colon because “MyFolder” is a folder
tell application “Finder”
set rawFileList to name of every file in folder MyFolder
display dialog "List = " & rawFileList – for debug
repeat with finderItem in rawFileList
set pOffset to offset of “.” in finderItem
if pOffset is 2 or pOffset is 3 then
set newFinderItem to (text (pOffset + 1) thru -1 of finderItem) & “.ps”
display dialog newFinderItem
set finderItemReference to (MyFolder & finderItem) as alias – create a reference
set name of finderItemReference to newFinderItem
end if
end repeat
end tell

Hi Guardian,

Why didn’t you type faster? I could have taken a bath. :slight_smile:

gl,

:stuck_out_tongue:

Well, guardian34’s script wouldn’t get the names of the files, so no joy with that one. Too bad, 'cause it looked soo elegant! Kel’s worked pretty well (once I changed the (pOffset + 1) to (pOffset + 3) to skip over the minutes portion of the time stamp prepended to my filenames.

The next item I have to deal with here is (1) go through this file list in order of oldest to youngest files, and (2) to allow overwriting an existing file. Reason: my file folder may well contain two versions of the same file, with the later one being the updated or corrected version. They’ll have different names in this folder, because the printing routine prepends a time stamp. But, when I strip the time stamp, they’d get the same name . . . and I want to overwrite the earlier one with the later one.

I assume this will require (1) making an array of not just the file names but also including the creation date/time of each file, then (2) sorting this rawFileList by file creation date/time, then (3) iterating through it to come up with the new names. Finally, (4) delete a file by this new name if it already exists, then rename the current one to this new name.

I can figure out step 4 (duh!), but I’ve not worked with arrays.

If my script isn’t working, you might need to change this line.

get name of every file in myFolder

to this (adding the term folder):

get name of every file in folder myFolder

This script will go through the newest files first. If an older file would have the same name after renaming, this will cause an error. If this error is received, the older file is deleted.

property myFolder : "Macintosh HD:MyFolder:"

try
	do shell script "cd " & quoted form of POSIX path of myFolder & "; ls -1pt | grep -v /"
	set fileList to result
on error errorMsg number errorNum
	display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ASCII character 13
set fileList to every text item of fileList
set AppleScript's text item delimiters to ASTID

try
	tell application "Finder" to launch
	
	repeat with thisItem in fileList
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "."
		set newName to (text items 2 through -1 of thisItem & "ps") as text
		set AppleScript's text item delimiters to ASTID
		
		try
			tell application "Finder" to set name of file thisItem of folder myFolder to ((characters 3 through -1 of newName) as text)
		on error errorMsg number errorNum
			if errorNum is -48 then
				tell application "Finder" to delete file (thisItem's contents) of folder myFolder
			else
				error errorMsg number errorNum
			end if
		end try
	end repeat
on error errorMsg number errorNum
	display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try

Thanks for the help! This last one seems to do it!

Interesting sidenote: my PS files have no extension, when AppleScript gets hold of them, it assigns “.ps” automatically. The script then goes on to append another “.ps” to that one. I had to strip it from the script, so as to get only one.