Monday, July 22, 2019

#1 2019-07-09 07:59:23 pm

jfish
Member
From:: Canada
Registered: 2019-07-09
Posts: 4

Change List Repeat Loop Is Processing

Hello,

I'm new to MacScripter and AppleScript in general and have been working on a small script. I have been searching on and off for weeks trying to find an answer to my problem but have not come across anything similar. Perhaps I am not searching for the correct terms or there is a much simpler way to do this.

My goal is to check if the 'fileName' passed to the script contains any of the text strings 'phrases' contained in each paragraph of a text file. I can get this to work using a repeat loop if I only use one text file.

However, I have a handful of text files in a folder that I want the script to check against. My thought was that if the fileName did not contain one of the phrases from the first text file I could increment variable 'type' and pass that to a handler to return the name and list of paragraphs from the next text file in the folder list.

I can return the correct variable and list from the handler but I do not understand or know if you can change the list the repeat loop is checking against.

Applescript:



set theFilePath to theFile as text

-- Remove file path
set AppleScript's text item delimiters to "/"
set fullName to last item of text items of theFilePath
set AppleScript's text item delimiters to {""}

-- Remove extension
set AppleScript's text item delimiters to "."
set fileName to first item of text items of fullName
set AppleScript's text item delimiters to {""}

tell application "Finder"
   set cList to displayed name of every file of entire contents of alias "Users:jfisher:SynologyDrive:#Hazel:Library:Text Files:" as list
end tell

on changeType(type)
   -- Access global list 'cType'
   global cList
   
   if type is less than or equal to (count cList) then
       set cType to item type of cList
       set txtPath to "Users:jfisher:SynologyDrive:#Hazel:Library:Text Files:" & cType & ".txt"
       set matchList to paragraphs of (read alias txtPath) as list
       return {{cType}, {matchList}}
   end if
end changeType

set type to 1

set criteria to item 2 of changeType(type)

repeat with phrase in criteria
   if fileName does not contain phrase then
       set type to type + 1
       display dialog type
       set criteria to item 2 of changeType(type)
   else
       return {hazelPassesScript:true, hazelOutputAttributes:{cType}}
   end if
end repeat

Offline

 

#2 2019-07-10 05:31:00 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4911

Re: Change List Repeat Loop Is Processing

Hi. Welcome to MacScripter.

I can't tell from your script whether you want to return the first matching paragraph found, every match, just the fact that a match exists, or something else. Taking a guess, I've come up with the following. It returns the first matching paragraph found if there is one, otherwise it returns a result indicating no match. It should get you started, but do ask again if you can't adapt it for your actual needs!

Applescript:

set theFilePath to theFile as text

-- Remove file path
set AppleScript's text item delimiters to "/"
set fullName to last text item of theFilePath
set AppleScript's text item delimiters to {""}

-- Remove extension
set AppleScript's text item delimiters to "."
set fileName to first text item of fullName
set AppleScript's text item delimiters to {""}

checkName(fileName)

on checkName(fileName)
   -- Get a list of Finder references to the text files, sorted by name.
   -- For the moment, I've assumed that the "Text files" folder only contains the relevant text files, not in subfolders, and numbered as described in the post #1.
   tell application "Finder" to set cList to (sort every file of folder "SynologyDrive:#Hazel:Library:Text Files:" of home by name)
   
   -- Work through each text file in turn.
   repeat with textFile in cList
       -- Coerce each Finder reference to alias, read the corresponding text file, and extract the paragraphs.
       set textFileAlias to textFile as alias
       set matchList to paragraphs of (read textFileAlias as «class utf8») -- Assuming the text in the file is encoded as UTF-8.
       
       -- Work through the paragraphs and see if any of them contain the file name.
       repeat with i from 1 to (count matchList)
           set phrase to item i of matchList
           -- If any do, return the hit immediately.
           if (fileName contains phrase) then return {hazelPassesScript:true, hazelOutputAttributes:{phrase}} -- Guessing that 'phrase' = 'cType'.
       end repeat
   end repeat
   
   -- If no hits above, return a 'false' result.
   return {hazelPassesScript:false, hazelOutputAttributes:{}}
end checkName


NG

Offline

 

#3 2019-07-10 07:11:56 am

jfish
Member
From:: Canada
Registered: 2019-07-09
Posts: 4

Re: Change List Repeat Loop Is Processing

Wow thank you for the quick response.

I think I understand what is going on but will have to take a closer look tonight to make sure haha. After my initial post I had thought of better way to describe my problem so thought it might help if someone else is looking for a similar answer in the future.

For example, I have multiple text files stored in a folder somewhere , each with the brand name of a vehicle manufacturer as their file name. Each text file would contain relevant vehicle models on each line of the text file.

Now if I pass in a file named F-150.jpg or Mustang.jpg the script will use the paragraphs of the first text file named ‘Dodge’ for instance as its matching criteria. The loop will not find any matching paragraph. The repeat loop would then need to switch to the next text file ‘Ford’ where it will find a match. The loop would then return the name of the text file it found the matching paragraph in to use in return {hazelPassesScript:true, hazelOutputAttributes(BrandName)}, unless it cannot find any match at all and returns {hazelPassesScript:false}.

Hopefully that is a little more clear.

Offline

 

#4 2019-07-10 08:35:57 pm

jfish
Member
From:: Canada
Registered: 2019-07-09
Posts: 4

Re: Change List Repeat Loop Is Processing

Got a chance to run your script. It appears to work well however I would like to actually return the name of the text file that is being processed when a match against one of its paragraphs occurs not the actual phrase that matched.

I believe it would be textFileAlias but textFileAlias returns the path to the file and not its name. Is there a cleaner way to return only the name of the file instead of using text delimiters to clean it up?

Offline

 

#5 2019-07-11 03:30:49 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4911

Re: Change List Repeat Loop Is Processing

jfish wrote:

I believe it would be textFileAlias but textFileAlias returns the path to the file and not its name. Is there a cleaner way to return only the name of the file instead of using text delimiters to clean it up?


Generally, it depends on what you've got, what you want, how many times you need to get one from the other, and what you know about the machine(s) running the script.  smile

Here, textFileAlias is an alias specifier and you want the name of the item to which it refers — without the ".txt" extension, to judge from your original script. If you know that the script will only ever be run on machines where the users' Finder preferences are set not to "show all filename extensions", you might get away with changing this line in my script …

Applescript:

if (fileName contains phrase) then return {hazelPassesScript:true, hazelOutputAttributes:{phrase}} -- Guessing that 'phrase' = 'cType'.

… to this :

Applescript:

if (fileName contains phrase) then
   tell application "Finder" to set cType to displayed name of textFileAlias
   return {hazelPassesScript:true, hazelOutputAttributes:{cType}}
end if

But otherwise the fastest and safest way would be to replace it with something like this:

Applescript:

if (fileName contains phrase) then
   set textFilePath to textFileAlias as text
   set astid to AppleScript's text item delimiters
   set AppleScript's text item delimiters to ":"
   -- Extract the file name from the HFS path. We can assume that the path to a readable text file doesn't end with a colon.
   set cType to text item -1 of textFilePath
   -- Drop the name extension if there is one, bearing in mind that names can contain more than one dot.
   if (cType contains ".") then
       set AppleScript's text item delimiters to "."
       set cType to text 1 thru text item -2 of cType
   end if
   set AppleScript's text item delimiters to astid
   
   return {hazelPassesScript:true, hazelOutputAttributes:{cType}}
end if

Last edited by Nigel Garvey (2019-07-11 03:31:36 am)


NG

Offline

 

#6 2019-07-16 07:36:57 pm

jfish
Member
From:: Canada
Registered: 2019-07-09
Posts: 4

Re: Change List Repeat Loop Is Processing

Nigel,

I used the second suggestion you posted and the script appears to work great. Just as intended. I appreciate all the help.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)