Re: file name mutations
Re: file name mutations
- Subject: Re: file name mutations
- From: Bryan <email@hidden>
- Date: Mon, 17 Sep 2001 01:21:43 -0400
- Organization: Apex Radiology
I've got multiple advanced degrees and still can't master that grep stuff.
That why I use AppleScript instead of C or perl
... OK, so I'm just too lazy to relearn the grep rules every time I want
to use it.
There are only about a hundred ways to do this.
If the name you want is always after the last hyphen;
my preference would be to convert the string to a list
using the hyphen as a delimiter.
My choice (again the easiest thing to remember) is the
ACME Script Widgets OSAXen command "tokenize":
tell app "Finder"
set filepath to "MacHD:nz1711jr-11028812.eps"
set oldName to the name of file filepath
set newName to (item -1 of (tokenize oldName with delimiters {"-"}))
set the name of file filepath to newName
end tell.
Ric Phillips wrote:
>
On 17/9/01 11:18 AM, "Andrew Simpson" <email@hidden>
>
wrote:
>
>
> can somebody tell me how i would do the following...
>
>
>
> check for file names with the following name convention:
>
>
>
> file name = nz1711jr-11028812.eps
>
>
>
> sometext-six digit number plus incrementer.
>
>
>
> and convert the file name to the following:
>
>
>
> file name = 11028812.eps
>
>
>
> ie chop the front off to the hyphen.
>
>
>
> i'm sure its quite easy to those in the know but i haven't got my head
>
> around the whole text manipulation routines as i'm used to VB with all the
>
> functions built in.
>
>
>
> split it and right it and away we go...
>
>
>
> any help appreciated.
>
>
>
> Andrew.
>
>
Step 1: Get the name.
>
----------------------------------
>
>
tell app "finder"
>
Set fName to the name of file "nz1711jr-11028812.eps"
>
end tell.
>
>
Step 2: Get the Regex extension from
>
http://www.lazerware.com/software.html
>
And install it - then,
>
--------------------------------------
>
>
set fName to REReplace fName with "\\2" pattern "([^-]*-)(.*)"
>
>
This defines two patterns to match the file name string against,
>
1: ([^-]*-), meaning any number of characters that are not a dash ending in
>
another character that is a dash, or, all characters up to and including the
>
first dash. (For those puzzled "([^-]*-)" is more reliable than "(.*-)")
>
Note: this won't work if any of your file names have >1 dash.
>
2: (.*), meaning any string of characters of any length, which, by coming
>
after the first pattern, means all the rest of the string.
>
>
The "\\2" is called a 'back reference' and means all the characters matched
>
by the second pattern. Because the back reference to the first pattern
>
(which would be "\\1" ) is omitted it won't be part of the replacement -
>
ie., everything up to and including the first dash will be dropped from the
>
string.
>
>
Step 3: Change the file name.
>
--------------------------------------
>
>
Tell app "finder"
>
set the name of file "nz1711jr-11028812.eps" to fName.
>
End tell
>
>
You will have to build either one or three loops around these steps, but you
>
will find that using the regular expression is very very fast compared to
>
apple scripted comparisons.
>
>
Step 4: Have some fun playing around with other regular expressions to see
>
how incredibly useful they are......
>
>
Cheers,
>
>
Ric Phillips
>
>
Computer Laboratory Support Officer
>
Faculty of Humanities and Social Sciences
>
La Trobe University
>
9479 2792
>
_______________________________________________
>
applescript-users mailing list
>
email@hidden
>
http://www.lists.apple.com/mailman/listinfo/applescript-users
[demime 0.98b removed an attachment of type text/x-vcard which had a name of bryan.vcf]