• 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: AS how to check if a file name matches a string in a text file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: AS how to check if a file name matches a string in a text file


  • Subject: Re: AS how to check if a file name matches a string in a text file
  • From: Christopher Stone <email@hidden>
  • Date: Sat, 05 Jul 2014 05:51:02 -0500

On Jun 29, 2014, at 11:28, Jim Weisbin <email@hidden> wrote:
Open a text document (read-only is fine). The document will always be RTF format, created with TextEdit, so I think that precludes any kind of shell scripting to search it’s contents.
______________________________________________________________________

Hey Jim,

I'm a bit late to this party, but...

Shell scripting is no problem as someone mentioned.  Perl, Python, or Ruby would make short work of this task.

Scripting TextEdit is a pretty sad business but adequate for your purpose.  I would prefer to use an editor with a scriptable search like Tex-Edit Plus or TextWrangler.

Personally I'd use the Satimage.osax for convenience, speed, and versatility.

(If your system is Mavericks you can use a ASObjC-Library for the regex instead of the Satimage.osax, and I believe Shane has posted one.)

A couple of examples are appended.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Using Tex-Edit Plus { http://www.tex-edit.com }
# Nice shareware styled text editor (not limited when unregisterd).
# Highly scriptable.
-------------------------------------------------------------------------------------------
# REQUIREMENT: File must start and end with a CR.
# It is possible to ensure this in the script if necessary.
-------------------------------------------------------------------------------------------

set srcfolder to alias "hfs:path:to:your:folder:"
set fileNameList to list folder srcfolder without invisibles
set missingFileNameList to {}
set foundFileNameList to {}

repeat with i in fileNameList
  set findStr to return & i & return
  tell application "Tex-Edit Plus"
    tell front document
      if (search looking for findStr) = false then
        set end of missingFileNameList to contents of i
      else
        set end of foundFileNameList to contents of i
      end if
    end tell
  end tell
  ""
end repeat

{foundFileNameList, missingFileNameList}

-------------------------------------------------------------------------------------------
# Using the Satimage.osax's regular _expression_ search { http://tinyurl.com/dc3soh }
# It must be installed for the script to work!
-------------------------------------------------------------------------------------------
# Way faster and more versatile than using Tex-Edit Plus.
-------------------------------------------------------------------------------------------

set srcfolder to alias "hfs:path:to:your:folder:"
set fileNameList to list folder srcfolder without invisibles
set missingFileNameList to {}
set foundFileNameList to {}

set AppleScript's text item delimiters to ""
tell application "TextEdit"
  set refFileNameList to text of front document
end tell
set refFileNameList to change "^$\\s+" into "" in refFileNameList with regexp
set refFileNameList to change "\\s+\\Z" into "" in refFileNameList with regexp

repeat with i in fileNameList
  set regEx to change "([\\[\\]{}()^.?*+-])" into "\\\\\\1" in (contents of i) with regexp
  set regEx to "^" & regEx & "$"
  try
    set _found to find text regEx in refFileNameList with regexp
    set end of foundFileNameList to contents of i
  on error
    set end of missingFileNameList to contents of i
  end try
end repeat

{foundFileNameList, missingFileNameList}

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

 _______________________________________________
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

  • Follow-Ups:
    • Re: AS how to check if a file name matches a string in a text file
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Creating nested folder on exchange in apple mail
  • Next by Date: Re: AS how to check if a file name matches a string in a text file
  • Previous by thread: Creating nested folder on exchange in apple mail
  • Next by thread: Re: AS how to check if a file name matches a string in a text file
  • Index(es):
    • Date
    • Thread