Re: newbie help - parsing pathnames to rename files
Re: newbie help - parsing pathnames to rename files
- Subject: Re: newbie help - parsing pathnames to rename files
- From: KOENIG Yvan <email@hidden>
- Date: Fri, 29 Feb 2008 12:30:14 +0100
Le 28 févr. 2008 à 22:07, Draper Kauffman a écrit :
We have a very large database of graphic files, stored as follows:
~/resources/<account>/<project#>/<artist>/<nn?.extension>
I've been asked to find a way to rename all of the graphic files to
add the account code (6 characters), the project number (1 to 4
digits), and optionally the artist's name in front of the existing
2 or 3-character filename and 3-character extension.
Acceptable formats for the new filenames would be any of these:
<artist>_<account>_<project#>_<existing file name>
<account>_<project#>_<artist>_<existing file name>
<account>_<project#>_<existing file name>
The first format is preferred, but if <artist> is included there
will have to be a way to truncate it if it would make the new name
too long. The second format is acceptable and might make the
truncation process easier. However, the third format avoids the
whole problem and is also acceptable.
If anyone knows of a commercial product or existing AppleScript
program that can do what I need, I'd deeply appreciate a pointer.
Failing that, my fallback plan is to try to learn enough
AppleScript to hack this out.
I was a programmer back in the age of dinosaurs (pre-Lisa/Mac), but
so far, I'm finding this pretty heavy going. There's a nice
function for renaming files in the sample "Add to File
Names.scpt". The problem I'm having is finding a way to parse the
pathname and convert the last three folder names to text strings.
If you have or know of resources or examples of this kind that I
can borrow/learn from, I'd be very grateful for the help.
Drake
You may try this one.
CAUTION some instructions may be modified by extraneous lineBreaks.
This is why I sent you the script to your own mailbox as attachment .
--(SCRIPT Rename-Files]
(*
--+-+-+-+-+-+
Save the script as Application or Application bundle on the desktop.
Drag and drop the icon of your folder "resources" onto the script's one.
Your files will be renamed <artist>∆_<account>_<project#>_<existing
file name>
The ∆ is here to allow you to easily cut the name if required.
Yvan KOENIG le 22 mars 2004
remanié le 24 février 2008
28 février 2008
--+-+-+-+-+-+
*)
property tt : "" -- globale
property msg99 : "" -- globale
--===========
(* lignes exécutées si on double clique
sur l'icône du script application
• lines executed if one double click
the application script's icon *)
tell application "Finder" to set listeFichiers to every item in
(choose folder)
if (count of listeFichiers) = 0 then return
open listeFichiers
--==============================
on open (sel) (* sel contient une liste d'alias des éléments
qu'on a déposés sur l'icône du script (la sélection)
• sel contains a list of aliases of the items
dropped on the script's icon (the selection) *)
my prepareMessages()
try
set tt to ""
repeat with elem in sel
my exploreTraite(elem as alias, "")
end repeat
set the clipboard to tt
on error MsgErr number NroErr
if NroErr is not -128 then
beep 2
tell application (path to frontmost application as string) to ¬
display dialog "" & NroErr & " : " & MsgErr ¬
with icon 0 buttons {msg99} giving up after 20
end if -- NroErr is…
return
end try
end open
--============= Routines
on exploreTraite(elem, ptree) (*
elem est un alias
elem is an alias *)
local elem_, cl_, ctype_, typeId_, nExt_
set elem_ to elem as text
tell application "System Events"
set cl_ to (class of item elem_) as Unicode text
end tell
if cl_ is in {"file package", "«class cpkg»"} then
my TraiteUnFichier(elem_)
else if cl_ is in {"folder", "«class cfol»"} then
my ExploreUnDossier(elem_, ptree)
else
my TraiteUnFichier(elem_)
end if -- cl_ is …
end exploreTraite
-- ===========
on ExploreUnDossier(dossier, ptree)
repeat with nomElement in list folder dossier without invisibles
set cheminElement to dossier & nomElement
tell application "Finder" to set C to name of (dossier as alias)
my exploreTraite(cheminElement as alias, ptree & C & ":")
end repeat
end ExploreUnDossier
--===========
on TraiteUnFichier(leCheminOriginal_UniText) (*
leCheminOriginal_UniText est unicode text *)
set AppleScript's text item delimiters to ":"
set LL to text items of leCheminOriginal_UniText
set AppleScript's text item delimiters to ""
set newName to (item -2 of LL) & "∆_" & (item -4 of LL) & "_" &
(item -3 of LL) & "_" & (item -1 of LL) (*
between the first two components of the name I inserted the
character ∆ ASCII character 198 so it would be easy tu cut the name
if required *)
tell application "Finder" to set name of file
leCheminOriginal_UniText to newName
end TraiteUnFichier
-- =============
on prepareMessages()
local n
set n to do shell script "defaults read 'Apple Global Domain'
AppleLocale"
if n starts with "fr_" then
set msg99 to " Vu "
else
set msg99 to "Oops"
end if
end prepareMessages
--[/SCRIPT]
Yvan KOENIG _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden