Well… hi…
sorry for my english…
im looking for a script extension ranamer for especifics files…
like ren asd123.txt to .rtf
but only in especifics files…
thx…
Well… hi…
sorry for my english…
im looking for a script extension ranamer for especifics files…
like ren asd123.txt to .rtf
but only in especifics files…
thx…
Welcome! How about something like this:
set mF to (choose file)
tell application "Finder"
if name extension of mF is "txt" then
set tName to name of mF
set bName to my baseName(tName)
set name of mF to bName & ".rtf"
else
display dialog "Not a text file"
end if
end tell
to baseName(t) -- by jj Sancho
set k to reverse of t's characters
set kCount to count k
--> find first dot in reverse
repeat with i from 1 to kCount
if k's item i is "." then exit repeat
end repeat
if kCount is i and k's item -1 is "." then --> ie, t was ".txt" (no base name)
return "" as Unicode text
else if kCount is i then --> ie, t was "blah" (no dot)
return t --> no dot
else
return text 1 thru (-i - 1) of t
end if
end baseName
Here’s a slightly different approach that does something similar (except that it should convert a file with any [or no] name extension):
to switch_file_extension of f to r
tell application "Finder" to tell file f
set e to name extension
if e is r then return
set {c, d} to {count e, count r}
if c is 0 then set r to "." & r
set name to (get name)'s text 1 thru -(c + 1 + 1 div (d + 1)) & r
end tell
end switch_file_extension
switch_file_extension of (choose file without invisibles) to "rtf" (* modify new extension as required *)
This variation should modify the name extension of every file in a chosen folder (including those in any sub-folders) as indicated:
to switch_file_extensions at f from e to r
if e is r then return
set {c, d} to {count e, count r}
if c is 0 then set r to "." & r
set p to -(c + 1 + 1 div (d + 1))
tell application "Finder" to repeat with i in (get (entire contents of f)'s files whose name extension is e)
tell i to set name to (get name)'s text 1 thru p & r
end repeat
end switch_file_extensions
switch_file_extensions at (choose folder) from "txt" to "rtf" (* modify current & new extension as required *)
(Of course, where a change could cause a name conflict, any of the above suggestions will result in an error.)
Assuming you were going to drop files only (not folders full of files) this would be a droplet that would change the file extensions of any file or files dropped on the script saved as an application.
-- to use Kai's handler to switch extensions as a droplet:
property wasExt : "txt" -- You have to set these properties
property newExt : "rtf"
on open these_items
tell application "Finder" to repeat with i in these_items
if item i's class is not folder then
set e to i's name extension
set r to newExt
if e is r then return
set c to count e
set d to count r
if c is 0 then set r to "." & r
set i's name to (get i's name)'s text 1 thru -(c + 1 + 1 div (d + 1)) & r
end if
end repeat
end open