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
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
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
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