Subject: AS how to check if a file name matches a string in a text file
Date: June 29, 2014 at 9:28:56 AM PDT
Hello. I’m fairly comfortable writing AS, but not an expert by any means.
I need to be able to do the following, and am looking for some pointers before I begin:
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.
Open a folder and iterate through the filenames there (filenames only, no paths. That part is easy).
For each file name, see if it that exact name matches a line of text in the text doument. Lines are paragraphs with the normal Unix delimiters I think (not sure how it’s done in RTF).
Final dialog box will show a list of all matched filenames and total number of files dropped on the AS app.
My biggest question is, should I try to do this via TextEdit’s AS dictionary, or is there another way I should be thinking about?
I could insist that the text doument be plain text instead, if that makes it much easier.
If you want to do this with applescript, it’s easiest to convert the document to plain text first:
-- get the file names from the document as an AS list
set doctxt to paragraphs of (do shell script "textutil -convert txt -stdout '/path/to/doc.rtf'")
tell application "System Events"
-- get the file names from the system
set fileNames to name of files of folder "/path/to/folder"
end tell
set foundFiles to {}
repeat with thisFileName in fileNames
-- compare filenames and store results
if thisFileName is in doctxt then copy thisFileName to end of foundFiles
end repeat
--convert found files list to text and display
set {oldTID, my text item delimiters} to {my text item delimiters, return}
set displayText to foundFiles as text
set my text item delimiters to oldTID
display dialog displayText