Help need with renaming files & (sub)folders

I am trying to write a script that has to rename files and folders

I get an error in the

on finderprocess(myFiles)

part telling me that the

JobNumberEntered

isn’t defined.
I can get it to work by putting the dialog part in the

on finderprocess(myFiles)

part but then there’s a repeat for every file found

Because of this i haven’t implemented the folder rename part yet.

Please help…

repeat
	set JobNumber to "Wat is het ordernummer (zie orderbon)?"
	set tempNumber to display dialog JobNumber default answer ""
	set JobNumberEntered to text returned of tempNumber
	display dialog "Is " & JobNumberEntered & " OK?" buttons {"Go back", "Continue"} default button 2
	if the button returned of the result is "Continue" then exit repeat
end repeat

tell application "Finder"
	set chosenfolder to choose folder
	my folderprocess(chosenfolder)
end tell

on folderprocess(chosenfolder)
	tell application "Finder"
		set myFiles to every file of chosenfolder
		if (count of myFiles) ≠ 0 then
			my finderprocess(myFiles)
		end if
		set subFolders to folders of chosenfolder
		if (count of subFolders) ≠ 0 then
			repeat with thisFolder in subFolders
				my folderprocess(thisFolder)
			end repeat
		end if
	end tell
end folderprocess

on finderprocess(myFiles)
	repeat with thisfile in myFiles
		set old_name to name of thisfile
		set offset_ to offset of "_" in old_name
		set name of thisfile to JobNumberEntered & text (offset_ - 0) thru end of old_name
	end repeat
end finderprocess

–Peter–

Hi

JobNumberEntered is defined in the (implicit) run handler, the custom handler can’t see it.
Declare the variable as global or property or pass the value thru the call as parameter.

finderprocess() without involving the Finder cannot work.
Only the Finder (and System Events) can read and write file names

Ok Stefan,
Makes sense.
What’s the best option for JobNumberEntered in my case (and why) and can you give me an example how to do this?

Thanks in advance,
–Peter–

there is no best way in this case, all are good, here the parameter method


.
my finderprocess(myFiles, JobNumberEntered)
.

on finderprocess(myFiles, jobNumber)
	tell application "Finder"
		repeat with thisfile in myFiles
			set old_name to name of thisfile
			set offset_ to offset of "_" in old_name
			set name of thisfile to jobNumber & text (offset_ - 0) thru end of old_name
		end repeat
	end tell
end finderprocess

Stefan,

Here’s the script but is doesn’t work.
It’s still complaing about the fact that

JobNumberEntered

isn’ defined in the

if (count of myFiles) ≠ 0 then
			my finderprocess(myFiles, JobNumberEntered)
		end if

part

repeat
	set JobNumber to "Wat is het ordernummer (zie orderbon)?"
	set tempNumber to display dialog JobNumber default answer ""
	set JobNumberEntered to text returned of tempNumber
	display dialog "Is " & JobNumberEntered & " OK?" buttons {"Go back", "Continue"} default button 2
	if the button returned of the result is "Continue" then exit repeat
end repeat

tell application "Finder"
	set chosenfolder to choose folder
	my folderprocess(chosenfolder)
end tell

on folderprocess(chosenfolder)
	tell application "Finder"
		set myFiles to every file of chosenfolder
		if (count of myFiles) ≠ 0 then
			my finderprocess(myFiles, JobNumberEntered)
		end if
		set subFolders to folders of chosenfolder
		if (count of subFolders) ≠ 0 then
			repeat with thisFolder in subFolders
				my folderprocess(thisFolder)
			end repeat
		end if
	end tell
end folderprocess

on finderprocess(myFiles, JobNumber)
	tell application "Finder"
		repeat with thisfile in myFiles
			set old_name to name of thisfile
			set offset_ to offset of "_" in old_name
			set name of thisfile to JobNumberEntered & text (offset_ - 0) thru end of old_name
		end repeat
	end tell
end finderprocess

What am I doing wrong?

–Peter–

sorry, I overlooked that the call is also in a handler.
Then it’s more convenient to declare a property

Use your script in post #1, add the Finder tell block in the second handler
and put this line at the top of your script


property JobNumberEntered : ""

Thanks a lot Stefan,
That did the trick.
Script works just fine now !!!

–peter–