• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: RegExps in AS
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RegExps in AS


  • Subject: Re: RegExps in AS
  • From: kai <email@hidden>
  • Date: Sun, 20 Feb 2005 21:02:54 +0000


On Sun, 20 Feb 2005 18:35:58 +0100, "Ihar 'Philips' Filipau" wrote:

[ I have checked archives of mail list, and found nothing interesting. If I have missed something I'd love to be pointed out in right direction. ]

I have found is that AS is completely missing regular expression support.
I have tried to strip extension from file name and after three hours ended up with 20 lines of code which works most of the time. That compared to single line of PCRE (Perl Compatible Regular Expressions) which handles all cases all the time. Not the point AS shines at.


In past I have explained to number of non-tech people how regexps do work, but if someone would ask me how to do anything with text in AS, I'd rather say "sorry pal, you can't".

How do you people do in AS simple things like: strip extension (if file has one), strip directories from path name (if there anything to strip), extract extension (if name has one), extract path name (if name has one), etc.

One way might be to use AppleScript's text item delimiters:

------------------------------------

on part of str at pos between sep
	set text item delimiters to sep
	set str to str's text item pos
	set text item delimiters to {""}
	str
end part

set filePath to "Macintosh HD:Users:UserName:Documents:Some File.rtf"

set fullName to part of filePath at -1 between ":"
set shortName to part of fullName at 1 between "."

{fullName, shortName}

--> {"Some File.rtf", "Some File"}

------------------------------------

P.S. And I need this to work on standard Mac OS 10.3.x. Without external applications please, code in question has to work on iTunes library with thousand files

You can also get a list of filenames directly from the Finder (or by using the 'list folder' command):


------------------------------------

to stripExtension of ext from lst
	set text item delimiters to return
	set lst to lst as string
	set text item delimiters to "." & ext
	set lst to lst's text items
	set text item delimiters to {""}
	(lst as string)'s paragraphs
end stripExtension

set mp3Folder to choose folder with prompt "Please choose an iTunes folder:"
tell application "Finder" to set mp3List to name of files of folder mp3Folder whose name extension is "mp3"
set nameList to stripExtension of "mp3" from mp3List


------------------------------------

- calling external applications is un-Unixy terribly expensive.

If you're more comfortable with Unixy stuff, then try calling it from AppleScript using a 'do shell script' command:


------------------------------------

do shell script "apropos strsep"

--> "strcat(3), strncat(3), strchr(3), strrchr(3), strcmp(3), strncmp(3), strcasecmp(3), strncasecmp(3), strcpy(3), strncpy(3), strerror(3), strlen(3), strpbrk(3), strsep(3), strspn(3), strcspn(3), strstr(3), strtok(3), index(3), rindex(3) - string specific functions
strsep(3) - separate strings"


------------------------------------

---
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


  • Follow-Ups:
    • Re: RegExps in AS
      • From: "Ihar 'Philips' Filipau" <email@hidden>
    • Re: RegExps in AS
      • From: kai <email@hidden>
  • Prev by Date: Re: RegExps in AS
  • Next by Date: Re: RegExps in AS
  • Previous by thread: Re: RegExps in AS
  • Next by thread: Re: RegExps in AS
  • Index(es):
    • Date
    • Thread