I am trying to rename files. The filename shall get a prefix, a group code, an individual number in his group and a suffix.
In my script, the names are generated correctly, but the script doesn’t rename the files, they just keep their names.
Has anybody any idea?
on open FileList
set GroupName to text returned of (display dialog "Please enter group name" with title "Group name" default answer "")
set GroupNo_t to text returned of (display dialog "Please enter group number" with title "Group No." default answer "")
if GroupNo_t < 10 then
set GroupNo_t to "0" & GroupNo_t as text
end if
set FileNoFirst to text returned of (display dialog "Please enter first file no." with title "First file no." default answer "1")
set FileNoCurr to FileNoFirst
repeat with FileCurr in FileList
set FileSuffix to FileCurr as text
repeat while (offset of "." in FileSuffix) > 0
set FileSuffix to text ((offset of "." in FileSuffix) + 1) thru -1 of FileSuffix
end repeat
set PathFull to FileCurr as text
set PathPrefix to ""
repeat while (offset of ":" in PathFull) > 0
set PathPrefix to PathPrefix & text 1 thru (offset of ":" in PathFull) of PathFull
set PathFull to text ((offset of ":" in PathFull) + 1) thru -1 of PathFull
end repeat
if FileNoCurr < 9 then
set FileNoCurr_t to "0" & FileNoCurr as text
else
set FileNoCurr_t to FileNoCurr as text
end if
tell application "Finder"
set FileNameNew to (PathPrefix & GroupName & " - S" & GroupNo_t & "E" & FileNoCurr_t & "." & FileSuffix) as text
set name of file FileCurr to FileNameNew
end tell
set FileNoCurr to FileNoCurr + 1
end repeat
end open
AppleScript: 2.1.2
Operating System: Mac OS X (10.6)
thx a lot. I tried both variants but didn’t succeed.
So I simplified the code:
set FileList to {"MacHD:Users:MOh:Documents:a.txt"}
repeat with FileCurr in FileList
tell application "Finder"
set FileNameNew to "MacHD:Users:MOh:Documents:f.txt"
set the name of contents of FileCurr to FileNameNew
end tell
end repeat
and get the following result:
option 2:
set FileList to {"MacHD:Users:MOh:Documents:a.txt"}
repeat with FileCurr in FileList
tell application "Finder"
set FileNameNew to "MacHD:Users:MOh:Documents:f.txt"
set the name of (FileCurr as text) to FileNameNew
end tell
end repeat
You are going to rename a file with a full path. this should work.
With this syntax don’t forget the keyword file
set FileList to {"MacHD:Users:MOh:Documents:a.txt"}
repeat with FileCurr in FileList
tell application "Finder"
set FileNameNew to "f.txt"
set the name of file FileCurr to FileNameNew
end tell
end repeat
or with alias specifier
set FileList to {alias "MacHD:Users:MOh:Documents:a.txt"}
repeat with FileCurr in FileList
tell application "Finder"
set FileNameNew to "f.txt"
set the name of contents of FileCurr to FileNameNew
end tell
end repeat
if FileList contains only text, dereferencing the list item is not necessary
Possibly the full script (apart from the error handling) should look like this. I’ve assumed from the ‘open’ handler that you’re going to use it as a droplet and that therefore FileList will contain aliases:
on open FileList
set GroupName to text returned of (display dialog "Please enter group name" with title "Group name" default answer "")
set GroupNo_t to text returned of (display dialog "Please enter group number" with title "Group No." default answer "")
if ((GroupNo_t as integer) < 10) then -- NB. Compare like with like, not text with integer.
set GroupNo_t to "0" & GroupNo_t
end if
set FileNoFirst to (text returned of (display dialog "Please enter first file no." with title "First file no." default answer "1")) as integer
set FileNoCurr to FileNoFirst
repeat with FileCurr in FileList
if (FileNoCurr < 9) then
set FileNoCurr_t to "0" & FileNoCurr
else
set FileNoCurr_t to FileNoCurr as text
end if
tell application "Finder"
set FileSuffix to name extension of FileCurr
set name of FileCurr to (GroupName & " - S" & GroupNo_t & "E" & FileNoCurr_t & "." & FileSuffix)
end tell
set FileNoCurr to FileNoCurr + 1
end repeat
end open