From (refined) list to labeled files

This is a bit of an extension of the prior script I was working on. And actually this script I’m about to post works fine, other than “Choose File” dialog boxes NOT coming to the front as I would like. But here’s what’s going on:

I will be handed a list that looks like this.

3
003_image.jpg
9/3/12 7/14/12 @ 07:38:27
6
006_image.jpg
9/3/12 7/14/12 @ 10:01:49
9
009_image.jpg
9/3/12 7/14/12 @ 08:08:09

I’m opening this in TextWrangler and using grep searches to refine the list because as-is, it has more information than I need. All I really want is the file name “003_image.jpg” bits. I’ve got two grep searches that tidy my list. One line takes care of all the extra junk, one takes care of the extra line breaks that occur. Like this:

tell application "TextWrangler"
	activate
	open find window
	replace "^\\d{3}\\s|^\\d{2}\\s|^\\d{1}\\s|^\\d{1}/\\d{1}/\\d{2}.*|^\\d{2}/\\d{2}/\\d{2}.*|^\\d{1}/\\d{2}/\\d{2}.*|^\\d{2}/\\d{1}/\\d{2}.*" using "" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
	replace "\\r\\r" using "\\r" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
	close find window saving no
end tell

So far, so good. This modifies my list to now look like this:
003_image.jpg
006_image.jpg
009_image.jpg

I want to use the above list to “look into a folder” whose contents looks like this:
001_image.jpg
002_image.jpg
003_image.jpg
004_image.jpg
005_image.jpg
006_image.jpg
007_image.jpg
008_image.jpg
009_image.jpg
010_image.jpg

If a file is on the list, it needs to get labeled. This is where my other script chunk comes into play. I’m using System Events to Copy the TextWrangler list to the clipboard, then using the clipboard contents to select and label the files in question:

set theFolder to (choose folder with prompt "Choose a Folder")
set fileNames to paragraphs of (the clipboard)

tell application "Finder"
   set label index of (every item whose name is in fileNames) of theFolder to 1
end tell

So ultimately, I want to combine these two major steps:
¢ Refine the list down (and copy contents to clipboard)
¢ Label files whose names reside in the clipboard

In searching the web last night for grep + TextWranlger + Applescript options I wandered onto a Droplet scenario that is almost what I need. It at least prompts for the text file (holding the list), refines the list and copies the list to the clipboard.

on run
	tell me to set theFiles to choose file "Please choose files:" with multiple selections allowed
	cleanFiles(theFiles)
end run

on open theFiles
	cleanFiles(theFiles)
end open

on cleanFiles(theFiles)
	tell application "TextWrangler"
		repeat with myFile in theFiles
			open myFile opening in new_window
			tell window 1
				replace "^\\d{3}\\s|^\\d{2}\\s|^\\d{1}\\s|^\\d{1}/\\d{1}/\\d{2}.*|^\\d{2}/\\d{2}/\\d{2}.*|^\\d{1}/\\d{2}/\\d{2}.*|^\\d{2}/\\d{1}/\\d{2}.*" using "" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
				replace "\\r\\r" using "\\r" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
				# run unix filter "~/Library/Application Support/TextWrangler/Unix Support/Unix Filters/myFilter.pl"
				
				tell application "TextWrangler" to activate
				
				tell application "System Events"
					tell process "TextWrangler"
						keystroke "a" using command down
						keystroke "c" using command down
						key code 126
					end tell
				end tell
				close saving yes
			end tell
		end repeat
	end tell
end cleanFiles

This works a-okay! Now I’m trying to append the file labeling function to the script. It does indeed work, right now! However, my “Choose FIle” and “Choose Folder” dialog boxes are not coming to the front.

So here’s final combined script with the only issue being the “Choose” windows:

on run
	tell me to set theFiles to choose file "Please choose files:" with multiple selections allowed
	cleanFiles(theFiles)
end run

on open theFiles
	cleanFiles(theFiles)
end open

on cleanFiles(theFiles)
	tell application "TextWrangler"
		repeat with myFile in theFiles
			open myFile opening in new_window
			tell window 1
				replace "^\\d{3}\\s|^\\d{2}\\s|^\\d{1}\\s|^\\d{1}/\\d{1}/\\d{2}.*|^\\d{2}/\\d{2}/\\d{2}.*|^\\d{1}/\\d{2}/\\d{2}.*|^\\d{2}/\\d{1}/\\d{2}.*" using "" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
				replace "\\r\\r" using "\\r" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
				# run unix filter "~/Library/Application Support/TextWrangler/Unix Support/Unix Filters/myFilter.pl"
				
				tell application "TextWrangler" to activate
				
				tell application "System Events"
					tell process "TextWrangler"
						keystroke "a" using command down
						keystroke "c" using command down
						key code 126
					end tell
				end tell
				close saving yes
			end tell
		end repeat
	end tell
	
	
	set theFolder to (choose folder with prompt "Choose a Folder")
	set fileNames to paragraphs of (the clipboard)
	
	tell application "Finder"
		set label index of (every item whose name is in fileNames) of theFolder to 1
		open theFolder
	end tell
	
end cleanFiles

What am I missing here? I know I am really close to a clean solution. Just a few minor details to tidy this up nicely.
Thanks so much for any input here!

Hi,

I guess you can process the text without the clipboard


tell application "TextWrangler"
	set fileNames to paragraphs of (get text of document 1)
end tell

I don’t have TextWrangler, but BBEdit, the basic dictionary might be the same

I am a bit curious about your situation. Are you just getting a plain text file?

I made a plain text file with
3
003_image.jpg
9/3/12 7/14/12 @ 07:38:27
6
006_image.jpg
9/3/12 7/14/12 @ 10:01:49
9
009_image.jpg
9/3/12 7/14/12 @ 08:08:09

Then I made a folder on my Desktop with 20 files named 00n_image.jpg (replacing n as needed).

Without using Textwrangler (not knowing your situation it may be required but if not, I’d bypass it), the image lines can easily be extracted with bash. Then a quick dance out to the Finder to do the labeling.


set fileFolder to alias ((path to desktop) & "fileList" as text) --This is the folder containing the images.
-- Alternately, a POSIX path could be used but it's just easier to pass an HFS path to the Finder. (It likes them more - lol.)
-- Also you could use a choose folder too.

set inputFile to "~/Desktop/fileList.txt" -- This is the file you receive.
-- This could also be a choose file but would need to be converted to a POSIX path for the shell bit (and ideally a quoted form to be safe).

set fileListing to paragraphs of (do shell script "egrep .*jpg " & inputFile)
-- If the file is as well structured as your example, this is trivial.
-- I'm using "paragraphs" to split the results into a list. Otherwise it comes in as text.

tell application "Finder" to set label index of (every file of fileFolder whose name is in fileListing) to 5
	-- You really shouldn't need to loop (repeat.end repeat) here.

Note this isn’t error-trapped but isn’t this doing what you want it to. and in 4 lines (excluding my comments). :smiley:

Jim
BLUEFROG

Jim,

Thanks so much! You really slimmed it down quite a bit. Cleaner, simpler is better!

And no, Textwrangler was not completely necessary. I had only gone that route because I knew about the grep searching functions there ” not thinking that I could skip it altogether with a bash command. So that saved a lot of time and effort.

I did add the choose file and folder options as the locations of the respective files and folders will vary.

I’ve ended up with this for now:

set fileFolder to (choose folder with prompt "Choose a Folder")

set inputFile to (choose file with prompt "Choose a File") as Unicode text
set thePath to POSIX path of inputFile

set fileListing to paragraphs of (do shell script "egrep .*jpg " & quoted form of thePath)

tell application "Finder" to set label index of (every file of fileFolder whose name is in fileListing) to 5

I’m going to experiment with converting this into a droplet whereby I can drop the folder in question onto it and proceed from there. Any reason why that wouldn’t work?

Thanks so much for your help!

Glad to be of service, Kevin. :slight_smile:

You could convert it to a droplet pretty easily but remember that an on open(x) handler will yield a list, even if it’s a list of one. If always run a repeat loop as a matter of habit to avoid annoying errors when you try and process what you think is a file but is really a list. This is the basic, basic shell I use.


on open (theFiles)

repeat with thisFile in theFiles
-- do some cool stuff to each dropped item individually
end repeat

end open

Have fun!
Jim
BLUEFROG