Replace filenames by string position?

I could do this in php, but I know nothing of applescript. The main function I need is PHP’s substr_replace, to rename files in a passed array, by start/end string positions, with passed text.

Basically I’m looking for an applescript function to add to an Automator workflow, that will replace filenames with text, by character position. The applescript function would have 4 parameters, (files:Array, startPosition:int, endPosition:int, replaceWith:String). The first is an array of the files passed by Automator’s “Get Selected Files” func, then the starting and ending positions for what is to be replaced, finally followed by the string to replace with.

So something like,

function renameFilenamesByPosition(files, startPos, endPos, replaceWith)
{
for(i=0;i<count(files);i++)
{
file = files[i];
oldName = basename(file);
newName = substr_replace(oldName, replaceWith, startPos, endPos);
rename(oldFilename, newName);
}
}

Any help in coming up with an applescript that I can do this, and be inserted into an Automator workflow, is MUCH APPRECIATED!! I often have files with number tags, that I’d like to have removed.

All that’s doable in AppleScript for sure (and an AppleScript can be incorporated in your Automator workflow) except for one salient point: what’s a string position?

Hi Adam, thanks for the response.

The text within the filename to be replaced is determined by start & end character positions. What I meant was start & end position of the specific text within the filename to be replaced. That probably makes more sense, but just to prevent any further confusion, I’ll post an example.

The array of files passed from Automator’s ‘Get All Selected Finder Items’ could be.
00012-myfile.jpeg
00013-another.jpeg
00014-someother.jpeg

The applescript’s job would be to present a dialogue requesting a start position, end position, and the text to replace with. So, in with the above files I might want to pass, 0 as the start position, and 6 as the end position, with a blank space as the replace with text, so that the files are renamed as.

myfile.jpeg
another.jpeg
someother.jpeg

I suspected as much. Since some AppleScript is required to do this, might as well do it in an AppleScript start to finish:

tell application "Finder" to set Sel to selection as alias list
set tRange to text returned of (display dialog "Please enter the range of characters to be changed using a colon to delineate beginning from end, e.g., 3:7 indicates third through seventh character inclusive." default answer "1:4")
set toReplace to text returned of (display dialog "Please enter the text to be substituted." default answer "theText")
repeat with oneFile in Sel
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set tParts to text items of tRange
	set AppleScript's text item delimiters to tid
	set tName to name of (info for oneFile)
	set toFind to text (item 1 of tParts as integer) thru (item 2 of tParts as integer) of tName
	set tNewName to findAndReplace(toFind, toReplace, tName)
	tell application "Finder" to set name of oneFile to tNewName
end repeat

to findAndReplace(toFind, toReplace, theText)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to toFind
	set textItems to theText's text items
	set AppleScript's text item delimiters to toReplace
	tell textItems to set editedText to beginning & toReplace & rest
	set AppleScript's text item delimiters to astid
	return editedText
end findAndReplace

Adam, Adam, Adam. you guys and your delimiter tricks :stuck_out_tongue:

set tRange to text returned of (display dialog "Please enter the range of characters to be changed using a colon to delineate beginning from end, e.g., 3:7 indicates third through seventh character inclusive." default answer "1:4")
words of tRange
-- Returns {"1","4"} - the colon is discarded

I don’t know why I’m the only person who ever uses “words” (though I use “words” and “text items” as needed) :stuck_out_tongue: :lol:

Enjoy!

Jim Neumann
BLUEFROG

Lots of ways to do it Jim. Here’s another, somewhat esoteric way to enter the range:

set T to run script ("{" & text returned of (display dialog "Enter your list separated by commas" default answer "1, 5") & "}") 
--> {1, 5}, an AppleScript List

I usually stay away from words because I script several apps that also have “words” in their dictionaries and occasionally that can be confusing.

True enough. :slight_smile:

Interesting use of run script. See. I still pick up new info after all these years. Thanks for the nice example. :smiley: /adding syntax to toolbox :smiley:

Jim

“run script” is a handy way to build variables from text strings.

Here’s one from Jobu I wouldn’t have dreamed of:

-- ask for an item from a record
set theKey to choose from list {"firstName", "lastName", "age", "birthday"}
-- the record itself
set theRecordList to {firstName:"Adam", lastName:"Bell", age:71, birthday:"July 29, 1937"}

return (getRecordValue(theKey, theRecordList))
-- build the text and run it
to getRecordValue(theKey, theList)
	run script "on run{theKey,theList}
	return (" & theKey & " of theList )
end" with parameters {theKey, theList}
end getRecordValue

AWESOME! Thanks Adam!! :smiley:

I made some minor changes, most notably replacing the use of the findAndReplace function, as it doesn’t restrict changes to the filename within the start/end positions. I then just added a few conditional statements to handle erroneous start/end positions that may be passed in. As well, you can choose from 3 ways to alter the filename. By passing 3 parameters (start, end, replacement) you can replace any section of a filename. By passing 2 parameters (end, replacement) you can replace a section starting from 0. And finally by passing 1 parameter (replacement) you can simply append to the front of the filename. Much appreciated Adam! I’m going to toss this into Automator! It might be ugly applescript, but I’ve never written one myself, so thanks a lot.


tell application "Finder" to set Sel to selection as alias list
set tRange to text returned of (display dialog "start, end, replacement 
end, replacement
replacement" default answer "0 : 0 :replace")
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set tParts to text items of tRange
set AppleScript's text item delimiters to tid
set paramList to every text item of tParts
set numParams to count of tParts

if numParams = 3 then
	set startPos to text item 1 of tParts as integer
	set endPos to text item 2 of tParts as integer
	
	if startPos < 0 or endPos < 0 or startPos > endPos then
		display dialog "Major bummer dude, why so negative?"
	else
		repeat with oneFile in Sel
			set tName to name of (info for oneFile)
			set toReplace to text item 3 of tParts
			set tNewName to insertReplacement(tName, toReplace, startPos, endPos)
			tell application "Finder" to set name of oneFile to tNewName
		end repeat
	end if
else if numParams = 2 then
	set startPos to 0
	set endPos to text item 1 of tParts as integer
	if endPos < 0 then
		display dialog "Major bummer dude, why so negative?"
	else
		repeat with oneFile in Sel
			set tName to name of (info for oneFile)
			set toReplace to text item 2 of tParts
			set tNewName to insertReplacement(tName, toReplace, startPos, endPos)
			tell application "Finder" to set name of oneFile to tNewName
		end repeat
	end if
else if numParams = 1 then
	set startPos to 0
	set endPos to 0
	repeat with oneFile in Sel
		set tName to name of (info for oneFile)
		set toReplace to text item 1 of tParts
		set tNewName to insertReplacement(tName, toReplace, startPos, endPos)
		tell application "Finder" to set name of oneFile to tNewName
	end repeat
	
end if

to insertReplacement(originalString, replaceString, startPos, endPos)
	set beforeChars to ""
	if startPos > 0 then
		set beforeChars to get text (0 + 1) thru (startPos) of originalString
	end if
	set afterChars to ""
	if (endPos < length of originalString) then
		set afterChars to get text (endPos + 1) thru (length of originalString) of originalString
	end if
	set editedText to beforeChars & replaceString & afterChars
	
	return editedText
end insertReplacement