Changing first four characters in multiple file names droplet?

Hi everyone,
I am new to applescripting and have done searches on the net. I can’t find anything?

I need to create a droplet for work that chages the first 4 digits of filenames in a folder.
I’m using OS 9.9.2

eg. Multiple files (usually about 10-20) in the same folder named:

1234_help.eps
1234_help1.eps
1234_help2.eps
1234_help4.eps

Changed to:

5678_help.eps
5678_help1.eps
5678_help2.eps
5678_help4.eps

Any help is greatly appreciated.
Thanks everyone! :slight_smile:

Sam

here ya go. Its a modification of something i made earlier, so it may have some unnesicary lines in there.

property startstring : "1234"
property newstartstring : "5678"

on open aliasListOfFiles
	repeat with i from 1 to number of items in aliasListOfFiles
		tell application "Finder"
			set patht to POSIX path of (item i of aliasListOfFiles)
		end tell
		
		if last text item of patht is "/" then error "No Folders!"
		
		--set patht to "/Users/chris/Documents/icon make test/Twice the Speed of Life.pict"
		
		tell application "Finder"
			set thefileposix to (POSIX file patht as alias)
			set fold to POSIX path of (folder of thefileposix as alias)
			set pcc to count of text items in (name of thefileposix as string)
			set frontthethingsdangname to (text items 1 thru (4) of (name of thefileposix as string)) as string
			set thethingsdangname to (text items 5 thru (pcc) of (name of thefileposix as string)) as string
		end tell
		
		if frontthethingsdangname is startstring then
			do shell script "mv \"" & patht & "\" \"" & fold & newstartstring & thethingsdangname & "\""
		end if
		
	end repeat
end open

I don’t think OS 9 can use “do shell script”.

Crud, missed that. You realize you are behind 5 major revisions of the Mac OS? What are you running on?

BTW, I’ll send you an old copy of Panther if you pay for shipping.

Here’s it with a subroutine from apple’s given stuff, but i’ve only 10.4 tested it…

property startstring : "1234"
property newstartstring : "5678"

on open aliasListOfFiles
	repeat with i from 1 to number of items in aliasListOfFiles
		tell application "Finder"
			set patht to POSIX path of (item i of aliasListOfFiles)
		end tell
		
		if last text item of patht is "/" then error "No Folders!"
		
		--set patht to "/Users/chris/Documents/icon make test/Twice the Speed of Life.pict"
		
		tell application "Finder"
			set thefileposix to (POSIX file patht as alias)
			set fold to POSIX path of (folder of thefileposix as alias)
			set pcc to count of text items in (name of thefileposix as string)
			set frontthethingsdangname to (text items 1 thru (4) of (name of thefileposix as string)) as string
			set thethingsdangname to (text items 5 thru (pcc) of (name of thefileposix as string)) as string
		end tell
		
		if frontthethingsdangname is startstring then
			set_item_name((POSIX file patht as alias), newstartstring & thethingsdangname)
		end if
		
	end repeat
end open

on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try
				set the name of this_item to new_item_name
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				--beep
				tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
				copy the result as list to {new_item_name, button_pressed}
				if the button_pressed is "Skip" then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
			copy the result as list to {new_item_name, button_pressed}
			if the button_pressed is "Skip" then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

You guys are awesome! Thank you. I’ll give them a try.

Hi again everyone,
Should there be a pop up that asks me to input the numbers I want to change and to what I would like to change them to?

Thank you!:slight_smile:

crud, didn’t realize u wanted that.
try

property startstring : ""
property newstartstring : ""

on open aliasListOfFiles
	display dialog "This will replace characters at the beginning of file names..."
	set startstring to text returned of (display dialog "Replace..." default answer startstring)
	set newstartstring to text returned of (display dialog "With..." default answer newstartstring)
	repeat with i from 1 to number of items in aliasListOfFiles
		tell application "Finder"
			set patht to POSIX path of (item i of aliasListOfFiles)
		end tell
		
		if last text item of patht is "/" then error "No Folders!"
		
		--set patht to "/Users/chris/Documents/icon make test/Twice the Speed of Life.pict"
		
		tell application "Finder"
			set thefileposix to (POSIX file patht as alias)
			set fold to POSIX path of (folder of thefileposix as alias)
			set pcc to count of text items in (name of thefileposix as string)
			set frontthethingsdangname to (text items 1 thru (count of text items in startstring) of (name of thefileposix as string)) as string
			set thethingsdangname to (text items ((count of text items in startstring) + 1) thru (pcc) of (name of thefileposix as string)) as string
		end tell
		
		if frontthethingsdangname is startstring then
			set_item_name((POSIX file patht as alias), newstartstring & thethingsdangname)
		end if
		
	end repeat
end open

on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try
				set the name of this_item to new_item_name
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				--beep
				tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
				copy the result as list to {new_item_name, button_pressed}
				if the button_pressed is "Skip" then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
			copy the result as list to {new_item_name, button_pressed}
			if the button_pressed is "Skip" then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

It worked! Thank you very much themacgeek.

You just helped me out a lot.:slight_smile:

Given the number of new features in each successive version of the Mac OS, it can sometimes be hard to remember exactly what works where. :wink:

There are a number of different reasons why someone might wish/need to continue using a system that is pre-Mac OS X (or, for that matter, a machine that is pre-G3). After all, I believe that’s why the AppleScript | Mac OS forum exists: to discuss scripting “for all versions of AppleScript prior to OS X”.

If memory serves, even commands like POSIX path and POSIX file weren’t introduced until about AppleScript version 1.8.1 - so I believe they’re available only from Mac OS 9.1 onwards.

In spite of that, backwards compatibility has (IMO) been pretty good, and some very old scripts of mine have survived numerous system upgrades remarkably well. It should still be possible to achieve the aims stated at the beginning of this thread, using syntax that works in even older AS/OS versions (although I wouldn’t care to try and put a precise number on it).

The following variation, for example, should change the names of files that are dropped directly - and/or that are contained (at the top level only) by dropped folders:

property searchText : ""
property replaceText : ""
property startChar : 1

to changeName(currFile)
	tell application "Finder" to try
		set currFile's name to replaceText & text startChar thru end of (get currFile's name)
	on error errMsg
		display dialog errMsg buttons {"Cancel", "Continue..."} default button 2
	end try
end changeName

to open itemList
	set searchText to text returned of (display dialog "Change filenames that start with what?" default answer searchText)
	set startChar to (count searchText) + 1
	if startChar is 1 then error number -128 -- no search text entered
	set replaceText to text returned of (display dialog "Replace \"" & searchText & "\" with what?" default answer replaceText)
	if replaceText is searchText then error number -128 -- no change
	
	repeat with currItem in itemList
		tell application "Finder" to if currItem's kind is "folder" then
			repeat with currFile in (get folder currItem's files whose name starts with searchText)
				my changeName(currFile)
			end repeat
		else
			my changeName(currItem)
		end if
	end repeat
end open

(Tested in OS 9.1 and OS 10.4.2.)