Search for filename with Regex

Hi,

i am realy new in Applescripting and i need some support.


I want to search for files with a specific name (Regex [A-Z]{2}[0-9]{2}\-[A-Z]{1}[0-9]{2}\_[0-9]{5}\_[A-Z]{2} ) and those, which aren't correcty written are supposed to colored with a red label.


Thanks for your help!


best regards

MacBook Pro (Retina, 15-inch, Early 2013), iOS 11.4.1, null

Posted on Oct 10, 2018 5:00 AM

Reply
1 reply

Oct 10, 2018 2:55 PM in response to ShoryuReppa

AppleScript has no native regular expression capability without third-party extension libraries. Personally, I use one of two different avenues in AppleScript: 1) a do shell script one-liner, and 2) a HERE script embedded in an AppleScript function (handler). Thus I have complete freedom over using perl, sed, awk, ruby, python, and AppleScript/Objective-C approaches to regular expressions depending on how trivial or complex the problem.


Here is how I would approach your problem using an AppleScript handler (Ruby HERE script) to send back the found files to the main AppleScript. Capitalizes on Ruby's strong regex capability. The following is the structure, but is untested:


The following code is scrollable in the x and y dimensions. Copy/paste into Script Editor, and test. It will likely explode.


property tagColors : {Unset:0, Orange:1, Red:2, Yellow:3, Blue:4, Purple:5, Green:6, Gray:7} property aDesktop : (path to desktop) as alias property amsg : "Select the folder to begin your Regex file search:" try set start_folder to POSIX path of (choose folder with prompt amsg default location aDesktop without invisibles, multiple selections allowed and showing package contents) on error errmsg number errnbr if errnbr = -128 then display alert "User Canceled, script ending." as critical giving up after 10 end if end try -- append each found filename and append to the list set regexList to paragraphs of my process_files(start_folder) if regexList contains {"None"} then return tell application "Finder" repeat with afile in regexList set the label index of ((POSIX file afile) as alias) to Red of tagColors end repeat end tell return on process_files(afolder) return do shell script "ruby <<EOF - " & afolder & " #!/usr/bin/ruby # must escape backslash with another, and double-quotes to appease AppleScript file_regex = /[A-Z]{2}[0-9]{2}\\-[A-Z]{1}[0-9]{2}\\_[0-9]{5}\\_[A-Z]{2}/ # ARGV.first is the start_folder name passed into Ruby # folder recursion is '**' and non-recursion is single '*' file_list = Dir.glob(File.join(ARGV.first, \"**\", \"*\")).grep(file_regex) puts 'None' && exit unless file_list # return every found file with a trailing newline file_list.each do |f| puts f end EOF" end process_files

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Search for filename with Regex

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.