• 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: Parsing filenames
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parsing filenames


  • Subject: Re: Parsing filenames
  • From: Nigel Garvey <email@hidden>
  • Date: Thu, 12 Jun 2003 12:08:26 +0100

brian mather wrote on Wed, 11 Jun 2003 22:38:06 -0400:

>I have a folder full of files that follow a standard naming convention:
>[prefix][digits]_[string].extension
>The [prefix] part of the name is either "p" or "pANYTEXT-".
>The digits is a string of 6 or 7 numbers.
>the _[string] is sometimes present and sometimes absent.
>And finally the extension while typically present does not necessarily have
>to exist and I'd like the script to be able to gracefully handle either
>instance.
>
>(e.g.: p123.tif; pFOO-456; pBAR-789_yippee.jpg; ...)
>
>I'm interested to parse the filename extracting and writing to a file the
>digits and the string following the underbar where applicable.

>tell application "Finder"
> activate
> set the_folder to choose folder
> set the_files to every file of the_folder
> repeat with a_file in the_files
> set long to the name of a_file

-- Get the text after the leading "p"
set trimmed to text 2 thru -1 of long

-- Get the text after the dash (if present)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"-"}
set trimmed to text item -1 of trimmed

-- Remove the underscore (if present)
set AppleScript's text item delimiters to {"_"}
set trimmed to trimmed's text items
set AppleScript's text item delimiters to {""}
set trimmed to trimmed as string

set AppleScript's text item delimiters to astid

set name of a_file to trimmed
end repeat
end tell

NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: topping a window of an application not understanding AppleScript
  • Next by Date: Re: Newbie question re records
  • Previous by thread: Re: Parsing filenames
  • Next by thread: Re: Parsing filenames
  • Index(es):
    • Date
    • Thread