Re: RegExps in AS
Re: RegExps in AS
- Subject: Re: RegExps in AS
- From: kai <email@hidden>
- Date: Tue, 22 Feb 2005 23:34:09 +0000
On Monday, February 21, 2005, at 10:25 pm, Ihar 'Philips' Filipau
wrote:
On 20 Feb 2005, at 22:02, kai wrote:
One way might be to use AppleScript's text item delimiters:
[snip]
Wow. That's cool.
I still have problems getting how this stuff with delimiters works. It
is - together with "text" - most unintuitive piece of stuff in AS.
I have asked about RegExp, since this is normal choice for text
processing for about last 20 years. PCRE are newer revised one from
Perl.
All this playing around with negative indexes makes AS messy: in some
places they work, in some places AS barks on them. In some places
empty string is Ok, in other it is error. Mess, compared to beauty,
simplicity and power of regular expressions.
But nevertheless, above quoted script works and works quite well where
I need it - that's important. Thanks!
I guess any less familiar 'system' can seem a tad illogical, Ihar -
especially with long experience elsewhere. I'd hope that it makes more
sense to you as you go along - as I believe it has done for others
around here. :-)
Looking at other vanilla options, one thought that I had initially
about this question was to use a list of extensions...
{".aac", ".ai", ".arc", ".aiff", ".asf", ".asmx", ".asp", ".aspx",
".avi", ".bat", ".bin", ".bmp", ".cer", ".cgi", ".cif", ".css", ".csv",
".dat", ".db", ".dbx", ".dll", ".dmg", ".doc", ".dot", ".eml", ".eps",
".exe", ".fpt", ".gif", ".gp4", ".gz", ".hlp", ".htm", ".html", ".hqx",
".ico", ".img", ".indd", ".ini", ".jar", ".jpeg", ".js", ".kpg",
".lit", ".lnk", ".log", ".m4a", ".max", ".mdb", ".mid", ".midi",
".mim", ".mime", ".mp3", ".mpu", ".mp4", ".mpg", ".mpeg", ".mpp",
".mov", ".moov", ".ns2", ".ns3", ".ns4", ".ocx", ".old", ".p65",
".pab", ".pct", ".pdf", ".pfc", ".pgm", ".pl", ".pmd", ".png", ".pps",
".ppt", ".ps", ".psd", ".pst", ".pub", ".qt", ".qxd", ".ra", ".ram",
".rar", ".rcd", ".rtf", ".rm", ".sea", ".sca", ".sit", ".smi", ".sql",
".swf", ".swp", ".sys", ".tar", ".tiff", ".tga", ".tmb", ".txt", ".uu",
".uue", ".vsd", ".wav", ".wk1", ".wma", ".wmf", ".wmv", ".wpd", ".wpt",
".wpm", ".xls", ".xlw", ".xlt", ".xml", ".ymg", ".yps", ".z", ".zip"}
...to help clean up a list of filenames. However, this wouldn't be a
particularly fast method - and I doubt that any list could ever be
considered sufficiently comprehensive.
Since then, I've been thinking about a point that Andrew made the other
day...
On Sun, 20 Feb 2005 13:29:45 -0800, Andrew Oliver wrote:
Your only real options here are Text Item Delimiters (text items 1
through
-2 of the name when TIDs are '.'), or asking the Finder for the
filename
extension (a property of a file) and stripping that.
Personally I'd use the latter approach since you're probably using the
Finder to change the file names anyway.
That appealed to me, since the Finder should normally present only
valid extension names. The principle could then be extended from
truncating a single name to processing an entire list of filenames. In
other words, we could first extract any extensions that might exist in
a folder - and then use the resulting list to clean up the filenames:
-----------------------
tell application "Finder" to tell folder (choose folder)'s files
set n to name
set x to name extension
end tell
set text item delimiters to return & "."
set x to "." & x & return
set v to 1
repeat while v < (count x's paragraphs)
set p to x's paragraph v & return
set text item delimiters to p
set x to x's text items
set text item delimiters to {""}
set x to p & x
set v to v + 1
end repeat
set text item delimiters to return
set n to n as string
repeat with i in x's paragraphs 1 thru -2
set text item delimiters to i
set n to n's text items
set text item delimiters to {""}
set n to n as string
end repeat
set n to n's paragraphs
-----------------------
But let's get back to the Finder for a moment. Since we're using it to
process files anyway, why not let it loose on the entire job? While the
thought had occurred to me a while back, I ignored it because the
general subject focused on text manipulation. However, Bob Poland
recently quizzed me about "hiding" extensions as a possible solution to
the more specific question. This prompted me to think again - and to
suggest this simpler (not to mention somewhat shorter) alternative:
-----------------------
tell application "Finder" to tell folder (choose folder)'s files
set extension hidden to true
set nameList to displayed name
end tell
-----------------------
Granted, this can affect the way in which the selected files are
displayed in the Finder - which may or may not be an issue. If you
normally prefer to show file extensions, I suppose there's always:
-----------------------
tell application "Finder" to tell folder (choose folder)'s files
set extension hidden to true
set nameList to displayed name
set extension hidden to false
end tell
nameList
-----------------------
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden